sql

2021/11/9 2:09:50

本文主要是介绍sql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

常用

update user set password=password('123456')where user='root'; --修改密码
flush privileges; --刷新数据库权限

show databases; --显示所有数据库
use dbname; --打开某个数据库
create database [if not exists] 数据库名; --创建数据库
drop database [if exists] 数据库名; --删除数据库

show tables; --显示数据库 mysql 中所有的表
desc user; --显示表user 表的信息
create database name; --创建数据库
use databasename; --选择数据库

-- 单行注释
/* 多行注释 */

第三章

创建数据库

CREATE DATABASE 数据库名;

删除数据库

DROP DATABASE<name>

查询数据库

SHOW DATABASES;

查看表的结构

desc student;

创建表

USE 数据库名;

CREATE TABLE name(
    prj_num char(8) PRIMART KEY,
    prj_name varchar(50),
    start_date datetime,
    end_date datetime,
    prj_status bit,
);

表的修改

ALTER TABLE 表名 ADD 新列名 varchar(10); --添加列
ALTER TABLE 表名 DROP COLUMN 列名;  --删除列

删除表

DROP TABLE name;

查询数据库所有表

use 数据库名;

show tables;

查询

select * from 表名;--查询表;

查询列

select  sno FROM student; -- 查询student表的sno列

datediff 日期函数

datediff(datepart,date1,date2)--格式
例子
SELECT datediff(day,start_date,end_date) FROM 表名;

student表

sno    sname  ssex  sbirthday speiality tc
196001	董明霞	女	1999-03-10	通信	50
196002	李茜	女	1998-07-25	通信	54
196004	周俊文	男	1998-07-25	NULL	52
196005	张三	男	1999-08-25	计算机	55
196006	李四	男	2000-01-01	工商	55

往表中添加

insert into student 
values (196006,"李四","男","2000-01-01","工商",55);

数据记录增删改查

  1. 增加
insert into student 
values (196006,"李四","男","2000-01-01","工商",55);
  1. 删除数据
delete from student1 where tc="48";--删除了表student1中tc为48的一行;
  1. 修改数据
update student set speiality="计算机" where sno="196004";

修改后的表student

sno   |sname|ssex|sbirthday |speiality|tc|
------+-----+----+----------+---------+--+
196001|董明霞  |女   |1999-03-10|通信       |50|
196002|李茜    |女   |1998-07-25|通信       |54|
196004|周俊文  |男   |1998-07-25|计算机     |52|
196005|张三    |男   |1999-08-25|计算机     |55|
196006|李四    |男   |2000-01-01|工商       |55|

mysql 建表约束

  1. 主键约束:
    不重复且不为空
  • 联合主键
create table user(
    id int,
    name varchar(20),
    password varchar(20),
    primary key(id,name)--两个主键
    );

-- 插入数据
insert into user values(1,"张三","123");
insert into user values(2,"张三","123");--可以运行,因为是两个主键,两个主键都重复就不能运行了
  1. 外键约束

  2. 自增约束

  • 没有创建主键
alter table user add primary key(id);--添加主键
  • 删除主键
alter table user drop primary key;--删除
  • 修改主键
alter table user4 modify id int primary key;--使用modify修改字段,添加约束
  1. 外键约束
    b站
    https://www.bilibili.com/video/BV1Vt411z7wy?p=15&spm_id_from=pageDriver
  2. 唯一约束
  • 修饰的字段不能为空 NULL;
  1. 非空约束

  2. 默认约束

create table user1(
    id int,
    name varchar(20),
    age int default 10 -- 年龄默认为10
);
insert into user1 values(1,"zhangsan");--不写默认值,默认为10
insert into user1 values(1,"zhangsan",19);--写上19就把默认改为19
/*user1表
Field|Type       |Null|Key|Default|Extra|
-----+-----------+----+---+-------+-----+
id   |int        |YES |   |       |     |
name |varchar(20)|YES |   |       |     |
age  |int        |YES |   |10     |     |*/

