-
数据类型
float(5,2);单精度
double(5,2);双精度
此上两种均为小数类型,整数部分为3位,小数部分为2位。
date:yyyy-mm-dd 格式;
time:hh:mm:ss 格式;
datetime:yyyy-mm-dd hh:mm:ss 格式;
sysdate():显示当前系统日期。
enum (“男”,“女”); 只可输入男或女。 -
表的约束
primary key: 主键约束,唯一+非空。
unique:唯一约束。
not null:非空约束。例如
create table Teacher
(
id int(4) primary key,
tid int(4) unique,
tname char(20) not null
);
联合主键:例如
create table Teacher
(
id int(4) ,
tid int(4) ,
primary key (id,tid)
);
该表的 id 与 tid 共为主键。foreign key:外键。
例如
create table student
(
sid int(4) primary key,
tname char(20) not null,
tid int(4) ,
foreign key (tid) references teacher (tid)
);
该表的字段 tid 为外键,依附于teacher的主键 tid;default:默认性约束
例如
create table student
(
sid int(4) primary key,
tname char(20) default “张三”
);
若该字段为 null,则默认填写为 “张三”。check(); 检查约束
例如
create table score
(
sid int(4) primary key,
ChengJi int(3) ,
check(ChengJi <101)
);
该表的成绩字段值必须小于101. -
表的修改
更改表的名称。
rename table 旧表名 to 新表名;
为表添加主键。
alter table 表名 add primary key (字段名);
为表删除主键。
alter table 表名 drop primary key;
为表添加非空约束。
alter table 表名 modify 字段名 数据类型(长度) not null;
为表删除非空约束。
alter table 表名 modify 字段名 数据类型(长度) null;
修改字段名
alter table 表名 change 旧名 新名 类型(长度);
删除字段
alter table 表名 drop column 字段名。
修改字段类型
alter table 表名 modify 字段名 类型(长度);
为表添加外键
alter table 表名 add constraint 外键约束名(自定义) foreign key (字段名)
references 被引用的表名 (被引用的字段名);
删除外键
第一步: alter table 表名 drop foreign key 外键约束名;
第二步:drop index 外键约束名 on 表名; -
查询
select * from teacher
where salary between 3000 and 6000;
查询工资在 3000~6000阶段的员工数据。is null:为空。
is not null:不为空。
例如
select * from teacher
where tname is null;
查询表中名字为空的老师数据。 -
聚合函数
max():最大值。 min():最小值。avg():平均数。sum():求和。count():个数。
例如
select max(字段名) 别名 , min(字段名) 别名,count(*) 别名 from 表名。 -
其他
AUTO_INCREMENT: 主键自增长
例如
create table student
(
sid int(4) primary key auto_increment,
sname char(20)
);
该表的主键 sid 可自动增加,由1开始。Show keys from 表名;
查看该表的键值。order by 字段名 [规则]:对表的数据排序,asc是由低到高(默认),desc是由高到低。
例如
select * from 表名
order by age ;
按年龄 由低到高的顺序进行排序。select * from 表名
order by age , class desc;
多字段排序,年龄 由低到高,班级由高到低。
软件测试-Mysql数据库2
最新推荐文章于 2023-09-12 01:23:15 发布