mysql中增删改查的详解 例题 sql 语句

一、基本命令

1.启动服务
以管理员身份运行cmd
net start mysql57

2.停止服务:net stop mysql57

3.连接数据 mysql -u root -p

4.退出登录(断开连接)quit或者 exit

5.查看版本 select version();(注意分号)

6.显示当前时间 select now();

7.远程连接 mysql -h ip地址 -u 用户名 -p

二、数据库操作

1.创建数据库 create database 库名 charset=utf-8;

2.删除数据库 drop database 库名;

3.切换数据库 use 库名;

4.查看当前使用的数据库 select database();

三、表操作

1.查看当前数据库中所有表 show tables;

2.创建表 create table 表名(列及类型);
create table student(id int auto_increment primary key, name varchar(20) not null,age int not null,gender bit default 1,address varchar(20),isDelete bit default 0);

3.删除表 drop table 表名;

4.查看表结构 desc 表名;

5.查看建表语句 show create table 表名

6.重命名表名 rename table car to newCar;

7.修改表结构 alert table 表名 add|change|drop 列名 类型;
alert table newcar add isDelete bit default 0;

四、数据操作

1.增
a.全列插入:insert into 表名 values(…)
insert into student values(0,’tom’,19,1,’北京’,0)
第一个0是占位

b.缺省插入:insert into 表名(列1,列2,...)values(值1,值2,...);
insert into student(name.age,address) values('lilei',19,'上海');

c.同时插入多条数据 insert into 表名 values(...),(...),...
insert into student values(0,'hanmeimei',19,0,'北京',0),(0,'popo',22,1,'海南',0);

2.删除
delete from 表名 where 条件;
delete from student where id=4;
注意,没有条件是全部删除,慎用

3.改
update 表名 set 列1=值1,列2=值2,…where 条件
update student set age=16 where id=7;
注意:如果没有条件,全部列都修改,慎用

4.查
查询表中全部数据 select * from 表名;

五、查

1.基本语法
select * from 表名;
select后面写表中的列名,如果是*,表示结果集中显示表中的所有列
如果要查询多个列,之间使用逗号分隔
select name,age from student;
select name as a ,age as b from student;

2.消除重复行

在select后面列的前面使用distinct可以消除重复的行

select gender from student; 
select distinct gender from student;

3.条件查询
a.语法
select * from 表名 where 条件;

b.比较运算符
等于  =
大于  select * from student where id > 8;
大于等于 select * from student where id >= 8;
小于等于
不等于

c.逻辑运算符
and select * from student where id>7 and gender=0; 
or
not 

d.模糊查询
like    
%表示任意多个任意字符
_表示一个任意字符
select * from student where name like '习%';
select * from student where name like '习_';

e.范围查询      
 in 表示在一个非连续的范围内
between...and... 表示在一个连续的范围内

select * from student where id in (8,10,12);
select * from student where id between 6 and 8;

f:空判断
注意 null与‘’不同
判断空 is null
判断非空 is not null

select * from student where address is null;

g.优先级
小括号,比较运算符,逻辑运算符
and比or优先级高,如果同时出现并且想用or,就用小括号

4.聚合
为了快速得到统计的数据,提供了5个聚合函数
a. count() 表示统计总行数,括号中可以写和列名
b. max(列) 表示求此列的最大值
c. min(列) 表示求此列的最小值
d. sum(列) 表示求此列的和
e. avg(列) 表示求此列的平均值

select count(*) from student; 
select max(id) from student where gender=0;
select sum(age) from studnet where gender=0;

5.分组
按照字段分组,表示此字段相同的数据会被放到一个集合中。
分组后,只能查询出相同的数据列,对于有差异的数据列,无法显示在结果集中。可以对分组后的数据进行统计,做聚合运算。
select gender,count(*) from student group by gender; 计算男女各有多少个
select gender,count(*) from student group by gender having age>20;

where是对from后面表中的进行筛选
having是对group by的结果进行筛选

6.排序
select * from 表名 order by 列1 asc|desc,列2 asc|desc,…
将数据按照列1进行排序,如果某些列1的值相同,则按照列2的进行排序
默认从小到大排序,asc升序,desc降序
select * from student order by age;
select * from student where isDelete=0 order by age desc,id desc;

