mysql快速上手

目录:
DDL

查看所有数据库
创建数据库
使用一个数据库
删除数据库

查看所有表
创建表
查看表字段
插入一条记录
查看表记录

复制表

添加一个表字段
添加2个表字段

修改表字段类型
删除表字段

修改表名
修改字段名及字段类型
删除表
快速删除全部表记录(truncate截去)

增加not null约束
增加default
增加default,not null约束
取消not null约束(实际上直接重定义字段约束)
取消not null,并指定默认值

创建列级unique(唯一性)约束
创建表级unique约束(指定约束名)
创建列级unique约束(不指定约束名)
unique组合约束

添加unique约束
修改(添加)列级unique约束
修改(添加)表级unique约束(此方法对列级同样适用)
删除unique约束

定义列级主键
定义表级主键
定义组合主键
删除主键
修改(添加)主键(表级)
修改(添加)主键(列级)
修改(添加)自增主键(列级)

创建字段级外键约束(不会生效)
创建表级外键约束(默认名字tablename_ibfk_n)
创建表级外键约束(指定约束名)
创建表级组合外键约束(指定约束名)

删除外键约束
修改(添加)外键约束
自关联

主表删除,从表级联
主表删除,从表设空


check约束(无效) 

创建索引
删除索引

创建视图(or replace相当于if exists)
删除视图
创建视图(不允许修改视图数据)



DML

insert
tb(id,name)表添加数据(null,'sky'),全字段方式
tb(id,name)表添加数据,指定字段方式
将tb2(id2,name2)表中的name2插入到tb(id,name)表的name上(适用子查询)
tb(id,name)表一次插入多组值

update
修改tb(id,name)表所有name为‘sky’
修改tb(id,name)表name=‘sky’的值为'shi'

delete
删除所有记录(效率低于truncate)
删除tb(id,name)表name=‘sky’的数据

select
查询所有字段
查询指定字段
查询name='shi'的记录
查询name='shi',并连接字符串“**”
查询age<3
查询age+5(列名可以当作变量处理)
查询age+5,并重命名(可适用as,或者直接空格隔开)
查询age+5,并重命名(命名中包括空格)
查询name='shi'字段,去重复
查询1<=age<=3(between and)
查询age=1,3(in)
查询id和age都是1
查询name以s开头的记录
查询name以s开头,i结尾长度为3的记录
查询name以_开头的记录
查询name不以_开头的记录
查询age为null的记录
查询id=4,age=null的记录
查询id=4或age=null的记录
查询所有,按id排序
查询所有,按id逆序
查询所有,按id升序,name逆序

分组和组函数
count
sum
max
min
avg
group by组合
having(过滤组,where用来过滤行)



SQL92
多表连接查询
自关联(需要对一个表,建两个别名)


SQL99
cross join交叉连接(相当于笛卡尔乘积,无条件判断)
natural join自然连接(同字段会自动比较,默认为自然连接)
using(fieldname)指定比较列
on指定比较条件
左右全(外)连接,left [outer] join,right join,full join

子查询
把子查询当数据表
返回单值子查询当标量使用
返回多值子查询in,any,all
返回多行多列子查询


查询姓氏(name第一个字符)

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

DDL

查看所有数据库
创建数据库
使用一个数据库
删除数据库
show databases;
create database db;
use db;
drop database [if exists] db;

查看所有表
创建表
查看表字段
插入一条记录
查看表记录
show tables;
create table tb
(
id int,
name varchar(8)
);
desc tb;
insert into tb values(1,'shizhijie');
select * from tb;

复制表
添加一个表字段
添加2个表字段

修改表字段类型
删除表字段
create table tb1
as
select * from tb;

alter table tb
add user int;

alter table tb
add
(
first int,
last int
);

alter table tb
modify first varchar(8);

alter table tb
drop last;

修改表名
修改字段名及字段类型
删除表
快速删除全部表记录(truncate截去)
alter table tb
rename [to] tb0;

alter table tb
change first last decimal;
drop table if exists tb1;
truncate table tb1;

增加not null约束
增加default
增加default,not null约束
取消not null约束(实际上直接重定义字段约束)
取消not null,并指定默认值
alter table tb
modify name varchar(8) not null;

