【Java web 】MySQL详细知识点(DDL,DML,DQL,约束)

MySQL

DDL

注:以下以db1位例

1.操作 数据库

(1)查询

SHOW DATABASES;

(2)创建

CTEATE DATABASE db1;
CTEATE DATABASE IF NOT EXISTS db1;

(3)删除

DROP DATABASE db1;
DROP DATABASE IF EXISTS db1;

(4)使用数据库

SELECT DATABASE();
USE db1;

2.操作 表

(1)创建

# cmd
create table tb_user(
    -> id int,
    -> username varchar(20),
    -> password varchar(32)
    -> );

(2) 查询

show tb_user;
desc tb_user; #查看表的状态

(3)删除

drop table tb_user;
drop table if exists tb_user;

(4) 修改

修改表名

alter table usere rename to daqi;

添加列

alter table daqi add mysqlee int;

删除列

alter table daqi drop mysqlee;

修改数据类型

alter table daqi modify name varchar(40);

修改列名和数据类型

alter table daqi change name namedq int;

DML

操作表中的数据

1.添加数据

insert into daqi(id,name) values(1123242,"张三");

# 给所有列赋值时不用在表后面加()
insert into daqi values(1123,"李四","男","dkfjdk");
# 批量添加用","隔开values后面的()

2.修改数据

update daqi set sex = '女' where name = '张三';

3.删除数据

delete from daqi where name = '张三';

DQL

1.条件查询

select * from daqi where id between 1124 and 1128;

select * from daqi where address is null;

# like模糊查询(可以查姓氏),%为多个位,_为后一个位(如查“达”性,只能查到达三,达四)
select * from daqi where name like '达%';

2.排序查询

升序

select * from daqi order by id asc;
# 其中asc是默认值,可以省略

降序

select * from daqi order by id desc;

多级排序

# 当第一个排序规则一样时,按第二个规则排序
select * from daqi order by id asc,idelse asc;

3.分组查询

聚合函数

注:null不参与聚合函数运算

# 统计数量 注:不统计null的值
select count(id) from daqi;
# 统计行数,建议如下
select count(*) from daqi;

# 求和
select sum(id) from daqi;

# 求最小值
select min(id) from daqi;

# 求最大值
select max(id) from daqi;

# 求平均数
select avg(id) from daqi;

分组查询

# 其中where在分组前限定,having在分组后进行过滤
select sex, sum(id),count(*) from daqi where id > 100 group by sex having count(*) > 1;

4.分页查询

# limit 起始索引,查询条数
#起始索引 = (当前页码 - 1)* 查询条数(每页显示的条数)
select * from daqi limit 0, 3; 

select * from daqi limit 3, 3; 

约束

# auto_increment为自增
CREATE TABLE emp (
	id INT PRIMARY KEY auto_increment,
	ename VARCHAR ( 50 ) NOT NULL UNIQUE,
	joindate date NOT NULL,
	salary DOUBLE ( 7, 2 ) NOT NULL,
bonus DOUBLE ( 7, 2 ) DEFAULT 0 
)

外键添加

将两个表联系起来

创建表前

CREATE TABLE yuan_gong (
	id INT PRIMARY KEY,
	ename VARCHAR ( 50 ) NOT NULL,
	bu_men_id INT NOT NULL,
	CONSTRAINT fk_yuan_gong_bu_men FOREIGN KEY ( bu_men_id ) REFERENCES bu_men ( id )	
);

创建表后

ALTER TABLE yuan_gong ADD CONSTRAINT fk_yuan_gong_bu_men FOREIGN KEY ( bu_men_id ) REFERENCES bu_men ( id )

删除

ALTER TABLE yuan_gong drop foreign key fk_yuan_gong_bu_men;

数据库设计

表关系

一对一:关联,外键设为unique

一对多(多对一):如员工和部门

多对多:需要用到中间表

alter table tb_order_goods add constraint fk_order_id foreign key(order_id) references tb_order(id);

alter table tb_order_goods add constraint fk_goods_id foreign key(goods_id) references tb_goods(id);

多表查询

1.内连接(交集)

隐式内连接

select * from yuan_gong, bu_men where yuan_gong.bu_men_id = bu_men.id;

# 起别名 t1和t2
select t1.ename, t2.ename from yuan_gong t1, bu_men t2 where t1.bu_men_id = t2.id;

显式内连接

select * from yuan_gong join bu_men on yuan_gong.bu_men_id = bu_men.id;

外连接(左/右表 + 交集)

# 左连接
select * from yuan_gong left join bu_men on yuan_gong.bu_men_id = bu_men.id;

# 右连接
select * from yuan_gong right join bu_men on yuan_gong.bu_men_id = bu_men.id;

2.子查询(嵌套)

查询中嵌套查询

select * from yuan_gong where id > (select id from yuan_gong where ename = '张三');

你可能感兴趣的:(java,web,mysql,java,开发语言,intellij-idea,数据库)