一: 修改表信息
1.修改表名
alter table student rename to student2
2.修改表注釋
alter table student comment '系統(tǒng)信息表'
二:修改字段信息
修改字段類型和注釋
alert table student modify column name varchar(50) NOT NULL comment 'XXX';
修改字段類型
alert table student modify column name int(11)
設(shè)置字段允許為空
alert table student modify column name NULL int(11)
增加一個字段,設(shè)好數(shù)據(jù)類型纤房,且不為空翻诉,添加注釋
alert table student add age int(1) NOT NULL comment '性別'
增加主鍵
alert table student add primary key (id)
增加自增主鍵
alert table student add sex int(int) auto_crement ,add primary key (idsex
修改字段名字(要重新指定該字段的類型)
alert table student change name app_name varchar(20) not null ;
刪除字段
alert table student drop name
在某個字段后增加字段
alert table student add column course int not null default 0 AFTER `id`碰煌;
調(diào)整字段順序
alert table student change course int not null default 0 AFTER `id`;
修改外鍵
1.刪除約束
ALTER TABLE ‘表名1’ DROP FOREIGN KEY ‘約束名’
2.增加約束
ALTER TABLE ‘表名1’ ADD CONSTRAINT '約束名' FOREIGN KEY (‘字段名’) REFERENCES '表名2' (‘字段名’)
三:外鍵
create table student
(
pk_id bigint unsigned not null auto_increment primary key,
uk_sno int(10) unsigned not null,
name char(60) not null,
sex char(10) not null,
class char(60) not null,
constraint uk_sno unique (sno)
)enige = InnoDB, charset = utf8
;
create table course
(
pk_id bigint unsigned not null auto_increment primary key,
uk_course_id int(10) unsigned not null,
course_name char(30) not null,
credit int not null,
constraint uk_course_id unique (course_id)
)enige = InnoDB, charset=utf8
;
create table score
(
pk_id bigint not null auto_increment primary key,
fk_sno int(10) unsigned not null,
fk_course_id int(10) unsigned not null,
result int not null,
constraint fk_sno foreign key (fk_sno) references <databasename>.student (sno),
constraint fk_course_id foreign key (fk_course_id) references <databasename>.course (course_id)
)enige = InnoDB, charset=utf8
;
我們插入數(shù)據(jù):
student表:
INSERT INTO student(uk_sno, name, sex, class) VALUES(123456, "spider_hgyi", "male", "cs");
crouse表:
INSERT INTO course(uk_course_id, course_name, credit) VALUES(1, "csapp", 10);
score表:
INSERT INTO score(fk_sno, fk_course_id, result) VALUES(123456, 1, 100);
好了,現(xiàn)在三個表里都已經(jīng)有了數(shù)據(jù)洪乍,現(xiàn)在我們嘗試更新學(xué)生表中學(xué)號的信息:
UPDATE student SET uk_sno=12345678 WHERE uk_sno=123456;
報(bào)錯:
(1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`bookmanager`.`score`, CONSTRAINT `fk_sno` FOREIGN KEY (`fk_sno`) REFERENCES `student` (`uk_sno`))')
我們在更新與刪除時遇到的外鍵約束解決方案分別對應(yīng)設(shè)置Update rule與Delete rule壳澳。有如下四個選項(xiàng):
1.CASCADE:從父表刪除或更新且自動刪除或更新子表中匹配的行茫经。
2.SET NULL:從父表刪除或更新行,并設(shè)置子表中的外鍵列為NULL抹镊。如果使用該選項(xiàng)荤傲,必須保證子表列沒有指定NOT NULL。
3.RESTRICT:拒絕對父表的刪除或更新操作氨菇。
4.NO ACTION:標(biāo)準(zhǔn)SQL的關(guān)鍵字,在MySQL中與RESTRICT相同乌询。
create table score
(
pk_id bigint not null auto_increment primary key,
fk_sno int(10) unsigned not null,
fk_course_id int(10) unsigned not null,
result int not null,
constraint fk_sno foreign key (fk_sno) references <databasename>.student (sno) on update cascade on delete cascade,
constraint fk_course_id foreign key (fk_course_id) references <databasename>.course (course_id) on update cascade on delete cascade
)enige = InnoDB, charset=utf8
mysql修改表結(jié)構(gòu) 外鍵
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門辩诞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纺涤,“玉大人撩炊,你說我怎么就攤上這事拧咳。” “怎么了骆膝?”我有些...
- 文/不壞的土叔 我叫張陵阅签,是天一觀的道長掐暮。 經(jīng)常有香客問我,道長政钟,這世上最難降的妖魔是什么路克? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮养交,結(jié)果婚禮上精算,老公的妹妹穿的比我還像新娘。我一直安慰自己碎连,他們只是感情好灰羽,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鱼辙,像睡著了一般廉嚼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上座每,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼分瘦!你這毒婦竟也來了嘲玫?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤土陪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后源哩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年耸袜,在試婚紗的時候發(fā)現(xiàn)自己被綠了句灌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片骗绕。...
- 正文 年R本政府宣布涝缝,位于F島的核電站嫩挤,受9級特大地震影響岂昭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恰矩,卻給世界環(huán)境...
- 文/蒙蒙 一萎胰、第九天 我趴在偏房一處隱蔽的房頂上張望技竟。 院中可真熱鬧熙尉,春花似錦检痰、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至堤瘤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間慎皱,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓献起,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 1.假如我們現(xiàn)在有個這樣的需求,我們公司有以下幾個部門帖旨,“研發(fā)部”货抄,“產(chǎn)品部”,“運(yùn)維部”琼梆,當(dāng)有新員工進(jìn)入的時候茎杂,...
- 背景:在實(shí)際操作表的時候,a表是主表恢恼,b表是外鍵表胶逢。當(dāng)我用delete去刪主表a的時候刪不掉.因?yàn)橛型怄I約束的原因...
- 基于Linux的MySQL操作實(shí)例(修改表結(jié)構(gòu),MySQL索引铐姚,MySQL數(shù)據(jù)引擎) 前言 本篇是基于Linux下...
- stackflow 上面找到一個不錯的答案,翻譯一下記錄下來。 Q:有三個表:regions,countries棺牧,...
- 為什么需要元數(shù)據(jù)鎖 如果一個查詢正在遍歷一個表中的數(shù)據(jù)巫糙,而執(zhí)行期間另一個會話對這個表結(jié)構(gòu)做變更,刪了一列颊乘,那么查詢...