关系型数据库(八)SQL

多表关联

因为大部分时候结果不在一张表中,所以需要多表查询,也称为关联查询,指两个或更多个表一起完成查询操作。

select a.student_name,b.score from student a,score b   where  a.student_no = b.student_no

 a b是别名,让a表学生表的学号=b表分数表的学号相等,取出a表中的学生名字,b表中的分数

练习 ,将department 部门表和员工表employee表相关联

查询结果显示,员工名称,员工年龄, 部门id,部门名称,

内连接查询

用左边表的记录去匹配右边表的记录,如果符合条件的则显示。

1.隐式内连接

看不到 join 关键字,条件使用where指定

select 列名 from 左表 [左表别名], 右表 [右表别名] where 主表.主键=从表.外键;

  • 相当于查询学生表,分数表的交集数据;
  • 使用where条件消除无效数据;
  • 字段要由对应的表名或别名调用。

2.显示内连接

使用 inner join ... on 语句,可以省略 inner;

select 列名 from 左表 inner join 右表 on 主表.主键=从表.外键;、

select * from student a inner join score on a.student_no=score.student_no;

注意一般不用谢inner join ,相当与=

外连接

左外连接

张三这个学生学号是4,但是成绩表里没有学号4的成绩,但是要求显示全部学生的名单,和成绩怎么办

select * from student a left join score on a.student_no=score.student_no;
特点:需要指定一张查询主表,查询主表的记录必须全部显示,即使不符合表关联的条件

左外连接:查询的数据以左表为准,即使在其他表中没有匹配的记录也会显示出来。

  • 使用 left outer join on,outer可以省略
  • 相当于查询A表所有数据和交集部分数据
  • select 列名 from 左表 left join 右表 on 表连接条件

右外连接

右外连接:查询的数据以右表为准,即使在其他表中没有匹配的记录也会显示出来。

使用right join ... on

下面这个例子,用来显示所有有成绩的学生名单,没有成绩的将不显示

select * from student a right join score on a.student_no=score.student_no;

update 更新语句

UPDATE  表名 SET  <列名>=<表达式>,<列名>=<表达式>  [WHERE 条件]

update 表名 set 字段名=值;

update student set student_name='王刚1'  where student_no=1

练习: 部门表department  名称为销售部的money改为1200

插入语句 insert

语法

insert into 数据表名(字段,字段,字段) values (值,值,值);

insert into 数据表名 value(值,值,值);

insert into   student  (student_no) values (8)

insert into   student  (student_no,student_name) values (9,'李四')

练习使用insert 语句给department查入一条数据

删除语句

DELETE FROM 表名 [ WHERE 条件]

delete from student where student_name ='李四'

删除employee中id为5的记录

创建表

根据已有的表创建新表
1.create table tab_new like tab_old (使用旧表创建新表)

create table student1 like student

2.create table tab_new as select col1,col2… from tab_old

create table tab_new as select student_no from student

create table student1(id int,name char(10),age int,sex char(5));

数据表添加列

alter table student1 add height int(10);

数据表删除列

alter table student1 drop height;

数据列修改数据类型

alter table student modify column high char(10);

修改表名

alter table student1 rename to student_table;

删除新表

drop table tabname

drop table tab_new

创建数据库

CREATE TABLE students33 (

    id INT PRIMARY KEY,

    name VARCHAR(32),

    age INT,

    gender VARCHAR(16)

);

子查询多表查询

SELECT子查询:子查询可以作为SELECT语句的一部分,用于在查询结果中生成一个或多个列

SELECT column1, column2, (SELECT COUNT(*) FROM table2) AS count FROM table1;

SELECT student_no, student_name, (SELECT COUNT(*) FROM student) AS count FROM student;

FROM子查询:子查询可以作为FROM子句的一部分,用于在外部查询中引用一个虚拟表

SELECT * FROM (SELECT column1, column2 FROM table1) AS subquery;

SELECT * FROM (SELECT student_name FROM student) AS subquery;

INSERT子查询:子查询可以作为INSERT语句的一部分,用于将子查询的结果插入到目标表中

INSERT INTO table1 (column1, column2) SELECT column1, column2 FROM table2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定谔的猫1981

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值