MySQL多表操作

2021-07-30 14:39:54 字數 3857 閱讀 9576

//方式一

create

table stu(sid int

primary

key,sname varchar(20),age int);

//方式二

create

table stu(sid int,sname varchar(20),age int, primary

key(sid));

//方式三

create

table stu(sid int,sname varchar(20),age int);

alter

table stu add

primary

key(sid);

//刪除主鍵

alter

table stu drop

primary

key;

//1.從表

create table dept(

deptno int primary key,

dname varchar(20)

);insert into dept value(10,"研發部");

insert into dept value(20,"財務部");

insert into dept value(30,"運營部");

//2.主表

create table emp(

empno int primary key,

ename varchar(20),

deptno int,

constraint fk_emp_dept foreign key (deptno) references dept(deptno)

);

//1.從表

create table hasband(

hid int primary key,

hname varchar(20)

);select * from hasband;

insert into hasband value(1,"劉備");

insert into hasband value(2,"關羽");

insert into hasband value(3,"張飛");

//2.主表(主表的主鍵作為外來鍵)

create table wife(

wid int primary key,

wname varchar(20),

constraint fk_wife_hasband foreign key(wid) references hasband(hid)

);insert into wife value(1,"小喬");

insert into wife value(2,"貂蟬");

insert into wife value(3,"吳氏");

//學生表

create table student(

sid int primary key,

sname varchar(20)

);//老師表

create table teacher(

tid int primary key,

tname varchar(20)

);//關聯表

create table stu_tea(

sid int,

tid int,

constraint fk_stu foreign key(sid) references student(sid),

constraint fk_tea foreign key(tid) references teacher(tid)

);insert into student value(1,"老大");

insert into student value(2,"老二");

insert into student value(3,"老三");

insert into student value(4,"老四");

insert into student value(5,"老五");

insert into teacher value(1,"***");

insert into teacher value(2,"張老師");

insert into teacher value(3,"劉老師");

insert into teacher value(4,"武老師");

insert into stu_tea value(1,1);

insert into stu_tea value(2,1);

insert into stu_tea value(3,1);

insert into stu_tea value(4,1);

insert into stu_tea value(5,1);

insert into stu_tea value(1,2);

insert into stu_tea value(2,2);

insert into stu_tea value(3,2);

insert into stu_tea value(2,3);

insert into stu_tea value(3,3);

insert into stu_tea value(4,3);

insert into stu_tea value(5,3);

insert into stu_tea value(3,4);

insert into stu_tea value(4,4);

insert into stu_tea value(5,4);

合併結果集

連線查詢

內連線左外連線

右外連線

子查詢

create table aa(a int,b varchar(20));

create table bb(c int,d varchar(20));

//1.查詢全部

select * from aa

union all

select * from bb;

//2.去重複查詢

select * from aa

union all

select * from bb;

//1.方言

select *

from emp,dept

where emp.deptno = dept.deptno;

//2.標準

select *

from emp inner join dept

on emp.deptno = dept.deptno

//3.自然

select *

from emp natural join dept

select * 

from emp left outer join dept

on emp.deptno = dept.deptno

select * 

from emp right outer join dept

on emp.deptno = dept.deptno

左外連線與右外連線合併結果集去重複就是全外連線
//查詢工資高於諸葛亮的員工資訊

select *

from emp

where sal >(select sal from emp where ename="諸葛亮");

mysql的多表操作 MySQL多表操作

交叉連線 交叉連線 cross join 又稱笛卡爾連線 cartesian join 或叉乘 product 它是所有型別內連線的基礎。它把表看作是行記錄的集合,交叉連線即返回這兩個集合的笛卡爾積。這其實等價於內連線的連線條件始終為 真 或連線條件不存在。笛卡爾積引用自數學,在數學中,兩個集合x和...

MySql多表操作

今日任務 教學目標 1.為什麼要拆表 1.1表的準備 建立一張分類表 類別id,類別名稱.備註 類別id為主鍵並且自動增長 建立一張明星表 明星id,明星名稱,明星身價,明星年齡,明星性別,明星類別.備註 明星id為主鍵並且自動增長 2.引用完整性 表和表之間存在一種關係,但是這個關係需要誰來維護和...

MySQL多表操作

1 笛卡爾積 兩個集合的所有組合 select from 表1,表2 2 內連線查詢 隱式內連線 select from 表1,表2 where 表1.欄位1 表2.id select 表1.欄位1,表1,欄位2,表2.欄位1 from 表1,表2 where 表1.欄位1 表2.id select...