查看建表语句

show create table `表名`

全部笔记

-- Notepad++快捷键:
--      CTRL D复制一行
--      CTRL L删除一行-- Eclipse快捷键:
--      ALT ↑↓移动一行-- 单个主键约束 主键不能为空 不能重复
create table user(id int primary key,name varchar(20)
);-- 显示所有表
show tables;-- 显示表结构
describe user;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)-- 插入
insert into user values(1, "魔鬼");-- 查询
select * from user;-- 联合主键 只要联合的主键值加起来不重复就可以 任何不能为空
create table user2(id int,name varchar(20),password varchar(20),primary key (id,name)
);-- 插入
insert into user2 values(1, "大傻子", 123456);
insert into user2 values(2, "大傻子", 123456);
insert into user2 values(2, "大鲨鱼", 123456);select * from user2;-- 自增约束
create table user3(id int primary key auto_increment,name varchar(20)
);insert into user3(name) values("大鲨鱼");
insert into user3(id, name) values(9, "大鲨鱼");
insert into user3(name) values("大鲨鱼");
insert into user3(name) values("大鲨鱼");
insert into user3(name) values("大鲨鱼");
insert into user3(name) values("大鲨鱼");-- 忘记创建主键,如何添加
create table user4(id int,name varchar(20)
);-- 修改为主键
alter table user4 add primary key(id);          -- 方法1
alter table user4 modify id int primary key;    -- 方法2-- 删除主键
alter table user4 drop primary key;-- 唯一约束 字段的值不能重复
create table user5(id int,name varchar(20),unique(name) -- 这里
);create table user6(id int unique, -- 也可以这里name varchar(20)
);alter table user5 add unique(name);   -- 如果创建表时忘记添加 可以这样修改
desc user5;insert into user5 values(1,"张三");
insert into user5 values(2,"张三");
ERROR 1062 (23000): Duplicate entry '张三' for key 'name'create table user7(id int,name varchar(20),unique(id,name)   -- 组合不重复
);
insert into user7 values(1,"张三");
insert into user7 values(2,"张三");-- 删除唯一约束
alter table user7 drop index id;-- 添加唯一约束
alter table user7 modify name varchar(20) unique;-- 删除表中所有数据
delete from user7;-- 非空约束
create table user8(id int,name varchar(20) not null
);
insert into user8 values(5, "张三");-- 默认约束 设置默认值
create table user10(id int,name varchar(20),age int default 10
);
insert into user10(id) values(1);-- 外键约束 涉及到两个表:主表、副表
-- 1) 副表的内容要参照主表,若主表不存在值,则副表不能使用
-- 2) 主表中的记录被副表引用,不可以被删除
create table classes(id int primary key,name varchar(20)
);
create table students(id int primary key,name varchar(20),class_id int,foreign key(class_id) references classes(id)
);
desc students;insert into classes values(1,"一班");
insert into classes values(2,"二班");
insert into classes values(3,"三班");
insert into classes values(4,"四班");insert into students values(1,"小明",2);
insert into students values(4,"老高",3);insert into students values(2,"老裴",5);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`today`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))-- 数据库的三大设计范式
-- 1)第一范式 1NF-- 数据表中的所有字段都是不可分割的原子值
create table students2(id int primary key,name varchar(20),address varchar(30)
);insert into students2 values(1,"高淇","中国北京海淀区200号");
insert into students2 values(2,"裴新","中国北京朝阳区50号");
insert into students2 values(3,"哥","山东青岛88号");-- 字段值还可以继续拆分,不满足第一范式
+----+------+---------------------+
| id | name | address             |
+----+------+---------------------+
|  1 | 高淇 | 中国北京海淀区200号 |
|  2 | 裴新 | 中国北京朝阳区50号  |
|  3 | 哥   | 山东青岛88号        |
+----+------+---------------------+create table students3(id int primary key,name varchar(20),country varchar(30),province varchar(30),streetNum varchar(30)
);
insert into students3 values(1,"高淇","中国","北京","200号");
insert into students3 values(2,"裴新","中国","上海","800号");
insert into students3 values(3,"哥","中国","江西","6号");
-- 字段值不能更详细划分,满足第一范式
+----+------+---------+----------+-----------+
| id | name | country | province | streetNum |
+----+------+---------+----------+-----------+
|  1 | 高淇 | 中国    | 北京     | 200号     |
|  2 | 裴新 | 中国    | 上海     | 800号     |
|  3 | 哥   | 中国    | 江西     | 6号       |
+----+------+---------+----------+-----------+-- 2) 第二范式
-- 满足第一范式前提下,除主键外每一列必须完全依赖主键-- 不满足第二范式
create table mylist(product_id int,product_name varchar(20),costumer_id int,costumer_name varchar(20),primary key(product_id,costumer_id)
);
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| product_id    | int(11)     | NO   | PRI | NULL    |       |
| product_name  | varchar(20) | YES  |     | NULL    |       |
| costumer_id   | int(11)     | NO   | PRI | NULL    |       |
| costumer_name | varchar(20) | YES  |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+-- 满足第二范式
create table user_order(order_id int primary key,product_id int,costumer_id int
);create table product(id int primary key,name varchar(20)
);create table costumer(id int primary key,name varchar(20)
);
desc mylist;
desc user_order;
desc product;
desc costumer;
+-------------+---------+------+-----+---------+-------+
| Field       | Type    | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| order_id    | int(11) | NO   | PRI | NULL    |       |
| product_id  | int(11) | YES  |     | NULL    |       |
| costumer_id | int(11) | YES  |     | NULL    |       |
+-------------+---------+------+-----+---------+-------++-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------++-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+-- 3)第三范式
-- 除了主键列之外,其他列之间不能有传递依赖关系-- 不满足第三范式
create table user_order(order_id int primary key,product_id int,costumer_id intcustomer_phone varchar(15)   -- phone应该放在costumer表中
);-- 查询练习
-- 创建数据库
CREATE DATABASE select_test;
-- 切换数据库
USE select_test;-- 创建学生表
CREATE TABLE student (no VARCHAR(20) PRIMARY KEY,       -- 学号name VARCHAR(20) NOT NULL,     -- 姓名sex VARCHAR(10) NOT NULL,      -- 性别birthday DATE,                     -- 生日class VARCHAR(20)              -- 所在班级
);-- 创建教师表
CREATE TABLE teacher (no VARCHAR(20) PRIMARY KEY,           -- 教师号name VARCHAR(20) NOT NULL,            -- 姓名sex VARCHAR(10) NOT NULL,          -- 性别birthday DATE,                     -- 生日profession VARCHAR(20) NOT NULL,   -- 职称department VARCHAR(20) NOT NULL        -- 部门
);-- 创建课程表
CREATE TABLE course (no VARCHAR(20) PRIMARY KEY,            -- 课程号name VARCHAR(20) NOT NULL,            -- 课程名t_no VARCHAR(20) NOT NULL,            -- 教师号FOREIGN KEY(t_no) REFERENCES teacher(no)-- 外键关联教师号
);-- 成绩表
CREATE TABLE score (s_no VARCHAR(20) NOT NULL,              -- 学号c_no VARCHAR(20) NOT NULL,                 -- 课程号degree DECIMAL,                           -- 成绩FOREIGN KEY(s_no) REFERENCES student(no),-- 外键绑定学号FOREIGN KEY(c_no) REFERENCES course(no), -- 外键绑定课程号PRIMARY KEY(s_no, c_no)                   -- 联合主键:学号、课程号
);-- 查看所有表
SHOW TABLES;-- 添加学生表数据
INSERT INTO student VALUES('101', '曾华', '男', '1977-09-01', '95033');
INSERT INTO student VALUES('102', '匡明', '男', '1975-10-02', '95031');
INSERT INTO student VALUES('103', '王丽', '女', '1976-01-23', '95033');
INSERT INTO student VALUES('104', '李军', '男', '1976-02-20', '95033');
INSERT INTO student VALUES('105', '王芳', '女', '1975-02-10', '95031');
INSERT INTO student VALUES('106', '陆军', '男', '1974-06-03', '95031');
INSERT INTO student VALUES('107', '王尼玛', '男', '1976-02-20', '95033');
INSERT INTO student VALUES('108', '张全蛋', '男', '1975-02-10', '95031');
INSERT INTO student VALUES('109', '赵铁柱', '男', '1974-06-03', '95031');-- 添加教师表数据
INSERT INTO teacher VALUES('804', '李诚', '男', '1958-12-02', '副教授', '计算机系');
INSERT INTO teacher VALUES('856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');
INSERT INTO teacher VALUES('825', '王萍', '女', '1972-05-05', '助教', '计算机系');
INSERT INTO teacher VALUES('831', '刘冰', '女', '1977-08-14', '助教', '电子工程系');-- 添加课程表数据
INSERT INTO course VALUES('3-105', '计算机导论', '825');
INSERT INTO course VALUES('3-245', '操作系统', '804');
INSERT INTO course VALUES('6-166', '数字电路', '856');
INSERT INTO course VALUES('9-888', '高等数学', '831');-- 添加添加成绩表数据
INSERT INTO score VALUES('101', '6-166', '88');
INSERT INTO score VALUES('103', '3-105', '92');
INSERT INTO score VALUES('103', '3-245', '86');
INSERT INTO score VALUES('103', '6-166', '85');
INSERT INTO score VALUES('105', '3-105', '88');
INSERT INTO score VALUES('105', '3-245', '75');
INSERT INTO score VALUES('105', '6-166', '79');
INSERT INTO score VALUES('109', '3-105', '76');
INSERT INTO score VALUES('109', '3-245', '68');
INSERT INTO score VALUES('109', '6-166', '81');-- 1、查询表中所有记录
SELECT * FROM course;
+-------+------------+------+
| no    | name       | t_no |
+-------+------------+------+
| 3-105 | 计算机导论 | 825  |
| 3-245 | 操作系统   | 804  |
| 6-166 | 数字电路   | 856  |
| 9-888 | 高等数学   | 831  |
+-------+------------+------+
SELECT * FROM score;
+------+-------+--------+
| s_no | c_no  | degree |
+------+-------+--------+
| 103  | 3-105 |     92 |
| 103  | 3-245 |     86 |
| 103  | 6-166 |     85 |
| 105  | 3-105 |     88 |
| 105  | 3-245 |     75 |
| 105  | 6-166 |     79 |
| 109  | 3-105 |     76 |
| 109  | 3-245 |     68 |
| 109  | 6-166 |     81 |
+------+-------+--------+
SELECT * FROM student;
+-----+--------+-----+------------+-------+
| no  | name   | sex | birthday   | class |
+-----+--------+-----+------------+-------+
| 101 | 曾华   | 男  | 1977-09-01 | 95033 |
| 102 | 匡明   | 男  | 1975-10-02 | 95031 |
| 103 | 王丽   | 女  | 1976-01-23 | 95033 |
| 104 | 李军   | 男  | 1976-02-20 | 95033 |
| 105 | 王芳   | 女  | 1975-02-10 | 95031 |
| 106 | 陆军   | 男  | 1974-06-03 | 95031 |
| 107 | 王尼玛 | 男  | 1976-02-20 | 95033 |
| 108 | 张全蛋 | 男  | 1975-02-10 | 95031 |
| 109 | 赵铁柱 | 男  | 1974-06-03 | 95031 |
+-----+--------+-----+------------+-------+
SELECT * FROM teacher;
+-----+------+-----+------------+------------+------------+
| no  | name | sex | birthday   | profession | department |
+-----+------+-----+------------+------------+------------+
| 804 | 李诚 | 男  | 1958-12-02 | 副教授     | 计算机系   |
| 825 | 王萍 | 女  | 1972-05-05 | 助教       | 计算机系   |
| 831 | 刘冰 | 女  | 1977-08-14 | 助教       | 电子工程系 |
| 856 | 张旭 | 男  | 1969-03-12 | 讲师       | 电子工程系 |
+-----+------+-----+------------+------------+------------+-- 2、查询 student 表中 name、sex、class 列
SELECT name, sex, class FROM student;-- 3、查询 teacher 表中不重复的 department 列
SELECT distinct department FROM teacher;-- 4、查询 score 表中成绩在60-80之间的所有记录
select * from score where degree between 60 and 80;
select * from score where degree>60 and degree<80;    -- 直接运算符比较-- 5、查询 score 表中成绩为 85,86 的记录
select * from score where degree in(85,86,87,88);-- 6、查询 student 表中 "95301" 班或性别为 "女" 的记录
select * from student where class = "95031" or sex = "女";-- 7、class 降序查询 student 表种所有记录
select * from student order by class;       -- 默认升序
select * from student order by class asc;   -- 升序
select * from student order by class desc;  -- 降序-- 8、以 c_no 升序、degree 降序查询 score 表中所有记录
select * from score order by c_no asc, degree desc;-- 9、查询"95031"班的学生人数
select count(*) from student where class="95031";-- 10、查询score表中最高分学生的学号和课程号
select s_no,c_no,degree from score where degree=(select max(degree) from score);   --子查询
select s_no,c_no,degree from score order by degree desc limit 0,1;  --排序:你不能确定有几个最高分-- 11、查询每门课的平均成绩
select avg(degree) from score where c_no="3-105";    -- 一门课
select c_no, avg(degree) from score group by c_no;  -- 每门课-- 12、查询score表中至少有两名学生选修并以3开头的课程的平均分数
select c_no,avg(degree),count(*) from score group by c_no; select c_no,avg(degree),count(*) from score group by c_no
having count(c_no)>=2 and c_no like '3%';-- 13、查询分数大于70,小于90的sno列
select s_no,degree from score
where degree>70 and degree<90;select s_no,degree from score
where degree between 70 and 90;-- 14、查询所有学生的  student.name   score.c_no   score.degree  列
-- 如果名称不重复 点之前的表名可以省略
select student.no,score.s_no,student.name, score.c_no, score.degree from student, score
where student.no = score.s_no;-- 15、查询所有学生的 sno(学号)  cname(课程名)  degree(成绩) 列
-- 通过观察发现 联查的原理是:先单查,再联合筛选
-- 最后查询结果数量 是跟着最长的结果degree表走的
-- 三表联查 不理解的话 先把where去掉 输出一下
select student.name,student.no,course.name,score.degree from student, course, score
where student.no = score.s_no and course.no = score.c_no;
-- 两表联查 答案
select score.c_no,course.name,score.degree from course,score
where score.c_no = course.no;-- 16、查询所有学生的sname学生名  cname课程名  degree课程成绩
select student.name,course.name,score.degree from student, course, score
where student.no = score.s_no and course.no = score.c_no;
-- 取别名
select  student.name as 姓名,course.name as 课程,score.degree as 成绩
from student, course, score
where student.no = score.s_no and course.no = score.c_no;-- 17、查询“95031”班学生每门课的平均分:先查什么,再查什么
select * from student where class="95031";   -- 先思考条件select course.name as 课程名称, course.no as 课程编号, avg(degree) as 平均分
from course, score
where score.s_no in(select no from student where class="95031") -- 条件筛选
and course.no = score.c_no
group by score.c_no;
+------------+----------+---------+
| 课程名称   | 课程编号 | 平均分  |
+------------+----------+---------+
| 计算机导论 | 3-105    | 82.0000 |
| 操作系统   | 3-245    | 71.5000 |
| 数字电路   | 6-166    | 80.0000 |
+------------+----------+---------+-- group by 的原理: 先收集前一句的结果 再按要求筛选
select * from score;
select * from course;
select * from course,score;
select * from course,score where course.no = score.c_no;                       -- where 用于联合两表(取交集)
select * from course,score where course.no = score.c_no group by score.c_no;   -- group by用于去除重复select * from course,score
where course.no = score.c_no -- where,group by顺序不能反
group by score.c_no; -- 18、查询选了"3-105"课程的同学的所有信息
select * from student where no in(select s_no from score where c_no = "3-105"  group by s_no);-- 19、查询成绩高于[学号为"109"、课程号为"3-105"的学生的成绩]的所有记录
select * from score where degree > (select degree from score where s_no=109 and c_no="3-105");
-- 先找到学号为"109"、课程号为"3-105"的学生的成绩
select degree from score where s_no=109 and c_no="3-105";-- 20、查询[与学号为108、101的同学同年出生]的所有学生的学号、姓名和生日
select * from student where year(birthday) in(select year(birthday) from student where no in(101, 108));
-- 先查询[学号为108、101的同学]的出生年份
select year(birthday) from student where no in(101, 108);-- 21、查询[“张旭”教师任课的]学生成绩
select * from student;
select * from course;
-- 查:张旭的教师号
select no from teacher where name = "张旭";
-- 查:张旭教师号对应的课程
select no from course where t_no in (select no from teacher where name = "张旭");
-- 查:上了这些课的学生成绩
select * from score where c_no in (select no from course where t_no in (select no from teacher where name = "张旭"));-- 22、查询选修某课程的同学的人数多于5人的教师姓名
-- 给我感觉 select count(*)是在group by之后运算的,select c_no 是在group by之前运算的?
INSERT INTO score VALUES('101', '3-105', '90');
INSERT INTO score VALUES('102', '3-105', '91');
INSERT INTO score VALUES('104', '3-105', '89');
-- 查:人数多于5人的课程编号
select c_no from score group by c_no having count(*)>5;
-- 查:老师 通过course衔接
select * from teacher where no in(select t_no from course where no in (select c_no from score group by c_no having count(*)>5));-- 23、查询95033班和95031班全体学生记录
INSERT INTO student VALUES('110', '张飞', '男', '1974-06-03', '95038');-- 24、 查询[存在有85分以上成绩]的课程的课程编号
select distinct c_no from score where degree > 85;-- 25、查询“计算机系”教师所教课程的成绩表
select * from score where c_no in(select no from course where t_no in(select no from teacher where department ="计算机系"));-- 26、查询[“计算机系”与"电子工程系"不同职称的教师]的姓名和职称
-- union求并集
select * from teacher;
+-----+------+-----+------------+------------+------------+
| no  | name | sex | birthday   | profession | department |
+-----+------+-----+------------+------------+------------+
| 804 | 李诚 | 男  | 1958-12-02 | 副教授     | 计算机系   |
| 825 | 王萍 | 女  | 1972-05-05 | 助教       | 计算机系   |
| 831 | 刘冰 | 女  | 1977-08-14 | 助教       | 电子工程系 |
| 856 | 张旭 | 男  | 1969-03-12 | 讲师       | 电子工程系 |
+-----+------+-----+------------+------------+------------+select * from teacher where department="计算机系" and profession not in (select profession from teacher where department="电子工程系")
union
select * from teacher where department="电子工程系" and profession not in (select profession from teacher where department="计算机系");
+-----+------+-----+------------+------------+------------+
| no  | name | sex | birthday   | profession | department |
+-----+------+-----+------------+------------+------------+
| 804 | 李诚 | 男  | 1958-12-02 | 副教授     | 计算机系   |
| 856 | 张旭 | 男  | 1969-03-12 | 讲师       | 电子工程系 |
+-----+------+-----+------------+------------+------------+-- 27、查询[选了编号为“3-105”的课程]且[成绩至少高于一个选了编号为"3-245"的课程同学]的[课程no、学生no、成绩],并按照degree从高到低排序
-- any表示至少一个
select * from score
where c_no="3-105" and degree > any (select degree from score where c_no="3-245")
order by degree desc;-- 28、()查询[选了编号为“3-105”的课程]且[成绩高于所有选了编号为"3-245"的课程同学]的[课程no、学生no、成绩],并按照degree从高到低排序
-- all表示所有
select * from score
where c_no="3-105" and degree > all (select degree from score where c_no="3-245")
order by degree desc;-- 29、查询所有教师和同学的name、sex和birthday
-- union取并集 只要列数相同即可 与列的数据类型无关
select name,sex,birthday from teacher
union
select name,sex,birthday from student;-- 30、查询所有“女”教师和“女”同学的name、sex和birthday
select name,sex,birthday from teacher where sex="女"
union
select name,sex,birthday from student where sex="女";-- 31、查询成绩低于该课程平均成绩的同学的成绩表
select * from score;--把表分成两个,取别名[a]                        [b]
+------+-------+--------+   +------+-------+--------+
| s_no | c_no  | degree |   | s_no | c_no  | degree |
+------+-------+--------+   +------+-------+--------+
| 101  | 3-105 |     90 |   | 101  | 3-105 |     90 |
| 101  | 6-166 |     88 |   | 101  | 6-166 |     88 |
| 102  | 3-105 |     91 |   | 102  | 3-105 |     91 |
| 103  | 3-105 |     92 |   | 103  | 3-105 |     92 |
| 103  | 3-245 |     86 |   | 103  | 3-245 |     86 |
| 103  | 6-166 |     85 |   | 103  | 6-166 |     85 |
| 104  | 3-105 |     89 |   | 104  | 3-105 |     89 |
| 105  | 3-105 |     88 |   | 105  | 3-105 |     88 |
| 105  | 3-245 |     75 |   | 105  | 3-245 |     75 |
| 105  | 6-166 |     79 |   | 105  | 6-166 |     79 |
| 109  | 3-105 |     76 |   | 109  | 3-105 |     76 |
| 109  | 3-245 |     68 |   | 109  | 3-245 |     68 |
| 109  | 6-166 |     81 |   | 109  | 6-166 |     81 |
+------+-------+--------+   +------+-------+--------+   -- 像是遍历求平均 这个score a,score b巧妙!
select * from score a where degree < (select avg(degree) from score b where a.c_no = b.c_no); -- 这个题使用group by没法做-- 32、查询所有[任课]教师的name和Department
INSERT INTO score VALUES('109', '9-888', '80');-- 所有 课程编号
select distinct c_no from score;
-- 根据 课程编号 找 教师编号
select t_no from course where no in (select distinct c_no from score);
-- 根据 教师编号 找 教师所有信息-- 33、查询至少有2名男生的班号
select * from student where sex="男" group by class having count(*)>1;-- 33、查询student表中不姓王的同学记录
-- 模糊查询
select * from student where name not like "王%"; -- 34、查询student表中每个学生的姓名和年龄
select year(now());
select name,year(now())-year(birthday) as "年龄" from student;-- 34、查询student表中最大最小birthday
select birthday from student order by birthday;
-- 最大最小
select max(birthday) as "最大", min(birthday) as "最小" from student;
+------------+------------+
| 最大       | 最小       |
+------------+------------+
| 1977-09-01 | 1974-06-03 |
+------------+------------+-- 35、以[1班号]和[2年龄]从大到小的顺序查询student表中的全部数据
select * from student order by class desc,birthday;-- 38、查询“男”教师及其锁上的课程
select * from teacher where sex="男";
select * from course where t_no in(select no from teacher where sex="男");-- 39、查询最高分同学的学号、课程编号、成绩
-- 我的解答:题意看错了。查询了最高分同学的学号、班号、成绩。
select student.no,student.class,score.degree from student,score
where score.s_no=student.no
order by degree desc
limit 0,1;
--标准解答
select * from score
where degree = (select max(degree) from score);-- 40.查询和“李军”同性别的所有同学的姓名
select name from student where sex=(select sex from student where name="李军");-- 41、查询查询和“李军”同性别、同班的所有同学的姓名
select name from student
where sex = (select sex from student where name="李军")
and class = (select class from student where name="李军");-- 42、查询所有选修“计算机导论”课程的“男”同学成绩表
-- 两个条件:计算机导论、男
select * from score
where c_no = (select no from course where name="计算机导论") -- 计算机导论
and   s_no in (select no from student where sex = "男");      -- 男-- 43、假设使用如下面命令建立了一个grade表:
create table grade(low int(3),upp int(3),grade char(1)
);
insert into grade values (90,100,'A');
insert into grade values (80,89,'B');
insert into grade values (70,79,'C');
insert into grade values (60,69,'D');
insert into grade values (0,59,'E');
+------+------+-------+
| low  | upp  | grade |
+------+------+-------+
|   90 |  100 | A     |
|   80 |   89 | B     |
|   70 |   79 | C     |
|   60 |   69 | D     |
|    0 |   59 | E     |
+------+------+-------+
--查询所有同学的s_no、c_no、grade列
select s_no,c_no,grade from score,grade
where degree between low and upp;
+------+-------+-------+
| s_no | c_no  | grade |
+------+-------+-------+
| 101  | 3-105 | A     |
| 101  | 6-166 | B     |
| 102  | 3-105 | A     |
| 103  | 3-105 | A     |
| 103  | 3-245 | B     |
| 103  | 6-166 | B     |
| 104  | 3-105 | B     |
| 105  | 3-105 | B     |
| 105  | 3-245 | C     |
| 105  | 6-166 | C     |
| 109  | 3-105 | C     |
| 109  | 3-245 | D     |
| 109  | 6-166 | B     |
| 109  | 9-888 | B     |
+------+-------+-------+--连接查询-- 内连接
--                  inner join  或者  join-- 外连接
--  1)左连接       left join   或者  left outer join
--  2)右链接       right join  或者  right outer join
--  3)完全外连接     full lion   或者  full outer join
CREATE DATABASE testJoin;
CREATE TABLE person (id INT,                --持有卡号name VARCHAR(20), --人姓名cardId INT         --卡种类
);CREATE TABLE card (id INT,                --卡种类name VARCHAR(20)   --卡名称
);INSERT INTO card VALUES (1, '饭卡'), (2, '建行卡'), (3, '农行卡'), (4, '工商卡'), (5, '邮政卡');
INSERT INTO person VALUES (1, '张三', 1), (2, '李四', 3), (3, '王五', 6);
SELECT * FROM card;
SELECT * FROM person;-- 1)inner join 内联查询:两张表中的数据,通过某个字段相等,查询出相关记录数据
select * from person inner join card on person.cardId = card.id;
select * from person join card on person.cardId = card.id; -- 省略inner-- 2)left join 左外连接
-- 把左边表中所有数据取出来,而右边表中的数据,如果有就显示出来,如果没有就显示null
select * from person left outer join card on person.cardId = card.id;
select * from person left join card on person.cardId = card.id; -- 省略outer
+------+------+--------+------+--------+
| id   | name | cardId | id   | name   |
+------+------+--------+------+--------+
|    1 | 张三 |      1 |    1 | 饭卡   |
|    2 | 李四 |      3 |    3 | 农行卡 |
|    3 | 王五 |      6 | NULL | NULL   |
+------+------+--------+------+--------+-- 3)right join 右外连接
-- 把右边表中所有数据取出来,而左边表中的数据,如果有就显示出来,如果没有就显示null
select * from person right outer join card on person.cardId = card.id;
select * from person right join card on person.cardId = card.id; -- 省略outer
+------+------+--------+------+--------+
| id   | name | cardId | id   | name   |
+------+------+--------+------+--------+
|    1 | 张三 |      1 |    1 | 饭卡   |
|    2 | 李四 |      3 |    3 | 农行卡 |
| NULL | NULL |   NULL |    2 | 建行卡 |
| NULL | NULL |   NULL |    4 | 工商卡 |
| NULL | NULL |   NULL |    5 | 邮政卡 |
+------+------+--------+------+--------+-- 4)full join 全外连接
-- mysql不支持full join
select * from person full join card on person.cardId = card.id;-- unoin 实现full join
select * from person left join card on person.cardId = card.id
union
select * from person right join card on person.cardId = card.id;
+------+------+--------+------+--------+
| id   | name | cardId | id   | name   |
+------+------+--------+------+--------+
|    1 | 张三 |      1 |    1 | 饭卡   |
|    2 | 李四 |      3 |    3 | 农行卡 |
|    3 | 王五 |      6 | NULL | NULL   |
| NULL | NULL |   NULL |    2 | 建行卡 |
| NULL | NULL |   NULL |    4 | 工商卡 |
| NULL | NULL |   NULL |    5 | 邮政卡 |
+------+------+--------+------+--------+-- 事务
-- 在 MySQL 中,事务其实是一个最小的不可分割的工作单元。事务能够保证一个业务的完整性。
-- 比如银行转账:
-- a -> -100
UPDATE user set money = money - 100 WHERE name = 'a';
-- b -> +100
UPDATE user set money = money + 100 WHERE name = 'b';
-- 如果只有一条执行成功,另一条执行不成功,会出现数据前后不一致
-- 多条sql语句可能会有同时成功的要求(要么同时成功,要么同事失败)-- mysql如何控制事务?
-- 1)mysql默认开启事务
-- 查询事务的自动提交状态
SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            1 |
+--------------+
-- 自动提交的作用:当我们执行一条 SQL 语句的时候,其产生的效果就会立即体现出来,且不能回滚CREATE DATABASE bank;USE bank;CREATE TABLE user (id INT PRIMARY KEY,name VARCHAR(20),money INT
);INSERT INTO user VALUES (1, 'a', 1000);SELECT * FROM user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
+----+------+-------+
-- 可以看到,在执行插入语句后数据立刻生效,原因是 MySQL 中的事务自动将它提交到了数据库中
-- 那么所谓回滚的意思就是,撤销执行过的所有 SQL 语句,使其回滚到最后一次提交数据时的状态
-- 在 MySQL 中使用 ROLLBACK 执行回滚:
-- 回滚到最后一次提交
ROLLBACK;   -- 不能回滚 没效果
SELECT * FROM user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
+----+------+-------+-- 由于所有执行过的 SQL 语句都已经被提交过了,所以数据并没有发生回滚。那如何让数据可以发生回滚?
-- 关闭自动提交
SET AUTOCOMMIT = 0;-- 查询自动提交状态
SELECT @@AUTOCOMMIT;-- 将自动提交关闭后,测试数据回滚:
INSERT INTO user VALUES (2, 'b', 1000);-- 关闭 AUTOCOMMIT 后,数据的变化是在一张虚拟的临时数据表中展示,
-- 发生变化的数据并没有真正插入到数据表中。
SELECT * FROM user;-- 由于数据还没有真正提交,可以使用回滚
ROLLBACK;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
+----+------+-------+-- 那如何将虚拟的数据真正提交到数据库中?使用 COMMIT
INSERT INTO user VALUES (2, 'b', 1000);-- 手动提交数据(持久性),将数据真正提交到数据库中,执行后不能再回滚提交过的数据。
COMMIT;-- 提交后测试回滚
ROLLBACK;-- 再次查询(回滚无效了)
SELECT * FROM user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
|  2 | b    |  1000 |
+----+------+-------+-- 事务总结--查看自动提交状态:1自动 2手动SELECT @@AUTOCOMMIT          --设置手动提交:SET AUTOCOMMIT = 0 --手动提交(AUTOCOMMIT = 0)COMMIT;--事务回滚(AUTOCOMMIT = 0)ROLLBACK; -- 开启自动提交
SET AUTOCOMMIT = 1;-- 手动开启一个事务
begin;              -- 这样
start transaction;  -- 或者这样-- 示例1
begin;
UPDATE user set money = money - 100 WHERE name = 'a';
UPDATE user set money = money + 100 WHERE name = 'b';
rollback; -- 成功-- 示例2
start transaction;
UPDATE user set money = money - 100 WHERE name = 'a';
UPDATE user set money = money + 100 WHERE name = 'b';
ROLLBACK; -- 成功--让事务生效
commit;-- 总结:事务的四大特征:-- A 原子性:事务是最小的单位,不可以再分割;-- C 一致性:要求同一事务中的 SQL 语句,必须保证同时成功或者失败;-- I 隔离性:事务1 和 事务2 之间是具有隔离性的;-- D 持久性:事务一旦结束 ( COMMIT ) ,就不可以再返回了 ( ROLLBACK ) 

