mysql修改表結(jié)構(gòu) 外鍵

一: 修改表信息

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)
增加一個(gè)字段,設(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
在某個(gè)字段后增加字段
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)在三個(gè)表里都已經(jīng)有了數(shù)據(jù),現(xiàn)在我們嘗試更新學(xué)生表中學(xué)號的信息:

UPDATE student SET uk_sno=12345678 WHERE uk_sno=123456;

報(bào)錯(cuò):
(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`))')

我們在更新與刪除時(shí)遇到的外鍵約束解決方案分別對應(yīng)設(shè)置Update rule與Delete rule。有如下四個(gè)選項(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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容