alter table tb
modify name varchar(9) default 'aaa';

alter table tb
modify name varchar(9) default 'bbb' not null;

alter table tb
modify name varchar(9) null;

alter table tb
modify name varchar(9) default 'ccc' null;

创建列级unique(唯一性)约束
创建表级unique约束(指定约束名)
创建表级unique约束(不指定约束名)
unique组合约束
create table unique_test
(
id int not null,
test_name varchar(9) unique
)

create table unique_test2
(
id int not null,
test_name varchar(9),
constraint test2_uk unique(test_name)
);

create table unique_test3
(
id int not null,
test_name varchar(9),
unique(test_name)
);


create table unique_test4
(
id int not null,
test_name varchar(9),
constraint test4_uk unique(id,test_name)
);

添加unique约束
修改(添加)列级unique约束
修改(添加)表级unique约束(此方法对列级同样适用)
删除unique约束
alter table tb
modify id int unique;

alter table tb
add [constraint id] unique(id,name);

alter table tb
drop index id;

定义列级主键
定义表级主键
定义组合主键
删除主键
修改(添加)主键(表级)
修改(添加)主键(列级)
修改(添加)自增主键(列级)
create table primary_test
(
id int primary key,
name varchar(9)
);

create table primary_test2
(
id int,
name varchar(9),
constraint test2_pk primary key(id)
);

create table primary_test3
(
id int,
name varchar(9),
constraint test3_pk primary key(id,name)
);

alter table tb
drop primary key;

alter table tb
add primary key(id);

alter table tb
modify id int primary key;

alter table tb
modify id int auto_increment primary key;


创建字段级外键约束(不会生效)
创建表级外键约束(默认名字tablename_ibfk_n)
创建表级外键约束(指定约束名)
创建表级组合外键约束(指定约束名)
create table tb1
(
id int auto_increment,
primary key(id)
);
create table tb2
(
sid int auto_increment primary key,
tid int references tb1(id)
);

create table tb1
(
id int auto_increment,
primary key(id)
);
create table tb2
(
sid int auto_increment primary key,
tid int,
foreign key(tid) references tb1(id)
);

create table tb2
(
sid int auto_increment primary key,
tid int,
constraint s_t_fk foreign key(tid) references tb1(id)
);

create table tb1
(
id int auto_increment,
name varchar(9),
primary key(id,name)
);
create table tb2
(
sid int auto_increment primary key,
tid int,
tname varchar(9),
constraint s_t_fk foreign key(tid,tname ) references tb1(id,name)
);

删除外键约束
修改(添加)外键约束
自关联
alter table tb2
drop foreign key s_t_fk ;

alter table tb2
add foreign key(tid,tname ) references tb1(id,name);

create table foreign_test
(
id int auto_increment primary key,
name varchar(9),
fid int,
foreign key(fid) references foreign_test(id)
);


主表删除,从表级联
主表删除,从表设空
create table tb1
(
id int auto_increment,
primary key(id)
);
create table tb2
(
sid int auto_increment primary key,
tid int,
foreign key(tid) references tb1(id) on delete cascade
);

delete from tb1 where id=1;

alter table tb2
drop foreign key tb2_ibfk_1;

alter table tb2
add foreign key(tid) references tb1(id) on delete set null;

delete from tb1 where id=1;

check约束(无效) 
create table check_test
(
id int auto_increment primary key,
salary int,
check(salary > 100)
);

创建索引
删除索引
create table tb
(
id int,
name varchar(9)
);

create index tb_name_index
on tb(name);

drop index tb_name_index
on tb;

创建视图(or replace相当于if exists)
删除视图
创建视图(不允许修改视图数据)
create or replace view view_test
as
select * from tb;

drop view view_test;

create or replace view view_test
as
select * from tb
with check option;




DML

insert
tb(id,name)表添加数据(null,'sky'),全字段方式
tb(id,name)表添加数据,指定字段方式
将tb2(id2,name2)表中的name2插入到tb(id,name)表的name上(适用子查询)
tb(id,name)表一次插入多组值
create table tb
(
id int,
name varchar(9)
);
insert into tb(name) values('sky');
insert into tb values(null,'sky');