第一范式

INF
数据表中所有子段都是不可分割的原子值

create table fanshi(
    id int primary key,
    name varchar(20),
    address varchar(30)
);
insert into fanshi values(1,"张三","中国河北省沧州市");
insert into fanshi values(2,"李四","中国河北省保定市");
insert into fanshi values(3,"王五","中国河北省石家庄");

/*id|name|address |
--+----+--------+
 1|张三  |中国河北省沧州市|
 2|李四  |中国河北省保定市|
 3|王五  |中国河北省石家庄|
 */

上面的表中adress还可以在分成中国,河北省,沧州市,就不满足第一范式.

create table fanshi(
    id int primary key,
    name varchar(20),
    cuntry varchar(30),
    privence varchar(30),
    city varchar(30),
);
insert into fanshi2 values(3,"王五","中国","河北省","邢台市");
insert into fanshi2 values(3,"王五","中国","河北省","邢台市");
insert into fanshi2 values(3,"王五","中国","河北省","邢台市");
/*表
id|name|cuntry|privence|ciey|
--+----+------+--------+----+
 1|张三  |中国    |河北省     |沧州市 |
 2|李四  |中国    |河北省     |保定市 |
 3|王五  |中国    |河北省     |邢台市 |*/

第二范式

  • 必须是满足第一范式的前提下,第二范式要求,除主键外的每一列都必须完全依赖于主键
  • 如果要出现不完全依赖,只可能发生在联合主键的情况下.
create table myorder(
	product_id int,
	customer_id int,
	product_name varchar(20),
	customer_name varchar(20),--不满足第二范式,因为customer_name只跟主键customer_id有关,与主键product_id无关 
	primary key(product_id,customer_id) 
);
分成三个表就行了

第三范式

  • 3NF
  • 必须满第二范式, 除开主键列的其他列之间不能有传递依赖关系.

只要非主键内部存在传递依赖,就不满足第三范式。

查询练习

创建表

  1. 学生表 student
create table student(
	sno varchar(20) primary key,
	sname varchar(20) not null,
	ssex varchar(10) not null,
	sbirthday datetime,
	class varchar(20)
);
insert into student values("101","曾华","男","1977-09-01"."95033");
/*sno|sname|ssex|sbirthday          |class|
---+-----+----+-------------------+-----+
101|曾华   |男   |1977-09-01 00:00:00|95033|
102|曾华2  |男   |1977-09-01 00:00:00|95031|
103|曾华3  |女   |1977-09-01 00:00:00|95033|
104|曾华4  |男   |1977-09-01 00:00:00|95033|
105|曾华5  |女   |1977-09-01 00:00:00|95031|
106|曾华6  |男   |1977-09-01 00:00:00|95031|
107|曾华7  |男   |1977-09-01 00:00:00|95033|
108|曾华8  |男   |1977-09-01 00:00:00|95031|
109|曾华9  |男   |1974-06-03 00:00:00|95031|*/
  1. 课程表 course
