mysql修改表结构 外键

2019-08-11  本文已影响0人  一生悬命Cat
一: 修改表信息

1.修改表名 
   alter table student rename to student2
2.修改表注释
  alter table student comment '系统信息表'

二:修改字段信息

修改字段类型和注释
alert table student modify column name varchar(50) NOT NULL comment 'XXX';
修改字段类型
alert table student modify column name int(11)
设置字段允许为空
alert table student modify column name NULL int(11)
增加一个字段,设好数据类型,且不为空,添加注释
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`;
调整字段顺序 
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
;
我们插入数据:
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);

好了,现在三个表里都已经有了数据,现在我们尝试更新学生表中学号的信息:

UPDATE student SET uk_sno=12345678 WHERE uk_sno=123456;

报错:
(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`))')

我们在更新与删除时遇到的外键约束解决方案分别对应设置Update rule与Delete rule。有如下四个选项:

1.CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行。 
2.SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL。如果使用该选项,必须保证子表列没有指定NOT NULL。 
3.RESTRICT:拒绝对父表的删除或更新操作。 
4.NO ACTION:标准SQL的关键字,在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


上一篇下一篇

猜你喜欢

热点阅读