数据库SQL实践46:在audit表上创建外键约束,其emp_no对应employees_test表的主键id

思路:

由于SQLite中不能通过 alter table ... add foreign key ... references ... 语句来对已创建好的字段创建外键,因此只能先删除表,再重新建表的过程中创建外键

drop table audit;
create table audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL,
    foreign key(EMP_no) references employees_test(ID));

mysql可以这样写:

alter table audit add foreign key(EMP_no) references employees_test(ID);

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/84783163