create table course(
 	cno varchar(20) primary key,
 	cname varchar(20) not null,
 	tno varchar(20) not null,
 	foreign key(tno) references teacher(tno) -- 外键
);
/*
cno  |cname|tno|
-----+-----+---+
3-105|计算机导论|825|
3-245|操作系统 |804|
6-166|数字电路 |856|
9-888|高等数学 |831|
  1. 成绩表 score
create table score(
	sno varchar(20) primary key,
	cno varchar(20) not null,
	degree decimal,
	foreign key(sno) references student(sno),
	foreign key(cno) references course(cno)
); 
/*
sno|cno  |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|
  1. 教师表 teacher
create table teacher(
	tno varchar(20) primary key,
	tname varchar(20) not null,
	tsex varchar(10) not null,
	tbirthday datetime,
	prof varchar(20) not null,
	depart varchar(20) not null
);
/*
tno|tname|tsex|tbirthday          |prof|depart|
---+-----+----+-------------------+----+------+
804|李诚   |男   |1958-12-02 00:00:00|副教授 |计算机系  |
825|王萍   |女   |1972-05-05 00:00:00|助教  |计算机系  |
831|刘冰   |女   |1977-08-14 00:00:00|助教  |电子工程系 |
856|张旭   |男   |1969-03-12 00:00:00|讲师  |电子工程系 |

练习

  1. 查询student表所有记录
select * from student;

2.查询 student 表中的 sname、ssex 和 class 字段的所有行

select sname,ssex,class from student;
  1. 查询 teacher 表中不重复的 department 列
-- distinct: 去重查询
select distinct depart from teacher;-- 去除teacher表中depart列重复的;
  1. 查询 score 表中成绩在60-80之间的所有行
select * from score where degree between 60 and 80;
--或者下面这条语句也可以查询出来 
-- and 表示并且
select * from score where degree >60 and degree <80;
/*sno|cno  |degree|
---+-----+------+
105|3-245|    75|
105|6-166|    79|
109|3-105|    76|
109|3-245|    68|*/
  1. 查询 score 表中成绩为 85, 86 或 88 的行
-- 或的关系用in
select * from score where degree in(85,86,88);
/*
sno|cno  |degree|
---+-----+------+
103|3-245|    86|
103|6-166|    85|
105|3-105|    88|*/
  1. 查询 student 表中 '95031' 班或性别为 '女' 的所有行
-- or表示或者
select * from student where class="95031" or ssex="女";
/*
sno|sname|ssex|sbirthday          |class|
---+-----+----+-------------------+-----+
102|曾华2  |男   |1977-09-01 00:00:00|95031|
103|曾华3  |女   |1977-09-01 00:00:00|95033|
105|曾华5  |女   |1977-09-01 00:00:00|95031|
106|曾华6  |男   |1977-09-01 00:00:00|95031|
108|曾华8  |男   |1977-09-01 00:00:00|95031|
109|曾华9  |男   |1974-06-03 00:00:00|95031|*/
  1. 以 class 降序的方式查询 student 表的所有行
-- DESC: 降序,从高到低
-- ASC(默认): 升序,从低到高
-- order by
select * from student order by class desc;
  1. 以 cno 升序、degree 降序查询 score 表的所有行
-- 条件靠前的优先级高
select* from score order by cno asc,degree desc;
  1. 查询 "95031" 班的学生人数
--统计 count
select count(*) from student where class="95031";
/*
count(*)|
--------+
       5|
  1. 查询 score 表中的最高分的学生学号和课程编号(子查询或排序查询)。
--子查询
select sno,cno,from score where degree=(select max(degree)from score);
--排序查询
-- 降序排序,
-- LIMIT r, n: 表示从第r行开始,查询n条数据
select sno,cno,degree from score order by degree desc limit 0,1;
/*
sno|cno  |
---+-----+
103|3-105|
  1. 查询每门课的平时成绩
-- avg()
select avg(degree) from score where cno="3-105" ;
/*
avg(degree)|
-----------+
    85.3333|
*/
select avg(degree) from score where cno="6-166" ;
/*
avg(degree)|
-----------+
    81.6667|*/
select avg(degree) from score where cno="3-245" ;

 写在一个sql语句中
-- group by 分组
select cno,avg(degree) from score group by cno;
/*
cno  |avg(degree)|
-----+-----------+
3-105|    85.3333|
3-245|    76.3333|
6-166|    81.6667|*/
  1. 查询 score 表中至少有 2 名学生选修,并以 3 开头的课程的平均分数。