create table tb2
(
id2 int auto_increment primary key,
name2 varchar(9)
);
insert into tb(name)
select name2 from tb2;

insert into tb
values(null,'aaa'),
(null,'bbb');


update
修改tb(id,name)表所有name为‘sky’
修改tb(id,name)表name=‘sky’的值为'shi'
update tb
set name='sky';

update tb
set name='shi'
where name='sky';


delete
删除所有记录(效率低于truncate)
删除tb(id,name)表name=‘sky’的数据
delete from tb;

delete from tb
where name='sky';



select
查询所有字段
查询指定字段
查询name='shi'的记录
查询name='shi',并连接字符串“**”
查询age<3
查询age+5(列名可以当作变量处理)
查询age+5,并重命名(可适用as,或者直接空格隔开)
查询age+5,并重命名(命名中包括空格)
查询name='shi'字段,去重复
查询1<=age<=3(between and)
查询age=1,3(in)
查询id和age都是1
查询name以s开头的记录
查询name以s开头,i结尾长度为3的记录
查询name以_开头的记录
查询name不以_开头的记录
查询age为null的记录
查询id=4,age=null的记录
查询id=4或age=null的记录
查询所有,按id排序
查询所有,按id逆序
查询所有,按id升序,name逆序
select * from tb;

select id,name from tb;

select * from tb
where name='shi';

select concat(name,'**') from tb
where name='shi';

select * from tb
where age<3;

select id+5 from tb;

select id+5 as id2 from tb;

select id+5 as "id 2" from tb;

select distinct name from tb
where name='shi';

select * from tb
where age between 1 and 3;

select * from tb
where age in (1,3);

select * from tb
where 1 in (id,age);

select * from tb
where name like 's%';

select * from tb
where name like 's_i';

select * from tb
where name like '\_%';

select * from tb
where not name like '\_%';

select * from tb
where age is null;

select * from tb
where id=4 and age is null;

select * from tb
where id=4 or age is null;

select * from tb
order by id;

select * from tb
order by id desc;

select * from tb
order by id,name desc;



分组和组函数
count
sum
max
min
avg
group by组合
having(过滤组,where用来过滤行)
select count(*) from tb;

select count(distinct age) from tb;

select sum(id) from tb;

select sum(20) from tb;
select sum(distinct 20) from tb;

select max(id) from tb;
select min(id) from tb;

select avg(ifnull(age,0)) from tb;

select count(*) from tb
group by name,age;

select count(*) from tb
group by age;


select count(*) from tb
group by age
having count(*)>=2;


SQL92
多表连接查询
自关联(需要对一个表,建两个别名)
select s.name,t.name 
from student s,teacher t
where s.teacher_id=t.id;

select s.name,t.name 
from student s,student t
where s.teacher_id=t.id;

select s.name 学生,t.name 老师
from student s,teacher t
where s.teacher_id=t.id;


SQL99
cross join交叉连接(相当于笛卡尔乘积,无条件判断)
natural join自然连接(同字段会自动比较,默认为自然连接)
using(fieldname)指定比较列
on指定比较条件
左右全(外)连接,left [outer] join,right join,full join
select s.name 学生,t.name 老师
from student s join teacher t
on s.teacher_id=t.id;

select s.name 学生,t.name 老师
from student s left join teacher t
on s.teacher_id=t.id;

select s.name 学生,t.name 老师
from student s right join teacher t
on s.teacher_id=t.id;

select s.name 学生,t.name 老师
from student s full join teacher t
on s.teacher_id=t.id;


子查询
把子查询当数据表
返回单值子查询当标量使用
返回多值子查询in,any,all
返回多行多列子查询
select * from
(select * from teacher) t
where t.id=1;

select * from teacher
where id>
(select id from teacher
where name='石志杰');

select * from teacher
where id in
(select id from teacher
where age>20);

select * from teacher
where id<all
(select id from teacher
where age>20);

select * from teacher
where id<any
(select id from teacher
where age>20);

select * from teacher
where (id,name)=any
(select id,name from teacher);



查询姓氏(name第一个字符)
select left(name,1) from teacher;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值