//查看所有数据库
SHOW DATABASES;
//选择使用数据库
USE blog;
//查看所有表
SHOW TABLES;
//创建表,若存在则删除原表
DROP TABLE IF EXISTS usertest;
CREATE TABLE usertest(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(20) NOT NULL,
age INT CHECK (age>20),
sex01 ENUM('男','女') DEFAULT '男',
PASSWORD VARCHAR(50) NOT NULL,
role VARCHAR(20) NOT NULL DEFAULT 'user' CHECK(role IN('user', 'admin'))
)
//默认插入表所有数据
INSERT INTO usertest VALUES
(7,"yto01",15,"男","111","admin"),
(4,"yto01",15,"男","111","user"),
(5,"yto01",15,"男","111","admin"),
(6,"yto01",15,"男","111","user");
//选择插入表数据
INSERT INTO usertest(NAME,age,PASSWORD) VALUES("yto01",30,"111")
//查询所有
SELECT * FROM usertest
//按条件查询
SELECT NAME AS 姓名,
age AS 年龄,
sex01 AS 性别
FROM usertest
WHERE id>2
GROUP BY NAME
HAVING age>15
ORDER BY age
//更新
UPDATE usertest
SET PASSWORD="123"
WHERE age="15"
//删除操作
DELETE FROM usertest
WHERE id=1;
//添加列
ALTER TABLE usertest
ADD COLUMN datetest VARCHAR(20)
//删除列
ALTER TABLE usertest
DROP COLUMN datetest
晋升版
use test
#学生表 Student
drop table if exists Student;
create table Student(Sid varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
#科目表 Course
DROP TABLE IF EXISTS Course;
create table Course(Cid varchar(10),Cname nvarchar(10),Tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
#教师表 Teacher
DROP TABLE IF EXISTS Teacher;
create table Teacher(Tid varchar(10),Tname nvarchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
#成绩表 SC
DROP TABLE IF EXISTS SC;
create table SC(Sid varchar(10),Cid varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
#1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select *
FROM (select * from sc where Cid='01')A
left join (select * FROM sc WHERE Cid='02')B
on A.Sid =B.Sid
where A.score>B.score
#1.1 查询同时存在" 01 "课程和" 02 "课程的情况
select *
from (select * from SC where Cid='01')A
left join (select * from SC where Cid='02')B
on A.Sid=B.Sid
where B.Sid is not null
#1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from
(select * from sc where Cid=01)A
LEFT join (select * from sc where Cid=02)B
on A.Sid=B.Sid
#1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from
(select * from sc where Cid=02)A
LEFT join (select * from sc where Cid=01)B
on A.Sid=B.Sid
where B.Cid is null
select * from SC
where Cid='02'and Sid not in(select Sid from SC where Cid='01')
#2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select B.Sid,B.Sname,A.dc
from(select Sid,avg(sc.score)dc from sc group by sc.Sid )A
left join student B
on A.Sid=B.Sid
where A.dc>=60
#3. 查询在 SC 表存在成绩的学生信息
select * from
(select * from student)A
left join (select * from sc)B
on A.Sid=B.Sid
WHERE B.Sid IS NOT NULL
group by A.Sid
#4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select B.Sid,B.Sname,A.countc,A.sumc from
(select sc.`Sid`, COUNT(sc.`Cid`)countc,SUM(sc.`score`)sumc
from sc GROUP BY sc.`Sid`)A
left join(select * from student)B
on A.Sid=B.Sid
#4.1 查有成绩的学生信息
select * from
(select * from student)A
left join (select * from sc )B
on A.Sid=B.Sid
WHERE B.Sid is not null
#5. 查询「李」姓老师的数量
select * from teacher where teacher.`Tname` like "李%"
#6. 查询学过「张三」老师授课的同学的信息
select student.`Sid`,student.`Sname`,student.`Ssex`,student.`Sage` ,teacher.`Tname`
from student,sc,teacher,course
where student.`Sid`=sc.`Sid`
and sc.`Cid`=course.`Cid`
and course.`Tid`=teacher.`Tid`
and teacher.`Tname`='张三'
group by student.`Sid`
#7. 查询没有学全所有课程的同学的信息
select * from
(SELECT * FROM student)D
LEFT JOIN (select * from sc where sc.`Cid`='01')A ON A.Sid=D.Sid
left join (SELECT * FROM sc WHERE sc.`Cid`='02')B ON B.Sid=A.Sid
left join (SELECT * FROM sc WHERE sc.`Cid`='03')C ON C.Sid=B.Sid
WHERE A.Sid is null or B.Sid is null or C.Sid is null
实在是太变态了,这个练习:超经典SQL练习题,做完这些你的SQL就过关了-https://blog.csdn.net/flycat296/article/details/63681089/ 有兴趣吧这学完