【MySQL数据库】一天学完MySQL笔记——纯SQL文档版相关推荐

  1. mysql数据库建仓范式_存mysql个数

    MySQL学习笔记之数据类型详解 注:以下内容针对MySQL5.0及以上版本 MySQL的数据类型非常多,选择正确的数据类型对于获得高性能至关重要,本文是我结合网上看到的一些blog加上<高性能 ...

  2. cmd命令操作Mysql数据库,命令行操作Mysql

    你需要先安装Mysql数据库,其实就是安装Mysql数据库服务器,然后设置环境变量path,在cmd.exe里查询查看环境变量参数的命令是:path 第二步就是连接Mysql服务器,命令如下:mysq ...

  3. php访问mysql数据库实验报告,php访问mysql数据库

    //建一个连接,造一个连接对象 $db = new MySQLi("localhost","root","123","mydb&q ...

  4. 视频教程-MySQL数据库应用快速入门培训课程-MySQL

    MySQL数据库应用快速入门培训课程 5年JAVA 开发经验,2年系统架构经验,PMP项目管理资格认证,ACP 项目管理认证,工作过程同时参与性能.自动化测试工作,负责测试部门的测试架构,项目服务器运 ...

  5. 【宋红康 MySQL数据库】【02】MySQL基本使用

    持续学习&持续更新中- 学习态度:守破离 [宋红康 MySQL数据库][02]MySQL基本使用 基本命令 启动 MySQL 服务命令 停止 MySQL 服务命令 登录MySQL 退出登录 查 ...

  6. linux mysql如何远程连接mysql数据库,Linux下远程连接MySQL数据库的方法

    Linux下远程连接MySQL数据库的方法 踩坑笔记 估计搞了一个多小时才把这个远程连接搞好.一台本地电脑,一台云服务器,都是linux系统. 步骤 1.在服务器端开启远程访问 首先进入mysql数据 ...

  7. mysql数据库优化课程---16、mysql慢查询和优化表空间

    mysql数据库优化课程---16.mysql慢查询和优化表空间 一.总结 一句话总结: a.慢查询的话找到存储慢查询的那个日志文件 b.优化表空间的话可以用optimize table sales; ...

  8. mysql数据库优化课程---15、mysql优化步骤(mysql中最常用最立竿见影的优化是什么)...

    mysql数据库优化课程---15.mysql优化步骤(mysql中最常用最立竿见影的优化是什么) 一.总结 一句话总结:索引优化最立竿见影 索引优化:不然有多少行要扫描多少次,1亿行大概是5到10分 ...

  9. mysql数据库导出最大值_4.6 MySQL数据库导入与导出攻略

    4.6 MySQL数据库导入与导出攻略 4.6.1 Linux下MySQL数据库导入与导出 1. MySQL数据库的导出命令参数 主要是通过两个mysql和mysqldump命令来执行 (1) MyS ...

