- 完整性约束
- 什么是数据完整性
数据的准确性和可靠性。
- 分类
- 实体完整性
记录准确的。(记录不能重复)
- 主键约束: 不能重复,不能为空。 Primary key
字段唯一的。
不能使用业务字段。
无意义的不重复的数据。
#1. create table stu( sid int primary key, sname varchar(20), age int );
#2. create table stu( sid int, sname varchar(20), age int, primary key(sid) ); #3. 约束 主键约束的名称 alter table student add CONSTRAINT PK_SID primary key(sid);
# 删除主键约束 alter table sc drop primary key; 主键约束只能有一个。 但是允许有联合主键(多个字段整体作为主键)。 |
- 唯一约束:字段值不重复 unique
#唯一约束 alter table student add CONSTRAINT UN_CARD unique(card); |
- 主键自增(1开始 增长1 ) auto_increment
#主键自增 create table stu( sid int primary key auto_increment, sname varchar(20) ); alter table student modify sid int auto_increment; |
- 域完整性
域:字段的值准确。
- 类型约束
- 非空约束 not null
- 默认值 default
create table student( sid int primary key auto_increment, sname varchar(20) not null, age int, sex varchar(10) default '男' ) |
- 引用完整性: 外键约束
外键字段所在表取值只能参考另一张表的主键的取值。
#外键约束 alter table student add CONSTRAINT FK_CLID foreign key(clid) references classroom(cid); |
- 自定义完整性 check(oracle支持)
Alter table student add constraint CK_AGE check(age between 1 and 150);
- 运算符
- 算术运算符 + - * / %
- 比较运算符
- 逻辑运算符
- 位运算符
#运算符 #1.算术运算符 select 1+1; select 1-1; select 2*3; select 3/2; #1.5 select 3/0; #null select 3 div 2; #取整 select 3%2; #2.比较运算符 > < >= <= !=/<> true 1 / false 0 select 1>2; select 1<2; select 1=2; select 1!=2; select 1<>2; #3.逻辑运算符 select 1>2 and 2<3; select 1>2 or 2<3; select !(1<>2); #4.位运算(二进制) select 2&3; #按位与 2 select 2|3; #按位或 3 select 2^3; #按位异或 1 |
- DML
- 添加(插入) insert
#insert # 3 ls 1 insert into student values(3,'ls',1); # 插入部分字段 insert into student(clid,sname) values(1,'ww'); # 插入三条数据(批量插入) insert into student(sname,clid) values('zl',1),('fq',1),('cb',1); #复制表中的值 insert into stu select * from student; #向date类型数据插入数据(oracle不允许) insert into stu(sname,clid,birthday) values('zs',1,'1998-09-09'); |
- 修改 update
#update update tname set 字段=新值 [where 条件] update stu set sname='bq' where sid = 2; |
- 删除
delete from tname [where 条件] delete from stu where sid = 10;
# 逐行删除 delete from stu; # 清空表 truncate table stu;
delete逐行删除,truncate清空内部数据文件。 Truncate效率高于delete。 Truncate自增重置,delete不会重置自增。 |
- 查询
查询的语法结构Select 子句 From tname(结果集) [where 条件 Group by 分组 Having 条件 Order by 排序 Limit 限制结果 ] #查询 # 查询emp表中所有的数据。 select * from emp; # 查询emp表中所有的员工姓名和职位信息。 select ename,job from emp; # 查询emp表中工资超过2000的员工信息。(单一条件) select * from emp where sal >= 2000; # 查询emp表中工资大于2000并且不在20号部门的员工信息。(组合条件) select * from emp where sal > 2000 and deptno <> 20; # 查询emp表中工资大于2000和20号部门的员工信息。 select * from emp where sal > 2000 or deptno = 20; # 查询emp表中工资在1000到2000之间的员工信息。(范围查询) select * from emp where sal BETWEEN 1000 and 2000; # 查询emp表中职位是CLERK,MANAGER,SALESMAN的员工信息。(集合查询) select * from emp where job in ('CLERK','MANAGER','SALESMAN'); # 查询emp表中所有的职位信息。(去重) select distinct job from emp; # 起别名(字段 表 结果集) [as] 别名 select 1+1 sum; select ename '姓名' from emp; select e.empno,e.ename from emp e; # 排序 order by 排序字段 [ASC|DESC] select * from emp order by sal desc; # 工资降序排序,工资一样的按照empno的降序排序。 select * from emp order by sal desc,empno desc; # 查询没有奖金的员工信息。 is null / is not null select * from emp where comm is not null; # 查询emp表中姓名S打头的员工信息。(模糊查询) like %:0到多位字符 _:代表一位 select * from emp where ename like 's%'; select * from emp where ename like '%t'; select * from emp where ename like '%s%'; #包含 # 第二个字母是L的员工 select * from emp where ename like '_l%' # 限制结果查询 limit index,length select * from emp limit 1,5; |
|
- 函数
- 单行函数
- 数学函数
- 字符函数
- 日期函数
#数学函数 select abs(-10); #绝对值 select ceil(-12.8); #向上取整(大最接近的整数) select floor(-12.3); #向下取整 select rand(); #[0,1)随机数 select POW(2,3); #求幂运算 select SQRT(9); #开方 select ROUND(14.35,-1); #四舍五入 select MOD(5,2); #取余 #字符函数 select LENGTH('this'); select length(ename) from emp; select UPPER('this'); select LOWER(ename) from emp; select CONCAT('this ','is'); #连接 select SUBSTR('abcdef',1,3); #字符串截取从1开始 select LPAD('aa',10,'*'); #左填充 select RPAD('aa',10,'*'); #右填充 select trim(' aa a '); #去空格 #日期函数 #获取日期时间 select now(); select sysdate(); #获取日期 select CURDATE(); select CURTIME(); select YEAR(now()); select MONTH('1998-09-09'); #select DAY(date); #日期计算 select DATE_ADD(NOW(),interval 1 MONTH); select LAST_DAY(now()); #获取某个月最后一天 |
- 聚合函数
Max()
Min()
Avg()
Sum()
Count()
#聚合函数 select max(sal) from emp; select min(sal) from emp; select avg(sal) from emp; select sum(sal) from emp; select count(*) from emp; #统计记录数 select count(1) from emp; #统计记录数 select count(comm) from emp; #统计该字段不为空的数目 |
- 分组函数
#分组函数 group by 分组字段 / having 对分组的结果再次进行检索,可以出现聚合函数
# 查看每个部门的平均工资 select deptno,avg(sal) from emp group by deptno; # 查询平均工资>2000的部门的编号和平均工资。 select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000; |
- 加密函数
#加密函数 select MD5('root'); select SHA('root'); select PASSWORD('root'); |