7.分页
select * from 表名 limit start,count;
select * from student limit 0,3;
select * from student limit 3,3;
select * from student where gender=1 limit 0,3;
索引从0开始

六、关联

建表语句:

1.create table class(id int auto_increment primary key,name varchar(20) not null,stuNum int not null);

2.create table student(id int auto_increment premary key,name varchar(20) not null,gender bit default 1,classid int not null,foreign key(classid) references class(id));

插入一些数据:

insert into class value(0,'python01',55),(0,'pyhton02',50);

insert into students value(0,'tom',1,1);

相关例题

(1)create_company

-- SQL:
--  DDL: create / drop / alter
--  DML: insert / delete / update
--  DQL: select
--  DCL: grant / revoke

-- 创建company数据库
drop database if exists company;
create database company default charset utf8;

-- 关系型数据库用二维表组织数据
-- 关系型数据库有自己的编程语言

-- 切换到company数据库
use company;

-- 创建部门表
-- primary key能够唯一确定一条记录的列可以设置为主键
drop table if exists tb_dept;

create table tb_dept
(
deptno integer not null comment '部门编号',
dname varchar(10) not null comment '名称',
dloc varchar(10) comment '所在地',
primary key (deptno)
);

alter table tb_dept add ddate  date comment '成立日期';

-- 向部门表添加数据
insert into tb_dept values (10, '财务部', '成都', now());
insert into tb_dept (deptno, dname) values (20, '研发部');
insert into tb_dept (deptno, dname) values (30, '销售一部'),(40, '销售二部'),(50, '后勤保障部');

delete from tb_dept where deptno=50;

update tb_dept set dloc='深圳', ddate='2018-4-1' where deptno=30;

-- 查所有行所有列
select * from tb_dept;
-- 投影
select deptno, dname from tb_dept;
-- 别名
select deptno as 部门编号, dname as 部门名称 from tb_dept;
-- 筛选
select deptno, dname from tb_dept where dloc='深圳';

(2)create_homework


drop database if exists homework;
create database homework default charset utf8;

use homework;

drop table if exists Grade;
drop table if exists Student;
drop table if exists StudentInfo;

create table Grade(
id int auto_increment not null comment'编号',
g_name varchar(5) not null,
g_desc varchar(30) not null,
g_create_time date not null,
primary key (id)
);

insert into Grade values (1,'牛班','一个很牛的班',1);
insert into Grade values (2,'虎班','一个很虎的班',1);
insert into Grade values (3,'熊班','一个很熊的班',1);


create table Student(
id int auto_increment not null comment'编号',
s_name varchar(10) not null,
s_addr varchar(30) not null,
s_sex varchar(5) not null,
s_tel varchar(11),
s_yuwen float not null,
s_math float not null,
g_id int not null,
primary key (id),
foreign key (g_id) references Grade(id)
);

insert into Student values (1,'小鸡','鸡窝','母','111','70','67',1);
insert into Student values (2,'母鸡','鸡窝','母','111','90','30',1);
insert into Student values (3,'公鸡','鸡窝','公','111','50','88',1);
insert into Student values (4,'战斗鸡','鸡窝','妖','111','99','100',1);
insert into Student values (5,'大熊','熊窝','母','222','30','40',2);
insert into Student values (6,'小熊','熊窝','母','222','76','87',2);
insert into Student values (7,'陈宫','平野','男','333','90','22',3);
insert into Student values (8,'刘备','荆州','男','444','76','72',3);
insert into Student values (9,'水起','建业','男','555','45','78',2);
insert into Student values (10,'伯约','天水','男','444','90','90',3);
insert into Student values (11,'德贤','咸阳','男','666','56','98',2);


create table StudentInfo(
id int auto_increment not null comment'编号',
i_name varchar(5) not null,
i_phone varchar(11) not null,
i_id int not null,
primary key (id),
foreign key (i_id) references Student(id)
);