-- -- LIKE 表示模糊查询,"%" 是一个通配符,匹配 "3" 后面的任意字符。
select cno,avg(degree),count(*) from score group by cno 
having count(cno)>=2 and cno like "3%";
/*
cno  |avg(degree)|count(*)|
-----+-----------+--------+
3-105|    85.3333|       3|
3-245|    76.3333|       3|*/

  1. 查询分数大于70小于90的sno列
select sno,degree from score 
where degree between 70 and 90;
或者
select sno,degree from score
where degree>=70 and degree<=90;
/*
sno|degree|
---+------+
103|    86|
103|    85|
105|    88|
105|    75|
105|    79|
109|    76|
109|    81|*/

多表查询

1. 查询所有学生的sname,cno,degree列

-- 来自于不同的表
select sname from student;
/*
sname|
-----+
曾华   |
曾华2  |
曾华3  |
曾华4  |
曾华5  |
曾华6  |
曾华7  |
曾华8  |
曾华9  |*/
select sno,sname from student;
/*
sno|sname|
---+-----+
101|曾华   |
102|曾华2  |
103|曾华3  |
104|曾华4  |
105|曾华5  |
106|曾华6  |
107|曾华7  |
108|曾华8  |
109|曾华9  |*/

select sno,cno,degree from score;
/*
sno|cno  |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|*/
通过分析俩表有共同的sno

select sname,cno,degree from student,score
where student.sno=score.sno;
/*sname|cno  |degree|
-----+-----+------+
曾华3  |3-105|    92|
曾华3  |3-245|    86|
曾华3  |6-166|    85|
曾华5  |3-105|    88|
曾华5  |3-245|    75|
曾华5  |6-166|    79|
曾华9  |3-105|    76|
曾华9  |3-245|    68|
曾华9  |6-166|    81|*/

2. 查询所有学生的sno,cname和degree列

-- 查找course表和score表;
select cno,cname from course;
/*
cno  |cname|
-----+-----+
3-105|计算机导论|
3-245|操作系统 |
6-166|数字电路 |
9-888|高等数学 |*/
select cno,sno,degree from score;
/*
cno  |sno|degree|
-----+---+------+
3-105|103|    92|
3-245|103|    86|
6-166|103|    85|
3-105|105|    88|
3-245|105|    75|
6-166|105|    79|
3-105|109|    76|
3-245|109|    68|
6-166|109|    81|*/

有相同的cno
select cname,sno,degree from course,score
where course.cno=score.cno;
/*
cname|sno|degree|
-----+---+------+
计算机导论|103|    92|
计算机导论|105|    88|
计算机导论|109|    76|
操作系统 |103|    86|
操作系统 |105|    75|
操作系统 |109|    68|
数字电路 |103|    85|
数字电路 |105|    79|
数字电路 |109|    81|*/

3. 查询所有学生的sname,cname和degree列

通过分析,三列分别来自来自三张表

select sname,cname,degree from score,student,course
where student.sno=score.sno 
and course.cno=score.cno;

/*
sname|cname|degree|
-----+-----+------+
曾华3  |计算机导论|    92|
曾华3  |操作系统 |    86|
曾华3  |数字电路 |    85|
曾华5  |计算机导论|    88|
曾华5  |操作系统 |    75|
曾华5  |数字电路 |    79|
曾华9  |计算机导论|    76|
曾华9  |操作系统 |    68|
曾华9  |数字电路 |    81|
*/

子查询

  1. 查询"95031"班学生每门课的平均分;
select cno,avg(degree)
from score 
where sno in (select sno from student where class="95031")
group by cno;
/*
cno  |avg(degree)|
-----+-----------+
3-105|    82.0000|
3-245|    71.5000|
6-166|    80.0000|
*/
  1. 查询在 3-105 课程中,所有成绩高于 109 号同学的记录。
-- 先查询出109同学,3-105的成绩
select * from score where sno= "109" and cno="3-105";
/*
sno|cno  |degree|
---+-----+------+
109|3-105|    76|*/
select * from score
 where cno="3-105"and  degree >(select degree from score where sno="109" and cno="3-105") ;
