----------------------MySQL列属性——惟一键(unique)-------------- create database day3 charset=utf8; use day3; --unique [key] --惟一键,保证数据惟一性 --惟一键和主键区别: --1.主键只有一个,惟一键能够有多个 --2.主键不能重复,不能为空 --3.惟一键不能重复,能够为空(NULL) --方法一:定义字段时添加 --key能够省略,一个表的惟一键能够有多个 --unique --unique key create table `unique` ( stuid int primary key, stuname varchar(20) unique key, stuaddr varchar(50) unique )engine=innodb charset=utf8; --方法二:单独指定 create table `unique2` ( stuid int primary key, stuname varchar(20), stuaddr varchar(50), unique key (stuname), unique (stuaddr) ); --将两个字段(两列)组合为一个惟一键 create table `unique3` ( stuid int primary key, stuname varchar(20), stuaddr varchar(50), unique key(stuname,stuaddr) ); --方法三:修改字段属性时添加 --alter table add unique create table `unique4` ( stuid int primary key, stuname varchar(20), stuaddr varchar(50) ); alter table `unique4` add unique key (stuname),add unique (stuaddr); --删除unique键 --使用index关键字删除unique alter table `unique4` drop index stuname; alter table --添加两个字段组合为一个unique键 --组合unique键在MySQL中存储的index名默认为第一个字段名 alter table `unique4` add unique (stuname,stuaddr); show create table `unique4`; --删除组合unique键 alter table `unique4` drop index stuname; --为组合unique键命名 alter table `unique4` add unique uuu (stuname,stuaddr); show create table unique4; --添加值 insert into `unique4` values (1,'aa','aa'); insert into `unique4` values (2,'aa','bb'); -----------------------MySQL列属性——备注(comment)--------------- --comment create table `comment`( stuno int primary key comment '学生编号', stuname varchar(20) comment '学生姓名' ); -----------------------MySQL列属性——注释--------------- --注释方法1:-- --注释方法2:# --注释方法3:/**/(多行注释) -------------------数据完整性-------------------- --一、实体完整性 ----a)主键约束 ----b)标识列(自动增加列) ----c)惟一约束 --二、域完整性 ----a)数据类型约束 ----b)非空约束 ----c)默认值约束 --三、引用完整性 ----a)外键约束 --四、自定义完整性 ----a)存储过程 ----b)触发器 -------------------主表和从表---------------- --一、 主表中没有对应的记录,从表中不容许插入 --二、 从表中有的,主表中不容许删除。 --三、 先删除从表,再删主表 -----------------------外键(foreign key)--------------------- --foreign key --外键:从表中的公共字段 --外键用来保证引用完整性 --公共字段的名字能够不同,可是类型必须是同样的 --外键的操做 --1.严格操做 --2.置空操做(set null) --3.级联操做(cascade) --建立外键 --方法一:建立表的时候建立外键 --建立主表 create table `foreign_key` ( stuid int primary key, stuname varchar(20) ); --建立从表 create table `foreign_key_2` ( stuno int primary key, score int, foreign key(stuno) references `foreign_key`(stuid) ); show create table `foreign_key_2`; --删除表时要先删除从表,再删除主表,先删主表会报错 drop table `foreign_key_2`; drop table `foreign_key`; --方法二:修改表时建立外键 create table `foreign_key` ( stuid int primary key, stuname varchar(20) ); create table `foreign_key_2` ( stuno int primary key, score int ); alter table `foreign_key_2` add foreign key (stuno) references `foreign_key`(stuid); --建立外键的时候指定外键的名字 --constraint 'name' alter table `foreign_key_2` add constraint `FK1` foreign key(stuno) references `foreign_key`(stuid); --删除外键 alter table `foreign_key_2` drop foreign key `FK1`; --级联操做 --当主表主键删除的时候,从表置空,主表更新的时候从表也更新,即级联 --只有innodb的引擎才支持主外键,myisam是不支持的。 --MySQL5.5默认引擎是innodb,低版本默认为myisam的。 --建立主表 create table fk1 ( stuno char(3) primary key, stuname varchar(20) ); --建立从表 create table fk2 ( stuno char(3), stuid int auto_increment primary key, score int, foreign key (stuno) references fk1(stuno) on delete set null on update cascade ); --测试 insert into fk1 values ('001','丁伟韬'); insert into fk1 values ('002','叶利云'); insert into fk1 values ('003','孙峰'); insert into fk2 values ('001',1,99); insert into fk2 values ('002',2,88); insert into fk2 values ('003',3,77); delete from fk1 where stuno='001'; update fk1 set stuno='200' where stuno='002'; -------------------查询语句--select------------- /*stu测试数据*/ create table stu ( stuNo char(6) primary key, stuName varchar(10) not null, stuSex char(2) not null, stuAge tinyint not null , stuSeat tinyint not null, stuAddress varchar(10) not null, ch tinyint, math tinyint ); insert into stu values ('s25301','张秋丽','男',18,1,'北京',80,null); insert into stu values ('s25302','李文才','男',31,3,'上海',77,76); insert into stu values ('s25303','李斯文','女',22,2,'北京',55,82); insert into stu values ('s25304','欧阳俊雄','男',28,4,'天津',null,74); insert into stu values ('s25305','诸葛丽丽','女',23,7,'河南',72,56); insert into stu values ('s25318','争青小子','男',26,6,'天津',86,92); insert into stu values ('s25319','梅超风','女',23,5,'河北',74,67); --select --选择并显示 select 10; select 10*10; --显示时间戳 select unix_timestamp(); --显示随机数 select rand(); --as --as关键字用来给字段取别名 select 10*10 as total; select ch,math,ch+math as total from stu; --as 能够省略 select 10*10 total; select ch+math total from stu; /*测试笛卡尔积数据*/ create table stu_info( name varchar(10), sex char(1) ); create table stu_marks( ch tinyint, math tinyint ); insert into stu_info values ('tom','男'),('berry','女'); insert into stu_marks values (11,11),(22,22); --from --from后面跟的是数据源,数据源能够有多个,返回的是笛卡尔积 select * from stu_info,stu_marks; --* --*查询全部字段 select * from stu; --table.key --明确某个表的字段 --select 字段1,字段2 from 表1,表2 --查询部分字段 --dual表 --dual表是个伪表,用来保证select语句的完整 --有些状况下不能省略from,可是又没有确切的表,这时候就用dual伪表 select 10*10 from dual; --where --where是在数据源中进行筛选 --查询的结果是一张表,(这张表的结构可能与数据库表的结构不同) select * from stu where stuage>25; select stuName,ch+math as '总分' from stu where stuSex='男'; --1表示真,显示全部数据 select * from stu where 1; --0表示false,查询不到记录 select * from stu where 0; --子查询 --在查询结果的表中继续查询 --高级查询 --is null --is not null --查询值非空/不为空的数据 select * from stu where ch is null or math is null; select * from stu where ch is not null and math is not null; --in --not in --表示在/不在某个范围内 select * from stu where stuaddress='北京' or stuaddress='上海'; select * from stu where stuaddress in ('北京','上海'); select * from stu where stuaddress not in ('北京','上海'); --between...and... --not between...and... select * from stu where stuage>=20 and stuage <=25; select * from stu where stuage between 20 and 25; select * from stu where stuage not between 20 and 25; ---------------------------聚合函数--------------- --sum --sum(key) select sum(ch) as '语文总分' from stu ; --avg --avg(key) select avg(math) as '数学平均分' from stu; --max --max(key) select max(stuage) as '学生中的最大年龄' from stu; --min --min() select max(ch) as '语文最低分' from stu; --count --count() select count(*) as '总人数' from stu; ------------------------模糊查询--------------------- --通配符 --'_'下划线:表示一个字符 --'%'百分号:表示任意字符 show tables like 's%'; select * from stu where stuname like '李%'; select * from stu where stuname like '__丽%';