MySQL-基本使用

来源:互联网 发布:宾夕法尼亚大学 知乎 编辑:程序博客网 时间:2024/06/16 14:38

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。
MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。接下来记录下MySQL的基本使用:

常见术语

  • 数据库:数据库是一些关联表的集合
  • 数据表:表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格
  • :一列包含了相同的数据
  • :一行是一组相关的数据
  • 冗余:存储两倍数据,冗余可以使系统速度更快
  • 主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据
  • 外键:外键用于关联两个表
  • 索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。类似于书籍的目录
  • 复合键:复合键(组合键)将多个列作为一个索引键,一般用于复合索引
  • 参照完整性:参照的完整性要求关系中不允许引用不存在的实体。与实体完整性是关系模型必须满足的完整性约束条件,目的是保证数据的一致性

登录我的MySQL

mysql -uroot -p     # root为用户名

成功显示:

image

如果出现:
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
修改密码,请看:http://blog.csdn.net/y472360651/article/details/73565221

退出MySQL

exit;

查看我的MySQL所有数据库

show databases;

效果如图:

image

进入某个数据库

use xxx;    # xxx为数据库名

效果如图:

image

查看该数据库所有表

show tables;

效果如图:

image

查看表的结构

desc xxx;   # xxx为表名或show columns from xxx;

效果如图:

image

查看表的所有数据

select * from xxx;      # xxx为表名

效果如图:

image

因为我们用的是终端操作的,而且数据过多,所以会显示的乱七八糟的,此时我们可以用命令:
select * from xxx\G;
进行格式化一下使得更加具有可读性:

image

为MySQL创建用户

为MySQL创建用户,起始就是在mysql数据库中的user表添加一个数据而已

'''grant all 表示拥有所有权限,也可指定权限(grant select,insert,update,delete)*.* 可操作所有数据库下的所有表,也可以指定(如:test.info。test数据库下的info表)'python'为用户名,'localhost'为域名,此时代表本机'123456'为密码'''grant all on *.* to 'python'@'localhost' identified by '123456'

创建数据库

create database xxx charset "utf8";      # xxx为数据库名 设置编码为utf8,使其能支持中文

删除数据库

drop database xxx;      # xxx为数据库名

创建表(例:student表)

# 创建表,id自增,name不能为空,age不能为空create table student(id int auto_increment,name text not null,age int not null,primary key(id));

删除表

drop table student;     # 删除student表

给表插入信息(例:student表)

insert into student(name,age) values("laowang",38);

此时我们来看一下student表的结构:

image

查看表的所有数据信息(例:student表)

  • 查看所有
select * from student;

效果如图:

image

  • limit:限制查找条数
select * from student limit 2;      # 限制值查找2条
  • offset:设置查找位置,需跟limit结合使用,并且需放置在limit后面
select * from student limit 2 offset 1;     # 从第一个位置查找,限制为2条
  • where和and:限制条件
select * from student where age > 35;       # 查找所有age大于35的数据select * form student where age > 35 and age < 40   # 查看所有age大于35并且小于40的数据
  • like:模糊查询
select * from student where name like 'l%'  # 查询name以'l'开头的数据
  • order by:排序,desc降序,asc升序。默认为asc
select * from student order by age desc;    # 降序查询
  • group by:分组\
    全部数据如图:

image

================================================================================================

select name from student group by name;     # 以名字分组

效果如图:

image

================================================================================================

select name,count(*) from student group by name;    # 以名字分组,并获取每组的人数

效果如图:

image

================================================================================================

select name,sum(age) from student group by name;    # 以名字分组,并获取每组的年龄和

效果如图:

image

================================================================================================

select name,sum(age) from student group by name with rollup;    # 以名字分组和获取每组的年龄和,并且获取每组相加的总和

效果如图:

image

================================================================================================

select coalesce(name,"total_age"),sum(age) as count_age from student group by name with rollup;     # 起别名

效果如图:

image

删除数据

delete from student where name = 'laowang'  # 删除name为laowang的数据,如果没有where条件,将删除表所有数据

更新数据

update student set age = 40 where name = "laowang"      # 把name为laowang的age改为40

对表的字段操作

  • 删除表的一个字段
alter table student drop age;       #  删除student表的age字段
  • 为表添加字段
alter table student add phone int not null;     # 为student表添加phone字段并且phone不能为空
  • 修改字段类型
alter table student modify age text not null;    # 将student表的age字段改为text类型
  • 修改字段默认值
alter table student modify age text not null default "20";      # 修改age的默认值,以后添加的数据,如果age无需手动赋值,自动赋值为20
  • 修改字段名
alter table student change age a int not null;      # 将student表的age字段改为a
  • 修改表名
alter table student rename r_student;       # 将student改为r_student

外键的使用

表1:create table student(    id int auto_increment primary key,    name text not null,    age int not null,    date date not null default "2017-6-20");表2:create table study_record(    id int auto_increment primary key,    day int not null.    stu_id int not null,    foreign key(stu_id) references student(id));是stu_id与student表的id进行约束,那么stu_id就是study_record的外键。

inner join内连接(交集)

select * from A inner join B on A.a = B.b;等价于:select A.*,B.* from A,B where A.a = B.b;

A:imageB:
imageA inner join B:image

left join左连接与right join右连接,差集

select * from A left join B on A.a = B.b;select * from A right join B on A.a = B.b;

A left join B:imageA right join B:image

full join(并集)

在MySQL中没有full join用法,但我们可以使用其他方式实现此种功能

select * from A left join on A.a = B.b union select * from A right join on A.a = B.b;

效果如图:

image

原创粉丝点击