数据库基本的增删改查语句

增删改查:


DDL,数据定义语言


1.创建数据库,表
1.create database 数据库名

2.create table 数据表名(
--结构
);

3.反引号 ``:作用:使用mysql标识符命名时,使用反引号包裹,执行sql语句会通过。


2.查看数据库,表列表

show databases; show tables;


3.查看数据库,表语句

show create database 数据库名
show create table 数据表名


4.删除数据库,表
drop database 数据库
drop database 数据库表


5.选择数据库
use 数据库名

1.like
作用:对展示的结果进行过滤。
mysql中的字符串,使用单引号进行包裹!
(也是支持双引号,但是双引号有特别的含义,因此不建议使用)


通配符:
匹配任意字符的任意数量: %
匹配任意字符的一次出现: _

如果要匹配% 和 _ ;使用反斜杠"\" 转义即可。

show databases like '数据库名\_';


3.特别容错语法:
1.if not exists
2.if exists

一个要求:
认为库名,表名,都区分大小写的。


4.create table 库名.表名,没练习过

5.\G

6.改表名,rename table 原表名 to 新表名

7.查看表结构:desc 数据表名

8.first 关键字 第一

9.after 关键字 后面


修改
alter
1.table:
1.修改表选项,比如字符集...........
2.修改表结构:
1.增加字段:
alter table 表名 add column(字段) 字段定义 (比如,id值)

2.在表里增加一个字段
alter table 表名 add column(字段) 字段定义 (比如,id值)after column(字段)

alter table 表名 add column(字段) 字段定义 (比如,id值)first column(字段)

3.删除表中的字段
alter table 数据表名 drop column column_name(字段名)

4.修改表的字段
alter table 数据表名 ”modify“ column 字段名 新定义
没练习过

5.字段改名
alter table 数据表名 change column 原字段名 新字段名 新字段定义
没练习过

2.主键:
管理主键:
alter table 表名 primary key(主键字段)

删除
alter table 表名 drop primary key

//例子:alter table preperty_unique_3 drop index sn;

添加:
alter table 表名 add primary key(指定的字段)

管理类似于自动增长其他属性时:

alter table 表名 modify column

3.unique key:唯一:
删除;
alter table 表名 drop unique key index 索引名

增加:
alter table 表名 add unique key 索引 字段列表

%%4.外键:


除:
alter table 表名 drop foreign key 外键语法

增加:
alter table 表名{子表} add constraint 约束名称 foreign key 外键索引名 (外键字段名) references 关联表名{父表} (关联字段) [操作]

置空:
alter table null_fk_student add constraint set_null foreign key(class_id) references null_fk_class (class_id) on delete set null on update cascade;



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
unsigned:无符号。
zerofill:0填充


DML,数据的管理语言


增加
insert into
插入数据表数据
1.insert into 表名 (字段列表) values (值列表)

2.支持自动对冲突的主键和唯一约束进行判断

insert into 表名 (字段列表) values (值列表) on duplicate key update 字段=值............

insert into duplicate key update:

3.插入数据,可能出现select子句

insert into 表名
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------


修改
update

修改字段数据
update 表名 set 字段 = 新值(可以多个) where 条件

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

删除

delete
delete from 表名 where 条件,目的:删除表中的数据

%%delete支持order by ,limit 来限制删除数据记录。需要练习


truncate:
清空表,truncate是一次性删除,删除后重建表
相当于 drop table ,create table。

总结:
delete 和drop删除数据表区别:
delete只能删除表数据,不能删除表结构,

而drop能够把整个表的结构和数据一次性清空。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

查询
select
查询
检索
选择

基本查询语句结构:
select [字段表达式列表] [from子句] [where 子句] [group by 子句] [having 子句] [order by子句] [limit 子句]

1.from子句:
查询的来源,就是表

2.where子句:
条件表达式,省略where,相当于永远为true

找到每条记录,并依次执行条件表达式,根据条件结果返回数据!

3.group by 子句
分组查询:

4.having
子句,条件子句

5.order by
排序子句
1.asc
2.desc

6.limit

7.sub-query(子查询):


1.标量子查询:
返回一个数据类型

select * from 表名 where 字段1=(select 字段1指定的数据 from 表名)

2.列子查询:
返回一个列

例子:
select * from student1 where sex="man" and
classId in (select classId from student1 where sex="gril" group by classId);

列子查询:
使用的查询条件:
1.in
2.not in
3.!= some
4.!=all
5.some()
6.all()


3.行子查询
返回一个行
select * from student1 where(height score)=(select max(height),max(score) from student1);


3.1exists型:子查询
exists:
如果子查询存在返回数据,则exists返回true,否则返回flase

not exists:
和exists相反

exists出现在where条件内,where exists.............



4.表子查询
返回一个二维表

连接查询:
join:
语法:
from 表名1 join 表名2 on 连接条件

select stuName,sex,classId className from student1

join selectClass on student1.classId=selectClass.id;

连接条件:
join:连接
on:连接条件

过程:
先执行 from 后 执行join


1.内连接
inner join:
记录真实记录的连接。mysql 默认的连接时 inner join

select stuName,sex,classId className from student1

inner join selectClass on student1.classId=selectClass.id;

细节:
on 可以省略,省略后内连接永远为true,但是两条记录的结构必须一致


2.外连接
概念:
连接的记录,可能是一方不存在的!(两条记录中,可能某条不存在)

1.左外连接
left join:

select 左表和右表的字段数据 from 左表 left join 右表 on 连接条件()

2.右外连接
right join:

select 左表和右表的字段数据 from 左表 right join 右表 on 连接条件()

3.交叉连接
cross join
结果与 内连接一致!

有时,在获得笛卡尔积时,显式的使用 交叉连接!

交叉连接 相当于 是 没有条件的内连接


4.自然连接
1.内连接
natural join:
自动判断连接条件,帮我程序员完成连接



典型的条件就是:表中的同名字段

2.外连接
outer join
自然左外:

natural left join

自然右外:
natural right join


总结:
自然连接,就是自动连接


连接条件:

1.on,后面使用一个连接条件表达式!

https://www.360docs.net/doc/a211008240.html,ing(连接字段),要求使用同名字段进行连接!

特别地方:
会对字段列表做一次整理!将连接字段作为一次显示!

union:l联合查询

将 多个查询的结果,并列到一个结果集合内!

select * from 表名 where 字段名=字段值
union
select * from 表名 where 字段名=字段值

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

相关文档
最新文档