/*
sno|cno  |degree|
---+-----+------+
103|3-105|    92|
105|3-105|    88|

  1. 查询所有和 101 、108 号学生同年出生的 no 、name 、birthday 列。
-- YEAR(..): 取出日期中的年份
-- 先查找101,108学生的出生年份
select year(sbirthday) from student where sno in(101,108);
/*
year(sbirthday)|
---------------+
           1977|
           1977|*/

select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in(101,108) );
-- 因为条件查询的是两个年份,所以用in

多层嵌套子查询

  1. 查询 '张旭' 教师任课的学生成绩表。
-- 先查张旭的老师号
select tno from teacher where tname="张旭";
/*
tno|
---+
856|*/
-- 在通过tno查张旭的课程号
select cno from course where tno=(select tno from teacher where tname="张旭");
/*cno  |
-----+
6-166|*/
select * from score where cno=(select cno from course where tno=(select tno from teacher where tname="张旭"));
/*
sno|cno  |degree|
---+-----+------+
103|6-166|    85|
105|6-166|    79|
109|6-166|    81|*/

多表查询

  1. 查询某选修课程多于3个同学的教师姓名。
    https://www.bilibili.com/video/BV1Vt411z7wy?p=33

子查询

  1. 查询出"计算机系"教师所教课程的成绩表;
-- 先查询teacher表中的计算机系对应的tno
select tno from teacher where depart="计算机系";
/*
tno|
---+
804|
825|*/

-- 再查询course表计算机系对应的cno
select cno from course where tno in(select tno from teacher where depart="计算机系");
/*
cno  |
-----+
3-245|
3-105|*/
-- 通过cno找到所有的成绩
select * from score where cno in (select cno from course where tno in (select tno from teacher where depart="计算机系" ));
/*
sno|cno  |degree|
---+-----+------+
103|3-245|    86|
105|3-245|    75|
109|3-245|    68|
103|3-105|    92|
105|3-105|    88|
109|3-105|    76|*/

YEAR 与 NOW 函数

  1. 查询 student 表中每个学生的姓名和年龄。
-- 使用函数 YEAR(NOW()) 计算出当前年份,减去出生年份后得出年龄。
SELECT name, YEAR(NOW()) - YEAR(birthday) as age FROM student;
/*
sname|age|
-----+---+
曾华   | 44|
曾华2  | 44|
曾华3  | 44|
曾华4  | 44|
曾华5  | 44|
曾华6  | 44|
曾华7  | 44|
曾华8  | 44|
曾华9  | 47|*/

MAX 与 MIN 函数

  1. 查询 student 表中最大和最小的 sbirthday 值。
SELECT MAX(sbirthday), MIN(birthday) FROM student;
/*
MAX(sbirthday)     |MIN(sbirthday)     |
-------------------+-------------------+
1977-09-01 00:00:00|1974-06-03 00:00:00|*/

MAX 函数与子查询

1.查询最高分同学的 score 表。

-- 先查询score表的最高分
select max(degree) from score;
/*
max(degree)|
-----------+
         92|*/

-- 最高分可以有多个,本次查询刚好只有一个,没有并列的;
select * from score where degree = (select max(degree) from score);

/*
sno|cno  |degree|
---+-----+------+
103|3-105|    92|*/

下面把student表的sname改了,上面全是曾华

sno|sname|ssex|sbirthday          |class|
---+-----+----+-------------------+-----+
101|曾华   |男   |1977-09-01 00:00:00|95033|
102|匡明   |男   |1977-09-01 00:00:00|95031|
103|王丽   |女   |1977-09-01 00:00:00|95033|
104|李军   |男   |1977-09-01 00:00:00|95033|
105|王芳   |女   |1977-09-01 00:00:00|95031|
106|陆军   |男   |1977-09-01 00:00:00|95031|
107|王尼玛  |男   |1977-09-01 00:00:00|95033|
108|张全蛋  |男   |1977-09-01 00:00:00|95031|
109|赵铁柱  |男   |1974-06-03 00:00:00|95031|
110|张飞   |男   |1974-06-03 00:00:00|95038|

