mysql数据库
一、如何连接数据库
1、命令连接
mysql-hIP地址 -P端口号 -uroot -p密码
2、mysql数据库客户端(navicat)
二、数据类型
1、数值型
整型
tinyint smallint
mediumint int bigint
定义:tinyint(m) unsigend zerofill
m代表数值长度,不影响取值范围
unsigned 无符号类型,影响取值范围,不影响数据长度
zerofill,0填充,一般与m配合使用
浮点型
float
double
定义:float(m,z)
m表示数值总长度
z表示小数点后的位数
2、字符型
char
varchar
1、定义:char(m)
m表示在内存中占用的位数
特点:分多少,占多少
读取速度快,内存利用率不高
当输入的字符长度小于m时,系统会自动在字符后用空格填充,当输出时,系统会自动过滤空格
当输入的字符后含有空格时,输出时,不会原样输出
2、定义:varchar(m)
m表示在内存中占用的位数
特点:用多少,占多少
读取速度慢,内存利用率高
当输入的字符长度小于m时,系统不会在字符后用空格填充
当输入的字符后含有空格时,输出时,原样输出
3、时间日期
1、datetime
范围:
2、date
范围:
3、time
范围:
4、year
范围:
三、约束类型
1、主键约束
不为空且不可重复
primarykey
举例:
create table student(
sid tinyint(3) unsigned zerofill primary key,
sname char(10)
) charset utf8;
2、唯一约束
不可重复
unique
举例:
create table student(
sid tinyint(3) unsigned zerofill primarykey,
sname char(10) unique
) charset utf8;
3、非空约束
不可为空
notnull
举例:
create table student(
sid tinyint(3) unsigned zerofill primarykey,
sname char(10) unique,
sage tinyint(3) unsigned not null
) charset utf8;
4、默认约束
默认约束字段不给值时,显示默认值
default
举例:
create table student(
sid tinyint(3) unsigned zerofill primarykey,
sname char(10) unique,
sage tinyint(3) unsigned not null,
ssex char(5) default ‘男’
) charset utf8;
5、外健约束
与别的表建立关系
foreignkey
references
例子:
主表:create table student(
sid tinyint(3) unsigned zerofill primarykey,
sname char(10) unique,
sage tinyint(3) unsigned not null,
ssex char(5) default ‘男’
) charset utf8;
从表:create table score(
sid tinyint(3) unsigned zerofill,
sc int(3),
constraint 外健名称foreign key(字段名) references 主表名称(主表字段)
);
注:constraint 外键名称 是给外健取一个名字,可省略
不写时,系统会默认给他一个外健名字
建立外健约束时,字段的数据类型必须完全一致
外键名称的格式:主表_从表_fk (主表和从表可以缩写)
四、基本sql语句
基于库的sql
1、创建库
createdatabase 库名;
2、查看当前数据库中有哪些库
showdatabases;
3、进入库中
use库名;
4、删除库
dropdatabase 库名;
5、库名一经创建,不允许修改
基于表的sql
1、创建表
createtable 表名(
字段名a 数据类型 [约束],
字段名b 数据类型 [约束],
···
字段名n 数据类型 [约束]
)charsetutf8;
2、查看表结构
desc表名;
3、查看当前库中有哪些表
showtables;
4、插入数据
insert into 表名()values(字段值列表) ;
insert into 表名(字段列表) values(字段值列表);
insert into 表名()values(字段值列表),(字段值列表)···;
insert into 表名(字段列表) values(字段值列表1),(字段值列表2)···;
5、查询
select字段列表 from 表名 where/having 条件表达式;
6、更新表中数据
update表名 set 字段名=新值 where/having 条件表达式;
7、删除表中数据
deletefrom 表名 where/having 条件表达式;
8、给表重命名
renametable 旧表名 to 新表名;
9、删除表
droptable 表名;
10、给表中增加一列
altertable 表名 add 字段名 数据类型 [after 字段名a];
11、查看建表语句
Show create table 表名
12、删除外键
altertable 表名 drop foreign key cjb_ibfk_1
13、添加外键
altertable表名add foreign key(字段名)references 主表表名(主表字段名)
14、删除主键
altertable 表名drop primary key
15、添加主键
alter table 表名 add primary key(字段名)
改变表结构
13、新增一个字段
alter table 表名 add 字段名 数据类型 [约束];
alter table 表名 add 字段名 数据类型 [约束]after 字段a;
alter table 表名 add 字段名 数据类型 [约束] afterfirst;
14、删除一个字段
alter table 表名 drop 字段名;
15:修改字段的名称/数据类型
alter table 表名 change 字段名称 新字段名 数据类型 [约束] [comment '注释说明']
16、改变数据类型
alter table 表名 modify 字段名称 新数据类型 [约束] [comment '注释说明']
alter table 表名 change 字段名称a 字段名称a 新数据类型 [约束] [comment '注释说明']
17、删除表中主键
alter table 表名 drop primary key;
18、删除表中外键
alter table 表名 drop foreign key 外键名;
19、添加主键
alter table 表名 add primary key(字段名);
alter 表名 add [constraint 主键约束名] primary key(列名)
20、添加外键
alter table 表名 add [constraint 外键约束名] foreign key(列名) references 引用外键表(列名)
如:
alter tableStu_PkFk_Sc
add constraintFk_s
foreign key(sno)
referencesStu_PkFk_S(sno);
五、where条件表达式
1、精确查询
=,>,=,)
2、模糊查询
like/notlike
通配符:% 匹配0~多个字符
- 匹配1个字符
3、区间查询
between···and
not between···and
4、逻辑
and/or
and与or同时出现时,先计算and,后计算or
5、非空运算
isnull/is not null
6、集合运算
in/notin
六:having条件表达式:
having必须跟group by一起使用
having后可以跟聚合函数
七、having与where的区别:
1、where条件表达式是从表中筛选数据
having条件表达式是从查询结果中筛选数据
2、where条件表达式后,不可以跟聚合函数作为查询条件
having件表达式后,可以跟聚合函数作为查询条件
3、having子句中可以使用字段别名,而where只能使用表中存在的字段
八、聚合函数
count
sum
avg
max
min
九、分组、排序、去重
groupby 字段(一般与聚合函数一起使用)
orderby 字段 排序
distinct去重
十、单表查询
十一、多表查询
1、嵌套查询(子查询)
根据已知条件,去求我们需要的数据
特点:记录永不增长,逻辑复杂
2、等值连接
select查询字段 from A,B where A.X=B.X [and +其他查询条件]
[groupby 字段名]
[having条件表达式]
[orderby 字段名 [asc/desc] ]
3、内连接
select查询字段 from A inner join B on A.X=B.X
[where条件]
[group by 字段名]
[having条件表达式]
[orderby 字段名 [asc/desc] ];
注:等值连接与内连接,查询的是两个表中信息对等的数据
4、左外连接(左连接)
select查询字段 from A left join B on A.X=B.X
[where条件]
[group by 字段名]
[having条件表达式]
[orderby 字段名 [asc/desc] ];
左连接:左表作为主表,左表向右表进行匹配,匹配成功,数据正常显示,
匹配不成功时,左表信息正常显示,右表信息显示为null
5、右外连接(右连接)
select查询字段 from A right join B on A.X=B.X
[where条件]
[group by 字段名]
[having条件表达式]
[orderby 字段名 [asc/desc] ];
右连接:右表作为主表,右表向左表进行匹配,匹配成功,数据正常显示,
匹配不成功时,右表信息正常显示,左表信息显示为null
注:左连接与右连接可以查询出两个表中信息不对等的数据