最新文章

  1. 计算机动画课程设计,计算机动画课程设计.doc
  2. H. Texas hold'em Poker(2019ICPC区域网络赛沈阳站)
  3. 李善友《认知升级之第一性原理》--507张PPT全解!_搜狐科技_搜狐网
  4. 2010年下半年计算机专业技术资格考试工作安排
  5. 剑指offer(刷题61-65)--c++,Python版本
  6. mysql和oracle转换_转MySql 与Oracle区别
  7. 【C语言】第五章 迭代计算与循环结构 题解
  8. ExtJs六(ExtJs Mvc首页展示)
  9. android intent sender,Android7.0以上调PendingIntent.getIntent()报错
  10. C++关键字 explicit
  11. Juniper Junos与思科IOS对比配置逻辑合理性
  12. 交叉编译工具链的安装以及介绍
  13. imx6ul pinctrl 驱动浅析
  14. 『GDAL』读写TIFF文件
  15. 搜狗主动提交url并反馈快照更新软件(含源码)
  16. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B. Tomb Raider(二进制枚举)
  17. rxjs ajax query,rxjs入门之ajax封装
  18. Android Animation动画详解(二): 组合动画特效
  19. 复旦大学计算机学院教师简介,复旦大学计算机科学技术学院导师教师师资介绍简介-危辉...
  20. 选择工业相机需要知道的50个问题

热门文章

  1. HDU - 2795 Billboard(线段树)
  2. SENetSKNet 解读
  3. Dominant Indices(CF 1009 F)
  4. Word/Excel文档伪装病毒-kspoold.exe分析
  5. 对现有的所能找到的DDOS代码(攻击模块)做出一次分析----GET篇
  6. cocos2d-x游戏开发(二)开始菜单续
  7. 秒杀多线程第十六篇 多线程十大经典案例之一 双线程读写队列数据
  8. scrapy框架的概念和流程
  9. 深度好文:Netflix奈飞微服务架构设计解析
  10. 柴树杉:面向数据科学领域的新语言,Go+蓄势待发