日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql关系表控制_mysql表关系

發布時間:2023/11/27 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql关系表控制_mysql表关系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、表的詳細操作

1.修改表名

alter table 舊表名 rename 新表名;

?2.修改表的引擎與字符編碼

alter table 表名 engine="引擎名" ch

arset="編碼名";

?3.復制表 *

#結構

create table 新表名 like 舊表名;

eg:1create table nt like tt;#將tt的表結構復制到新表nt中, 約束條件一并復制

eg:2create table nt1 select* from tt where 1=2; #將tt的表結構復制到新表nt1中, 約束條件不會復制

?#結構+數據

create table 新表名 select * from舊表名;

注: 會復制表結構+數據, 但不會復制約束條件

?4.清空表

truncate 表名;

注:表被重置,自增字段重置

二、表中字段的詳細操作

create table t2(

id int primary key auto_increment,

x int,

y int

);

insert into t2(x, y) values(10, 20), (100, 200), (1000, 2000);

?1.修改字段信息 #modify

alter table 表名 modify 字段名 類型[(寬度) 約束];

alter table t2 modify x bigint default 0;#模式不同, 涉及精度問題 default設置默認值

?2.修改字段名及信息 #change

alter table 表名 change 舊字段名 新字段名 類型[(寬度) 約束];

alter table t2 change y c char(10) not null; #模式不同, 涉及類型轉換問題

?3.添加字段名 #alter add#末尾添加

alter table 表名 add 字段名 類型[(寬度) 約束], ..., add 字段名 類型[(寬度) 約束];

alter table t2 add age int, add gender enum("male", "female", "wasai") default "wasai";

?#首尾添加

alter table 表名 add 字段名 類型[(寬度) 約束] first;

?#指定位添加:指定字段后

alter table 表名 add 字段名 類型[(寬度) 約束] after 舊字段名;

alter table t2 add y int after x;

?4.刪除字段名 #drop

alter table 表名 drop 字段名;

alter table t2 drop y;

三、特殊表(mysql.user) 用戶管理

#操作前提:登錄root用戶

?1.重要字段

Host| User |Password

?2.新建用戶

create user 用戶名@主機名 identified by'密碼'; #正確

create user zero@localhost identified by 'zero';

?

注:insert into mysql.user(Host,User,Password) values("主機名","用戶名",password("密碼")); #錯誤

?3.設置用戶權限

grant 權限們 on 數據庫名.表名 to 用戶名@主機名 [with grant option];

grant create on db1.*to zero@localhost with grant option;

?

注:權限有select,delete,update,insert,drop..., all代表所有權限

注:數據庫名,表名可以用*替換,代表所有

注:設置權限時如果沒有當前用戶,會自動創建用戶,提倡使用

重點: grant all on db1.* to owen@localhost identified by 'owen'; #(創建用戶)設置權限

?4.撤銷權限

revoke 權限名 on 數據庫名.表名from用戶名@主機名;

revoke delete on db1.* fromowen@localhost;

?5.修改密碼

set passwordfor 用戶名@主機名 = password('新密碼');

set passwordfor owen@localhost = password('123');

?6.刪除用戶

drop user 用戶名@主機名;

四、表關系

社會中存儲需要可以構建成表的數據, 它們形成的表,往往之間存儲某種或某些社會關系,

mysql數據庫建立表結構就是社會中產生的各種數據, 分門別類管理;

但mysql建立的(代碼層次的)表之間, 同樣需要處理表與表之間的關系;

形成了 多對一 | 多對多 | 一對一 三種關系

1、多對一

案例:員工employees表 |部門department表

?

建表規則:

先建立主表,再建立從表,在從表中設置主表的唯一字段(通常為主鍵)作為外鍵

?

建表語法:

create table 主表(

id int primary key auto_increment,

...

);#創建主表實例

create table dep(

id int primary key auto_increment,

name varchar(16),

work varchar(16)

);

?

建從表語法:

create table 從表(

id int primary key auto_increment,

...

主表_id int,#只是在從表中起了一個名字, 該名字和主表主鍵對應,所有起了個見名知義的名字

foreign key(主表_id) references 主表(唯一字段名id)

on update cascade

on delete cascade

);#創建從表實例

create table emp(

id int primary key auto_increment,

name varchar(16),

salary float,

dep_id int,

foreign key(dep_id) references dep(id)

on update cascade#設置級聯

on delete cascade

);

?

插入記錄規則:

先插入主表數據,再插入從表數據

insert into dep values(1, '市場部', '銷售'), (2, '教學部', '授課');

?

insert into emp(name, salary, dep_id) values('egon', 3.0, 2),('yanghuhu', 2.0, 2),('sanjiang', 10.0, 1),('owen', 88888.0, 2),('liujie', 8.0, 1);

?

更新刪除數據:

兩表間相互影響,先從依賴數據入手,再進行更新刪除操作

eg:1刪除主表dep中一個部門

deletefrom dep where id=1; =>從表emp中屬于該部門的員工都被刪除了

?

更新從表emp中一個員工的部門

update emp set dep_id=3 where name='egon'; <=部門必須存在

insert into dep values(3, '管理部', '吃飯睡覺打豆豆, 明確團隊方針');

2、多對多

案例:作者author表 |書book表

?

建表規則:

新建第三張表,通過兩個外鍵形成多對多關系

?

建表語法:

create table 表1(

id int primary key auto_increment,

...

);

create table book(

id int primary key auto_increment,

name varchar(16),

price int

);

create table 表2(

id int primary key auto_increment,

...

);

create table author(

id int primary key auto_increment,

name varchar(16)

);

create table 關系表(

id int primary key auto_increment,

表1_id int,

表2_id int,

foreign key(表1_id) references 表1(id)

on update cascade

on delete cascade,

foreign key(表2_id) references 表2(id)

on update cascade

on delete cascade

);

create table book_author(

id int primary key auto_increment,

book_id int,

author_id int,

foreign key(book_id) references book(id)

on update cascade

on delete cascade,

foreign key(author_id) references author(id)

on update cascade

on delete cascade

);

3、一對一

案例:丈夫husband表 |妻子wife表

?

建表規則:

未存放外鍵的表被依賴,稱之為左表;存放外鍵的表示依賴表,稱之為右表;先操作左邊再操作右表

?

建表語法:

create table 左表(

id int primary key auto_increment,

...

);

create table husband(

id int primary key auto_increment,

name varchar(16)

);

create table 右表(

id int primary key auto_increment,

...

左表_id int unique,#一對一的外鍵需要唯一性

foreign key(左表_id) references 左表(id)

on update cascade

on delete cascade

);

create table wife(

id int primary key auto_increment,

name varchar(16),

husband_id int unique,#一對一的外鍵需要唯一性

foreign key(husband_id) references husband(id)

on update cascade

on delete cascade

);

總結

以上是生活随笔為你收集整理的mysql关系表控制_mysql表关系的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:mysql关系表控制_mysql表关系