insert into StudentInfo values (1,'大鸡','1111',1);
insert into StudentInfo values (2,'大鸡','1111',2);
insert into StudentInfo values (3,'大鸡','1111',3);
insert into StudentInfo values (4,'大鸡','1111',4);
insert into StudentInfo values (5,'超级熊','2222',5);
insert into StudentInfo values (6,'超级熊','2222',6);
insert into StudentInfo values (7,'陈大宫','3333',7);
insert into StudentInfo values (8,'刘大备','4444',8);
insert into StudentInfo values (9,'船翻','5555',9);
insert into StudentInfo values (10,'姜维','6666',10);
insert into StudentInfo values (11,'有德','7777',11);
#每个班级的学生总数
select count(*) 学生总数, g.g_name 班级名称 from Student s join Grade g on s.g_id=g.id group by(g.id);

SELECT * FROM
(SELECT s.g_id , MAX(s.s_yuwen) AS score
FROM Student AS s
GROUP BY (s.g_id)) AS t1
JOIN Student AS t2
ON t2.s_yuwen = t1.score AND t2.g_id = t1.g_id;
#decimal(3,1)三位数,有一位小数
select * from
(select max(s_yuwen) as c, g_id from Student s1 group by s1.g_id) t1
join Student t2 on t2.s_yuwen = t1.c and t1.g_id=t2.g_id;


select * from student s1
where 3>= (
select count(*) from student s
where s1.s_math>=s.s_math
and s1.g_id=s.g_id
) order by s1.g_id, s1.s_math;

#每个班及语文成绩总和 与平均分
select avg(s_yuwen) '语文平均分', sum(s_yuwen) '语文成绩总和', g.g_name '班级名称' from student s
join Grade g on g.id=s.g_id
group by(g.id)
#语文成绩大于60分的学生
select * from student s 
join Grade g on g.id=s.g_id
having s.s_yuwen > 60
order by s.id

select * from student s
join Grade g on g.id=s.g_id
where s.s_yuwen > 60
order by s.id

(3)create_stu

drop DATABASE if EXISTS stu 
create DATABASE stu DEFAULT CHARSET utf8;
use stu;

drop table if exists TbStudent;
drop table if exists TbCourse;
drop table if exists TbSC;


