数据库的增删改查

本文详细介绍了SQL中的数据定义语言(DDL)、数据操纵语言(DML)和数据查询语言(DQL)。涵盖了创建和修改表结构、插入和更新数据、基础及高级查询技巧,包括条件、模糊、排序和分组查询。还讨论了约束的概念,如主键和外键,并展示了如何进行多表查询以及事务处理。内容适合数据库初学者和进阶者学习。
摘要由CSDN通过智能技术生成

DDL

在这里插入图片描述

DDL操作表

在这里插入图片描述
在这里插入图片描述

DML

添加数据

在这里插入图片描述

修改数据

在这里插入图片描述

DQL查询

基础查询

在这里插入图片描述

drop table if exists stu;
select *from stu
create table stu(
id int,
name varchar(5),
age int,
sex varchar(5),
address varchar(10),
math double(5,2),
english double(5,2),
hire_date date
);
alter table stu add address varchar(10);
Insert INTO stu(id,name,age,sex,address,math,english,hire_date)
VALUES
(1,'马云',55,'男','杭州',66,78,'1995-09-01'),
(2,'马化腾',45,'女','深圳',98,87,'1998-09-01'),
(3,'马斯克',55,'男','香港',56,77,'2001-01-11'),
(4,'陆佰',20,'女','湖南',76,65,'1997-09-05'),
(5,'刘青',20,'男','湖北',86,NULL,'1998-09-01'),
(6,'刘德华',45,'男','香港',33,56,'1998-09-11'),
(7,'张学友',22,'女','香港',99,99,'1997-02-11'),
(8,'德玛西亚',18,'男','南京',67,76,'1994-09-02');
select name,age from stu;
select distinct address from stu;
select name ,math as 数学成绩,english as 英语成绩 from stu;

条件查询

在这里插入图片描述

模糊查询

在这里插入图片描述

drop table if exists stu;
select *from stu
create table stu(
id int,
name varchar(5),
age int,
sex varchar(5),
address varchar(10),
math double(5,2),
english double(5,2),
hire_date date
);
alter table stu add address varchar(10);
Insert INTO stu(id,name,age,sex,address,math,english,hire_date)
VALUES
(1,'马云',55,'男','杭州',66,78,'1995-09-01'),
(2,'马化腾',45,'女','深圳',98,87,'1998-09-01'),
(3,'马斯克',55,'男','香港',56,77,'2001-01-11'),
(4,'陆佰',20,'女','湖南',76,65,'1997-09-05'),
(5,'刘青',20,'男','湖北',86,NULL,'1998-09-01'),
(6,'刘德华',45,'男','香港',33,56,'1998-09-11'),
(7,'张学友',22,'女','香港',99,99,'1997-02-11'),
(8,'德玛西亚',18,'男','南京',67,76,'1994-09-02');
select name,age from stu;
select distinct address from stu;
select name ,math as 数学成绩,english as 英语成绩 from stu;
select *from stu where age>=20;
select *from stu where age>20;
select* from stu where age>20&&age<30;
select *from stu where hire_date BETWEEN '1998-01-01' and '2022-02-01';
select*from stu where age in(18,19,20);
select*from stu where english is NULL;
select*from stu where english is not null;
select*from stu where name like '马%';
select*from stu where name like'_斯%';
select*from stu where name like'%刘%';

排序查询

在这里插入图片描述

分组查询

在这里插入图片描述

-- 查询学生信息按升序排列
select *from stu order by age asc;
-- 查询学生信息按数学成绩降序排列.
select*from stu order by math desc,english desc;
select count(id) from stu;
select count(english)from stu;
select min(math) from stu;
select sum(math) from stu;
select avg(math) from stu;
select min(english) from stu;

在这里插入图片描述

-- 查询学生信息按升序排列
select sex,avg(math)from stu GROUP BY sex;
select sex,avg(math),count(*) from stu group by sex;
select sex,avg(math),count(*) from stu where math>70 group by sex;
select sex,avg(math),count(*) from stu where math>70 group by sex having count(*)>1;

分页查询

在这里插入图片描述

约束

约束的概念和分类

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

drop table if exists emp;
create table emp
(
    id int primary key auto_increment,
    ename varchar(50)charset utf8 null null unique,
    joindate date not null,
    salary double(7,2)not null,
    bonus double(7,2)default 0
);
-- 主键非空且唯一
insert into emp(id, ename, joindate, salary,bonus) VALUES
(1,'张三','1999-11-11',8000,5000);
insert into emp(id, ename, joindate, salary,bonus) VALUES
    (null,'张三','1999-11-11',8000,5000);
insert into emp(id, ename, joindate, salary,bonus) VALUES
    (1,'张三','1999-11-11',8000,5000);
insert into emp(id, ename, joindate, salary,bonus) VALUES
    (2,'李四','1999-11-11',8000,5000);
insert into emp(id, ename, joindate, salary) VALUES
    (3,'王五','1999-11-11',8000);
-- 演示自动增长
insert into emp(ename, joindate, salary,bonus) VALUES
    ('赵六','1999-11-11',8000,5000);
insert into emp(id, ename, joindate, salary,bonus) VALUES
    (null,'赵六2','1999-11-11',8000,5000);
alter table emp modify id int not null;
insert into emp(id, ename, joindate, salary,bonus) VALUES
    (null,'赵六2','1999-11-11',8000,5000);
alter table emp modify  id int ;

select *from emp;

外键约束

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

数据库设计

在这里插入图片描述

多对多

在这里插入图片描述

数据库多表查询

在这里插入图片描述

内连接

在这里插入图片描述

select *from dept,ewp where dept.id=ewp.dep_id;
select*from ewp join dept on ewp.dep_id=dept.id;

外连接

在这里插入图片描述

select*from ewp left join dept on ewp.dep_id=dept.id;
select*from ewp right join dept on ewp.dep_id=dept.id;

子查询

在这里插入图片描述

select *from stu where math>(select math from stu where name='马云');

在这里插入图片描述

select *from (select *from ewp where age>18) t1,dept where t1.dep_id=dept.id;

事物

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值