NOT LIKE 模糊查询取反

  1. 查询 student 表中不姓 "王" 的同学记录。
select * from student where sname not like "王%";
/*
sno|sname|ssex|sbirthday          |class|
---+-----+----+-------------------+-----+
101|曾华   |男   |1977-09-01 00:00:00|95033|
102|匡明   |男   |1977-09-01 00:00:00|95031|
104|李军   |男   |1977-09-01 00:00:00|95033|
106|陆军   |男   |1977-09-01 00:00:00|95031|
108|张全蛋  |男   |1977-09-01 00:00:00|95031|
109|赵铁柱  |男   |1974-06-03 00:00:00|95031|
110|张飞   |男   |1974-06-03 00:00:00|95038|*/

多段排序

  1. 以 class 和 sbirthday 从大到小的顺序查询 student 表。
-- desc降序 asc(默认)升序一般不用写
-- 不写默认为升序
select * from student order by class desc,sbirthday;
/*
sno|sname|ssex|sbirthday          |class|
---+-----+----+-------------------+-----+
110|张飞   |男   |1974-06-03 00:00:00|95038|
103|王丽   |女   |1976-02-20 00:00:00|95033|
107|王尼玛  |男   |1976-02-20 00:00:00|95033|
104|李军   |男   |1977-02-20 00:00:00|95033|
101|曾华   |男   |1977-09-01 00:00:00|95033|
106|陆军   |男   |1974-06-03 00:00:00|95031|
109|赵铁柱  |男   |1974-06-03 00:00:00|95031|
105|王芳   |女   |1975-02-10 00:00:00|95031|
102|匡明   |男   |1975-10-02 00:00:00|95031|
108|张全蛋  |男   |1975-10-02 00:00:00|95031|
*/

子查询

  1. 查询和"李军"同性别的所有同学sname;
-- 首先将李军的性别作为条件取出来
select ssex from student where sname="李军";
/*
ssex|
----+
男   |*/
根据性别查询 sname 和 ssex
select sname, ssex from student 
where ssex =(select ssex from student where sname="李军");
/*
sname|ssex|
-----+----+
曾华   |男   |
匡明   |男   |
李军   |男   |
陆军   |男   |
王尼玛  |男   |
张全蛋  |男   |
赵铁柱  |男   |
张飞   |男   |*/
  1. 查询和 "李军" 同性别且同班的同学 sname 。
-- 上面题把和李军同性别的找出来了,这里用上 and:并且
select sname, ssex from student 
where ssex =(select ssex from student where sname="李军")
and class=(select class from student where sname="李军");
/*sname|ssex|
-----+----+
曾华   |男   |
李军   |男   |
王尼玛  |男   |*/
  1. 查询所有选修 "计算机导论" 课程的 "男" 同学成绩表。
select * from student where ssex= "男";
/*
sno|sname|ssex|sbirthday          |class|
---+-----+----+-------------------+-----+
101|曾华   |男   |1977-09-01 00:00:00|95033|
102|匡明   |男   |1975-10-02 00:00:00|95031|
104|李军   |男   |1977-02-20 00:00:00|95033|
106|陆军   |男   |1974-06-03 00:00:00|95031|
107|王尼玛  |男   |1976-02-20 00:00:00|95033|
108|张全蛋  |男   |1975-10-02 00:00:00|95031|
109|赵铁柱  |男   |1974-06-03 00:00:00|95031|
110|张飞   |男   |1974-06-03 00:00:00|95038|*/
select * from course where cname="计算机导论";
/*cno  |cname|tno|
-----+-----+---+
3-105|计算机导论|825|*/