# 创建学生
create table TbStudent(
stuid integer auto_increment primary key,
stuname varchar(5) not null,
stusex bit default 1,
[we stubirth datetime not null,
stutel char(11) not null,
stuaddr varchar(255) not null,
stuphoto longblob
);
# 从表中删除stutel这列
alter table TbStudent drop column stutel;
alter table TbStudent add column stutel int;

insert into TbStudent values (1001,'张三丰',1,'1978-1-1','成都市一环路西二段17号',null);
insert into TbStudent (stuid,stuname,stusex) values (1002,'郭靖','1980-2-2');
insert into TbStudent (stuid,stuname,stusex,stubirth,stuaddr) values (1003,'黄蓉',0,'1982-3-3','成都市二环路南四段123号');
insert into TbStudent values (1004,'张无忌',1,'1990-4-4',null,null),
(1005,'丘处机',1,'1983-5-5','北京市海淀区宝盛北里西区28号',null),
(1006,'王处一',1,'1985-6-6','深圳市宝安区宝安大道5010号',null),
(1007,'刘处玄',1,'1987-7-7','郑州市金水区纬五路21号',null),
(1008,'孙不二',0,'1989-8-8','武汉市光谷大道61号',null),
(1009,'平一指',1,'1992-9-9','西安市雁塔区高新六路52号',null),
(1010,'老不死',1,'1993-10-10','广州市天河区元岗路310号',null),
(1011,'王小美',0,'1994-11-11',null,null),
(1012,'隔壁老王',1,'1995-12-12',null,null),
(1013,'郭啸天',1,'1977-10-25',null,null);

delete from TbStudent where stuid=1004;

update TbStudent set stubirth='1980-12-12',stuaddr='上海市宝山区同济支路199号' where stuid=1002;


#课程表
create table TbCourse(
cosid integer not null,
cosname varchar(50) not null,
coscredit tinyint not null,
cosintro varchar(255)
);
# 给此表添加主键
alter table TbCourse add constraint pk_course primary key(cosid);

insert into TbCourse values (1111,'C语言程序设计',3,'大神级讲师授课需要抢座'),
(2222,'Java程序设计',3,null),
(3333,'数据库概论',2,null),
(4444,'操作系统原理',4,null);


# 学生选课
create table TbSC(
scid integer primary key auto_increment,
sid integer not null,
cid integer,
scdate datetime not null,
score float
);
#给此表添加外键   on delete和on update指在删除和更新是进行变化。cascade主键更改时,关联的外键会随之更改
# restrict在父表中进行删除时,首先检测是有否外键,若有则不能删除 还一个no action意义相同
alter table TbSC add constraint fk_sid foreign key(sid) references TbStudent(stuid) on delete cascade on update cascade;
alter table TbSC add constraint fk_cid foreign key(cid) references TbCourse(cosid) on delete set null on update cascade;

insert into TbSC values (default,1001,1111,'2016-9-1',95),
(default,1002,1111,'2016-9-1',94),
(default,1001,2222,now(),null),
(default,1001,3333,'2017-3-1',85),
(default,1001,4444,now(),null),
(default,1002,4444,now(),null),
(default,1003,2222,now(),null),
(default,1003,3333,now(),null),
(default,1005,2222,now(),null),
(default,1006,1111,now(),null),
(default,1006,2222,'2017-3-1',80),
(default,1006,3333,now(),null),
(default,1006,4444,now(),null),
(default,1007,1111,'2016-9-1',null),
(default,1007,3333,now(),null),
(default,1007,4444,now(),null),
(default,1008,2222,now(),null),
(default,1010,1111,now(),null);

#查询所有学生信息
select * from TbStudent s join TbSC S on S.sid=s.stuid order by stuid;
#查询所有课程名称及学分
select cosname 课程名称,coscredit 学分 from TbCourse; 
#查询所有女学生的姓名和出生日期(筛选)
select stuname 学生姓名,stubirth 出生日期 from TbStudent where stusex=0;
#查询所有80后学生的姓名,性别和出生日期(筛选)
select stuname 学生姓名,stusex 性别,stubirth 出生日期 from TbStudent where stubirth like '198%';
#查询姓王的学生姓名和性别(模糊)
select stuname 学生姓名,stusex 性别 from TbStudent where stuname like '王%';
#查询姓郭名字总共两个字的学生的姓名(模糊)
select stuname 学生姓名,stusex 性别 from TbStudent where stuname like '郭%' and stuname like '__';
#查询姓郭名字总共三个字的学生的姓名(模糊)
select stuname 学生姓名,stusex 性别 from TbStudent where stuname like '郭%' and stuname like '___';
#查询名字中有王字的学生的姓名(模糊)
select stuname 学生姓名,stusex 性别 from TbStudent where stuname like '%王';
#查询没有录入家庭地址和照片的学生姓名(多条件筛选和空值处理)
select stuname 学生姓名 from TbStudent where stuaddr='';
#查询学生选课的所有日期(去重)
select distinct scdate 选课日期 from TbSC;
#查询学生的姓名和生日按年龄从大到小排序(排序)
select stuname 学生姓名, stubirth 生日 from TbStudent order by stubirth;
#查询所有录入了家庭地址的男学生的姓名、出生日期和家庭住址按年龄从小到大排列(多条件筛选和排序)
select stuname 学生姓名, stubirth 出生日期, stuaddr 家庭住址 from TbStudent where stuaddr!='' and stusex=1 order by stubirth desc;
#查询年龄最大的学生的出生日期(聚合函数)
select stuname 学生姓名, stubirth 出生日期 from TbStudent where stubirth=(select min(stubirth) from TbStudent);
#查询年龄最小的学生的出生日期(聚合函数)
select stuname 学生姓名, stubirth 出生日期 from TbStudent where stubirth=(select max(stubirth) from TbStudent);
#查询男女学生的人数(分组和聚合函数)
select count(*) 人数, stusex 性别 from TbStudent group by stusex;
#查询课程编号为1111的课程的平均成绩(筛选和聚合函数)
select avg(score) from TbSC where cid=1111;
#查询学号为1001的学生所有课程的总成绩(筛选和聚合函数)
select sum(score) from TbSC where sid=1001;
#查询每个学生的学号和平均成绩,null值处理为0(分组和聚合函数)
select sid 学号, avg(ifnull(score,0)) from TbSC group by sid;
#查询平均成绩大于等于90分的学生的学号和平均成绩
select t.学号, t.a 平均分 from (select sid 学号, avg(score) a from TbSC group by sid) t where t.a>=90;
#查询年龄最大的学生的姓名
select stuname 学生姓名 from TbStudent where stubirth=(select min(stubirth) from TbStudent);
#查询选了两门以上的课程的学生姓名
select t.stuname 学生姓名 from TbStudent t, (select count(*) 选课数, sid from TbSC group by sid) t1 where t1.选课数>2 and t.stuid=t1.sid;
#查询选课学生的姓名和平均成绩
select t.stuname 学生姓名, t1.均分 from TbStudent t, (select avg(score) 均分, sid from TbSC group by sid) t1 where t1.均分 is not null and t.stuid=t1.sid;
#查询学生姓名、所选课程名称和成绩
select s.stuname 学生姓名, s1.cosname 课程名称, ifnull(s1.score,0) 成绩 from (select * from TbSC sc join TbCourse c on sc.cid=c.cosid) s1 join TbStudent s on s1.sid=s.stuid order by s1.sid;
#查询每个学生的姓名和选课数量
select s.stuname 学生姓名, ifnull(s1.s,0) 选课数 from (select count(*) s, sid from TbSC group by sid) s1 right join TbStudent s on s1.sid=s.stuid;

select stuname from TbStudent where stuid in (select sid a from TbSC group by sid having count(*)>2);

select stuname from TbStudent limit   

select distinct sid from TbSC #distinct表示去重     alter table        modify修改原有列属性

(4)create_test2

drop database if exists test2;
create database test2 default charset utf8;

use test2;
create table grade(
id int auto_increment not null,# auto_increment递增
g_name varchar(30) not null,
primary key(id)
);

create table student(
id int auto_increment not null,
s_name varchar(10) not null,
s_tel varchar(11),
g_id int default null,
primary key(id),
foreign key(g_id) references grade(id) #references标记在grade的id上
);

delete from grade;
insert into grade  values ('1','python');
insert into grade  values ('2','java');
insert into grade  values ('3','php');

delete from student;
insert into student values (1,'傻子','12345678910',1);
insert into student values (2,'呆子','14345678910',1);
insert into student values (3,'蠢瓜','12345678910',1);
insert into student values (4,'大呆','14345678910',2);
insert into student values (5,'二呆','14345678910',2);
insert into student values (6,'三呆','14345678910',2);
insert into student values (7,'大傻','12345678910',3);
insert into student values (8,'二傻','12345678910',3);
insert into student values (9,'三傻','12345678910',3);
insert into student values (10,'蠢瓜1','12345678910',1);

select * from student s join grade g on g.id=s.g_id where g.g_name='python';
# select *选择所有数据,join是连接,on是条件
select s_name '垃圾',g.g_name '喳喳' from student s join grade g on s.g_id=g.id group by(g.id);
select count(*) '学生人数', g.g_name '班级名称' from student s join grade g on s.g_id=g.id group by(g.id);

alter table student drop s_grades;# 删字段
# delete from student where  删行
alter table student change s_grades s_grades int;#改字段 重命名一定要存在.
alter table student add s_grades int;# 加字段
update student set s_grades=70 where id=1;
update student set s_grades=50 where id=2;
update student set s_grades=80 where id=3;
update student set s_grades=90 where id=4;
update student set s_grades=30 where id=5;
update student set s_grades=67 where id=6;
update student set s_grades=80 where id=7;
update student set s_grades=97 where id=8;
update student set s_grades=99 where id=9;
update student set s_grades=100 where id=10;

select max(s_grades) 最高分 from student;
select min(s_grades) 最低分 from student;
select avg(s_grades) 平均分 from student;

(5)create_HR

drop database if exists HR;
create database HR default charset utf8;

use HR;

drop table if exists TbDept;
create table TbDept(
deptno int not null comment '部门编号',
dname varchar(5) not null,
dloc varchar(30) not null comment '部门所在地',
primary key(deptno)
);

insert into TbDept values (1,'企划部','北京');
insert into TbDept values (2,'人事部','深圳');
insert into TbDept values (3,'测试部','广东');
insert into TbDept values (4,'战略部','成都');
insert into TbDept values (5,'事业部','深圳');

drop table if exists TbEmp;

create table TbEmp(
empno int not null comment '员工编号',
ename varchar(5) not null,
job varchar(10) not null,
mgr int not null comment '主管编号',
sal int not null,
dno int not null comment '所在部门编号',
primary key(empno),
foreign key(dno) references TbDept(deptno)
);

insert into TbEmp values (1001,'张飞','程序员',25,'6000',5);
insert into TbEmp values (1002,'关羽','程序员',25,'7000',5);
insert into TbEmp values (1003,'马超','战略师',24,'8000',4);
insert into TbEmp values (1004,'诸葛亮','战略主管',0,'16000',4);
insert into TbEmp values (1005,'庞统','战略师',24,'9000',4);
insert into TbEmp values (1006,'貂蝉','对外公关',22,'10000',2);
insert into TbEmp values (1007,'小乔','招聘',22,'5000',2);
insert into TbEmp values (1008,'甄宓','对外公关',22,'8000',2);
insert into TbEmp values (1009,'庞德','架构师',23,'26000',3);
insert into TbEmp values (1010,'曹操','技术总监',0,'46000',1);

select ename 员工姓名, sal 最高薪资 from TbEmp where sal = (select max(sal) from TbEmp);

select ename 员工姓名, (select 12*sal) 年薪 from TbEmp;

select e1.ename, e2.c 部门编号, e2.dname 部门名称 from TbEmp e1, (select d.dname, e.dno, count(*) c from TbEmp e join TbDept d on e.dno=d.deptno group by e.dno) e2 where e1.dno=e2.dno;

select d.dname 部门名称, e.dno 部门编号, count(*) 人数 from TbEmp e join TbDept d on e.dno=d.deptno group by d.deptno;

select ename 员工姓名, sal 最高薪资 from TbEmp where sal = (select max(sal) from TbEmp where mgr!=0);

select ename 员工姓名, sal 工资 from TbEmp where sal >(select avg(sal) from TbEmp);

select * from TbDept e2 join (select ename 员工姓名, e1.dno 部门编号, sal 工资 from TbEmp e1 where e1.sal>(select a from (select avg(sal) a, dno from TbEmp group by dno) e where e1.dno=e.dno)) e3 on e2.deptno=e3.部门编号;

select * from TbDept e2 join (select ename 员工姓名, e1.dno 部门编号, sal 工资 from TbEmp e1 where e1.sal=(select m from (select max(sal) m, dno from TbEmp group by dno) e where e1.dno=e.dno)) e3 on e2.deptno=e3.部门编号; 

select * from TbEmp where empno in (select mgr,empno from TbEmp where mgr=0);#distinct 去重

select * from TbEmp order by sal desc limit 3;
# asc表示正序,desc表示倒叙
select * from TbEmp order by sal limit 3,5;
drop database if exists test1;
create database test1 default charset utf8;

use test1;

create table student(
id int auto_increment not null,
s_name varchar(30) not null,
primary key(id)
);

alter table student add sex text comment '性别';
insert into student (s_name) values('coco');
insert into student (s_name) values('张三');
insert into student (s_name) values('罗超');
update student set sex='妖' where s_name='罗超';
delete from student where s_name='罗超';
select * from student limit 3 offset 2;     #limit是显示数据,后面是几就显示前几行。offset是从第几条开始,第一条是0
# order by(-id)将表中倒叙输出,id in(1,3,4,5)表示输出id为括号中的数据
delete from student where id='2';
insert into student values ('2','张飞','男');

猜你喜欢

转载自blog.csdn.net/Lq_520/article/details/81475818