CREATE DATABASE 库名;
SHOW databases;
use 库名;
drop database 库名;
show tables;
create table class(id int, name varchar(128), teacher varchar(64));
insert into class values(101,'六年级一班','马老师');
select*from class;
select*from class where class_id=101;
create table class1(class_id int primary key,class_name varchar(128) not null,class_teacher varchar(64) unique);
create table class2(class_id int(8) zerofill,class_name varchar(128),class_teacher varchar(64));
create table class3(id int auto_increment primary key,name varchar(128),teacher varchar(64));
insert into class3(name,teacher) values('六年级三班','贰老师');
create table class4(fl float,dl double,de dec(10,9));
create table time1(y year,d date,t time, dt datetime,ts timestamp);
insert into time1 values(2020,'2020-10-1','24:10:56','2020-10-01 23:10:57','2020:10:01 23:10:56');
insert into time1 values(year(now()),curdate(),time(now()),now(),now());
default null comment'注释'
create table userinfo(_no int unsigned auto_increment primary key ,
_name varchar(64) default null comment'姓名',
telephone char(11) default null comment'电话',
home varchar(128) default null comment'家庭地址',
prpr text default null comment'个人简历',
sex enum('男','女','保密') default null comment'性别',
age tinyint unsigned default null comment'年龄',
id char(18) default null comment'身份证号');
enum类型单选create table test1(sex enum('男','女','保密')); 只能插入列举出来的内容或者用数字替换 男=1女=2,加了not null属性默认为第一个值
set类型多选create table test2(hobby set('足球','篮球','羽毛球')); insert into test2 values('足球,篮球');set选项的最大个数为64
二进制类型
binary(M)建表时长度就指定了,不足最大长度空间用”\0”补全,varbinary长度是可变的
constraint 设置主键别名为pk_name
create table class(id int,constraint pk_name primary key(id));
多个属性组合成主键
create table class2(id int,_name char(4),constraint pk_name primary key(id,_name));
外键约束foreign key(字段1)references 表2(字段2)
create table class3(m_id int,foreign key(m_id)references class(id));
auto_increment 自动属性必须设置为键
default默认值,不设置时默认为空
alter table 表名 add constraint 约束名 primary key(列名);
删除主键
alter table 表名 drop primary key;
外键或唯一键
alter table 表名 drop index(约束名);
alter table 表名 modify 列名 类定义;
普通索引,不附加任何键,在表后加index 索引名(列名 长度 asc或desc),索引名和长度可加可不加,asc为升序desc为降序
create table 表名(id int,m_name varchar(64),telephone char(11),index (id asc));
追加普通索引,索引名不能省略长度可以省略,长度必须是字符串类型才能使用
create index 索引名 on 表名(列名 长度 排序方法);
alter table 表名 add index 索引名(列名 长度 排序方法);
create table 表名(id int,m_name varchar(64),unique index (列名 asc));
追加唯一索引,在index前加上unique,规则和追加普通索引一样
create unique index 索引名 on 表名(列名 长度 排序方法);
alter table 表名 add unique index 索引名(列名 长度 排序方法);
全文索引,主要基于char、varchar和text的字段上
create table 表名(test varchar(164),fulltext index 索引名(test 长度));
select*from 表名 where match(列名) against(查询字段);
追加全文索引
create fulltext index 索引名 on 表名(列名);
alter table 表名 add fulltext index 索引名 (列名);
删除索引
drop index 索引名 on 表名;
多列索引
create table 表名(id int,m_name varchar(64),index 索引名(列名1,列名2));
invisible隐藏,visible取消隐藏,用来验证索引的必要性
alter table 表名 alter index 索引名 invisible;
alter table 表名 alter index 索引名 visible;
插入多条记录
insert into 表名 values(数据),(数据),(数据)……;
更新数据,替换所有数据则不需要加where
update 表名 set id=10,m_name='小十' where id=8;
删除数据记录,删除所有记录则不需要加where
delete from 表名 where 条件;
md5();加密函数,括号里面填写需要加密的字符,验证时也加上md5()即可
distinct去重
select distinct 列名 from 表名;
in查询,查询是否有集合内的数据,not in 反向查询非集合中的数据
select*from 表名 where 列名 in(值1,值2);
区间between and 查询,not between and不在区间内查询
select*from 表名 where 列名 between 小值 and 大值;
like查询,模糊查找 _单个字符,%任意长度字符
select*from 表名 where 列 like '%王';
查询列中既有王又有张的记录
select*from 表名 where 列 like '%王%'and 列 like’%张%’;
对查询结果排序,空值视为最小的数
select*from 表名 where 条件 order by 列名 排序方式;
依据列进行分组
select count(列) from 表 group by 列;
统计分组,显示每个分组中的所有字段
select count(列),group_concat(需要统计的列) from 表 group by 列;
联合查询,内连接
select*from 表1 inner join 表2 on 表一.字段=表二.字段;
as取别名,表名 as 别名
select*from stdu as s inner join class as c on c.id=s.class_id;
左外连接,如果左表某行在右表中没有匹配行,则在显示结果中游标右表显示行所有值为空,右连接把left改成right就行了
select*from 左表 left join 右表 on 左表.列=右表.列;
select*from class union select id,class_id,m_name from stdu;
select*from class where id=(select class_id from stdu where m_name='小李');
嵌套,当子语句可以查询到多条记录时可以使用in查询
select*from stdu where class_id in (select id from class where teacher!='王老师');
exists判断子查询记录是否存在,存在则返回ture,不存在则返回false
select*from stdu where class_id=102 and exists (select*from class where id=102);
any表示满足子查询中的任何一个记录,可在any前加上运算符
select*from stdu where class_id >any(select id from class);
all表示满足子查询中所有记录,可在all前加上运算符
select*from stdu where class_id =all(select id from class);
create view 视图名 as 查询语句;
desc 视图名;
drop view 视图名;
create trigger 触发器名称 after insert on stdu1 for each row update class1 set count=count+1 where class1.id=new.class_id;
create trigger 触发器名称 after delete on stdu1 for each row update class1 set count=count-1 where class1.id=old.class_id;
drop trigger 触发器名称;
show triggers;
DELIMITER $$
create procedure proc_detele_stu (in sid int)
begin
declare cid int;
select class_id into cid from stdu1 where id=sid;
delete from grade where id=sid;
delete from stdu1 where id=sid;
update class set count=count-1 where id=cid;
end;
$$
DELIMITER ;
call proc_detele_stu(2);
定义变量,多个变量用逗号隔开
declare 变量名1 类型 default 默认值
给变量赋值,多个变量赋值则在值后面用逗号隔开
set 变量名=值;
删除存储过程
drop procedure proc_detele_stu;
DELIMITER $$
create procedure quer_student (in sid int,out cname varchar(128),out cid int)
begin
declare tmp_name varchar(128);
declare tmp_cid int;
declare cur_student cursor for select stdu_name,class_id from stdu1;
open cur_student;
fetch cur_student into tmp_name,tmp_cid;
select tmp_name,tmp_cid;
close cur_student;
set cname=tmp_name,cid=tmp_cid;
end;
$$
DELIMITER ;
if 条件 then 需要执行的语句 ;elseif 条件 then需要执行的语句; else 需要执行的语句 ;end if;
if sid<10 then set count1=sid+1;
elseif sid>10 then set count1=sid+2;
else set count1=0;
end if;
case语句,就是c++的switch
case sid
when sid=1 then set count1=sid;
when sid=2 then set count1=sid;
when sid=3 then set count1=sid;
else set count1=0;
end case;
loop语句,就是c++的循环语句,语法 开始标志:loop 循环语句 end loop 结束标志;
Add_num:loop
set sid=sid+1;
if sid=20 then set count1=sid;leave Add_num;end if;
end loop add_num;
leave语句,leave 结束标志;结束循环
leave Add_num;
iterate语句,跳出本次循环
iterate Add_num;
repeat语句,相当于do while, 开始标志:repeat 语句; until 结束条件 end repeat 结束标志;
Add_num:repeat
set sid=sid+1, count1=sid;
until sid>100
end repeat add_num;
while 语句,开始标志:while 开始条件 do 语句; end while 结束标志;
Add_num:while sid<100 do
set sid=sid+1, count1=sid;
end while add_num;
select last_insert_id() as temp;
use school;
create table userinfo(_no int unsigned auto_increment primary key ,
_name varchar(64) default null comment'姓名',
telephone char(11) default null comment'电话',
home varchar(128) default null comment'家庭地址',
prpr text default null comment'个人简历',
sex enum('男','女','保密') default null comment'性别',
age tinyint unsigned default null comment'年龄',
id char(18) default null comment'身份证号');
insert into userinfo values(1,"老王","182****9567","江西省某某市某某县","性格开朗活泼","男",18,"360781************");
select*from userinfo;
create table test1(sex enum('男','女','保密'));
insert into test1 values('男');
select*from test1;
create table test2(hobby set('足球','篮球','羽毛球'));
insert into test2 values('足球,篮球');
select*from test2;
create table bin_example(e_bin binary(5),e_varbin varbinary(5));
insert into bin_example values('ab','ab');
select*from bin_example;
drop table class;
create table class(id int,constraint pk_name primary key(id));
create table class2(id int,_name char(4),constraint pk_name primary key(id,_name));
create table class3(m_id int,foreign key(m_id)references class(id));
create table class4(test int);
alter table class4 add constraint pk_id primary key(test);
show keys from class4;
alter table class4 drop primary key;
alter table class4 modify test char(4) default NULL;
use school;
drop table class5;
create table class5(id int,m_name varchar(64),telephone char(11),index (id asc));
insert into class5 values(5,"老五","1*********");
insert into class5 values(4,"老四","1*********");
insert into class5 values(6,"老六","1*********");
insert into class5 values(1,"老大","1*********");
select*from class5 where id>0;
create index index_id on class5(id asc);
alter table class5 add index index_id (id asc);
create table class6(id int,m_name varchar(64),unique index id_index(id asc));
drop table class6;
insert into class6 values(1,"一");
insert into class6 values(2,"二");
insert into class6 values(3,"三");
select*from class6;
create table class7(test varchar(164),fulltext index fu_index(test));
insert into class7 values("我是你爸爸,最伟大");
select*from class7 where match(test) against("我是你爸爸");
drop index fu_index on class7;
create fulltext index fu_index on class7(test);
alter table class7 add fulltext index fu_index (test);
create table class8(id int,m_name varchar(64),index (id,m_name));
insert into class8 values(5,'老王');
insert into class8 values(4,'老张');
select *from class8 where id>0;
alter table class7 alter index fu_index invisible;
alter table class7 alter index fu_index visible;
insert into class8 values(6,'老九'),(7,"老七"),(8,'老八');
update class8 set id=10,m_name='小十' where id=8;
delete from class8 where id=10;
select distinct m_name from class8;
select*from class8 where m_name in('老王','老张');
select*from class8 where id between 3 and 7;
select*from class8 where m_name like '%王';
select*from class8 where id>0 order by id desc;
create table class9(id int,m_name varchar(64),class varchar(10));
insert into class9 values(1,'张三',1),(2,'李四',2),(3,'熊大',1),(4,'熊二',2);
select count(class),class from class9 group by class;
select count(class),group_concat(m_name) from class9 group by class;
use school;
create table class(id int not null auto_increment unique,m_name varchar(64)default null,teacher varchar(64)default null);
create table stdu(id int not null auto_increment unique,m_name varchar(64) default null,class_id int default null,sex enum("男","女")default null);
insert into class values(101,'一班','马老师'),(102,'二班','王老师'),(103,'三班','刘老师');
insert into stdu values(1,'小花',101,'女'),(2,'小李',102,'男'),(3,'小美',101,'女'),(4,'小明',102,'男'),(5,'小红',103,'女');
select*from stdu inner join class on class.id=stdu.class_id;
select*from stdu as s inner join class as c on c.id=s.class_id;
select*from stdu left join class on stdu.class_id=class.id;
select*from class right join stdu on stdu.class_id=class.id;
select*from class union select id,class_id,m_name from stdu;
select*from class where id=(select class_id from stdu where m_name='小李');
select*from stdu where class_id in (select id from class where teacher!='王老师');
select*from stdu where class_id=102 and exists (select*from class where id=102);
select*from stdu where class_id =any(select id from class);
select*from stdu where class_id =all(select id from class);
create view te as select teacher from class;
desc te;
select*from te;
drop view te;
create table class1 (id int not null auto_increment unique,class_name varchar(64)default null,teacher varchar(64)default null,count int default 0);
insert into class1 values(101,'一班','马老师',0),(102,'二班','王老师',0),(103,'三班','刘老师',0);
create table stdu1 (id int not null auto_increment unique,stdu_name varchar(64)default null,class_id int default 0,sex enum('男','女')default null);
create trigger tri_inser_class after insert on stdu1 for each row update class1 set count=count+1 where class1.id=new.class_id;
insert into stdu1 values(1,'小王',102,'男'),(2,'小美',101,'女');
select*from class1;
create trigger tri_delete_class after delete on stdu1 for each row update class1 set count=count-1 where class1.id=old.class_id;
delete from stdu1 where id=1;
drop trigger tri_inser_class;
show triggers;
DELIMITER $$
create procedure proc_detele_stu (in sid int)
begin
declare cid int;
select class_id into cid from stdu1 where id=sid;
delete from stdu1 where id=sid;
update class1 set count=count-1 where id=cid;
end;
$$
DELIMITER ;
call proc_detele_stu(2);
drop procedure proc_detele_stu;
DELIMITER $$
create procedure quer_student (in sid int,out cname varchar(128),out cid int)
begin
declare tmp_name varchar(128);
declare tmp_cid int;
declare cur_student cursor for select stdu_name,class_id from stdu1 where id=sid;
open cur_student;
fetch cur_student into tmp_name,tmp_cid;
select tmp_name,tmp_cid;
close cur_student;
set cname=tmp_name,cid=tmp_cid;
end;
$$
DELIMITER ;
drop procedure quer_student;
call quer_student(1,@a,@b);
select @a,@b;
use school;
DELIMITER $$
create procedure pro_test (in sid int,out count1 int)
begin
if sid<10 then set count1=sid+1;
elseif sid>10 then set count1=sid+2;
else set count1=0;
end if;
end;
$$
DELIMITER ;
drop procedure pro_test;
call pro_test(11,@c);
select @c;
DELIMITER $$
create procedure pro_test1 (in sid int,out count1 int)
begin
case sid
when sid=1 then set count1=sid;
when sid=2 then set count1=sid;
when sid=3 then set count1=sid;
else set count1=0;
end case;
end;
$$
DELIMITER ;
drop procedure pro_test1;
call pro_test1(1,@c);
select @c;
DELIMITER $$
create procedure pro_test2 (in sid int,out count1 int)
begin
Add_num:loop
set sid=sid+1;
if sid=20 then set count1=sid;leave Add_num;end if;
end loop add_num;
end;
$$
DELIMITER ;
drop procedure pro_test2;
call pro_test2(1,@c);
select @c;
DELIMITER $$
create procedure pro_test3 (in sid int,out count1 int)
begin
Add_num:repeat
set sid=sid+1,count1=sid;
until sid>100
end repeat add_num;
end;
$$
DELIMITER ;
drop procedure pro_test3;
call pro_test3(1,@c);
select @c;
DELIMITER $$
create procedure pro_test4 (in sid int,out count1 int)
begin
Add_num:while sid<100 do
set sid=sid+1,count1=sid;
end while add_num;
end;
$$
DELIMITER ;
drop procedure pro_test4;
call pro_test4(1,@c);
select @c;
DELIMITER $$
create procedure quer_student1 (out cname varchar(128),out cid int)
begin
declare tmp_name varchar(128);
declare tmp_cid int;
declare done int default 0;
declare cur_student cursor for select stdu_name,class_id from stdu1;
declare continue handler for not found set done=1;
open cur_student;
READ_Stdu:loop
fetch cur_student into tmp_name,tmp_cid;
if done=1 then leave READ_Stdu;
end if ;
select tmp_name,tmp_cid;
end loop READ_Stdu;
close cur_student;
set cname=tmp_name,cid=tmp_cid;
end;
$$
DELIMITER ;
drop procedure quer_student1;
call quer_student1(@a,@b);
select @a,@b;
select last_insert_id();
show procedure status like 'quer%';
show create procedure quer_student1;
show engines;