select * from score 
where cno=(select cno from course where cname="计算机导论")
and 
sno in (select sno from student where ssex= "男");
  1. 查询 "男" 教师及其所上的课程
Select * from teacher where tsex="男";
/*
tno|tname|tsex|tbirthday          |prof|depart|
---+-----+----+-------------------+----+------+
804|李诚   |男   |1958-12-02 00:00:00|副教授 |计算机系  |
856|张旭   |男   |1969-03-12 00:00:00|讲师  |电子工程系 |*/

--只需要tno就行了,有两个需要用in
select * from course where tno in (select tno from teacher where tsex="男");
/*cno  |cname|tno|
-----+-----+---+
3-245|操作系统 |804|
6-166|数字电路 |856|*/

连接查询 内连接,左连接,有连接

  1. 内连接
inner join 或者 join
  1. 外连接
  • 左连接
left join 或者 left outer join 
  • 右连接
right join 或者 right outer join
  • 完全外连接
full join 或者 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, '邮政卡');
SELECT * FROM card;
/*
+------+-----------+
| id   | name      |
+------+-----------+
|    1 | 饭卡      |
|    2 | 建行卡    |
|    3 | 农行卡    |
|    4 | 工商卡    |
|    5 | 邮政卡    |
+------+-----------+*/

INSERT INTO person VALUES (1, '张三', 1), (2, '李四', 3), (3, '王五', 6);
SELECT * FROM person;
/*
+------+--------+--------+
| id   | name   | cardId |
+------+--------+--------+
|    1 | 张三   |      1 |
|    2 | 李四   |      3 |
|    3 | 王五   |      6 |
+------+--------+--------+*/
  1. inner join查询(内连接)
-- inner join: 表示为内连接,将两张表拼接在一起。
-- on: 表示要执行某个条件。
select * from person inner join card on person.cardId= card.id;
/*
id|name|cardId|id|name|
--+----+------+--+----+
 1|张三  |     1| 1|饭卡  |
 2|李四  |     3| 3|农行卡|*/

 -- 将 inner 关键字省略掉,结果也是一样的。
-- select * from person JOIN card on person.cardId = card.id;
  1. left join(左外连接)

完整显示左边的表 (person) ,右边的表如果符合条件就显示,不符合则补 NULL 。

-- left join 也叫做 left out join,用这两种方式的查询结果是一样的。
select * from person left join card on person.cardId=card.id;
/*
id|name|cardId|id|name|
--+----+------+--+----+
 1|张三  |     1| 1|饭卡  |
 2|李四  |     3| 3|农行卡 |
 3|王五  |     6|NULL| NULL|*/

  1. right join(右外连接)

完整显示右边的表 ( card ) ,左边的表如果符合条件就显示,不符合则补 NULL 。

select * from person right join card on person.cardId = card.id;
/*
id|name|cardId|id|name|
--+----+------+--+----+
 1|张三  |   1  | 1|饭卡   |
 2|李四  |     3| 3|农行卡 |
 NULL|NULL|NULL| 2|建行卡 |
 NULL|NULL|NULL| 4|工商卡 |
 NULL|NULL|NULL| 5|邮政卡 |
  1. 全外连接
-- MySQL 不支持full join这种语法的全外连接

-- MySQL全连接语法,使用 UNION 将两张表合并在一起。
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|邮政卡 |*/

视图

-- 创建视图
create view v1 as select * from course where cname="计算机导论";-- v1是视图的名字
-- 使用视图
select * from v1;
/*
cno  |cname|tno|
-----+-----+---+
3-105|计算机导论|825|*/

-- 删除视图
drop view v1;
-- 修改视图
-- 方法一
create or replace view v1 as 查询语句; -- 如果有视图修改视图,没有视图建一个视图 
-- 方法二
alter view v1 as 查询语句;--

事务

没讲以后所有时间再看
b站:https://www.bilibili.com/video/BV1Vt411z7wy?p=57



这篇关于sql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程