首页 > 试题广场 >

在audit表上创建外键约束,其emp_no对应employ

[编程题]在audit表上创建外键约束,其emp_no对应employ
  • 热度指数:106464 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
在audit表上创建外键约束,其emp_no对应employees_test表的主键id。
(以下2个表已经创建了)
CREATE TABLE employees_test(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);

CREATE TABLE audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL
);

后台会判断是否创建外键约束,创建输出1,没创建输出0

示例1

输入

drop table if exists audit;
drop table if exists employees_test;
CREATE TABLE employees_test(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

CREATE TABLE audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL
);

输出

1
DROP TABLE audit;
CREATE TABLE audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL,
    FOREIGN KEY(EMP_no) REFERENCES employees_test(ID)
);
发表于 2020-11-10 23:32:07 回复(1)
alter table audit add constraint emp_nofk foreign KEY(emp_no) REFERENCES employees_test(id)
注意:emp_nofk这个是自定义的外键名称
发表于 2021-07-17 13:31:44 回复(1)
我是在自己电脑的终端进行交互式写的mysql,因为之前已经建立了表audit,所以直接用的add constraint.....foreign key.....references ...
alter table audit add constraint fk_a_et foreign key(emp_no)
    -> references employees_tes
可以通过。
发表于 2019-10-08 17:15:46 回复(1)

mysql实现:

ALTER TABLE audit ADD FOREIGN KEY (emp_no) REFERENCES employees_test (id)
发表于 2017-08-03 20:39:56 回复(2)
考点:创建外键的基本语法
alter table audit 
add constraint foreign key (emp_no) 
references employees_test (id);


发表于 2021-03-27 15:34:53 回复(0)
MySQL里面:   alter table audit add foreign key(EMP_no) references employees_test(ID); 
SQLite只能首先删除表,然后再新建表的时候添外键约束。
发表于 2019-02-21 11:42:22 回复(0)
mysql实现:
ALTER TABLE audit ADD FOREIGN KEY (emp_no) REFERENCES employees_test (id)
sqlite实现:

drop table audit;
 
CREATE TABLE audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL,
  FOREIGN KEY(EMP_no) REFERENCES employees_test(ID)
);


发表于 2020-11-07 14:42:40 回复(0)
drop table audit;
CREATE TABLE audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL,
    foreign key(EMP_no) references employees_test(id));
先drop table ,再创建带有外键约束的table。
发表于 2017-08-31 10:32:44 回复(0)
alter table 表名 add constraint 外键约束名 foreign key(列名) references 引用外键表
语言运行环境:Sql(mysql 8.0)
alter table audit 
add constraint cs foreign key(EMP_no)
references employees_test(id);


发表于 2021-01-25 13:37:13 回复(0)
--sql语句创建表的同时添加外键约束 CREATE TABLE tb_UserAndRole  --用户角色表
(
  ID INT PRIMARY KEY IDENTITY(1,1),
  UserID INT NOT NULL,--用户ID
  RoleID INT NOT NULL,--角色ID
  foreign key(UserID) references tb_Users(ID)--tb_Users表的ID作为tb_UserAndRole表的外键 ) 

   --2、添加外键约束(关联字段要用括号括起来)

   -- ALTER TABLE 从表

   -- ADD CONSTRAINT 约束名 FOREIGN KEY (关联字段) references 主表(关联字段);

   --例如:

ALTER TABLE tb_UserAndRole

   ADD CONSTRAINT FK__tb_UandR_Role FOREIGN KEY (RoleID) references tb_Role(ID);

发表于 2021-10-28 09:41:33 回复(0)
我有个疑问,假设audit中本来就有很多数据,那么删除表之后数据不也丢了吗?
发表于 2018-10-09 09:34:23 回复(1)
新知识点:alter table 表名 add foreign key(字段名) references 表名(唯一的字段)
alter table audit add foreign key(emp_no) references employees_test(id)

发表于 2023-04-28 17:59:00 回复(0)
alter table audit add constraint fk_emp_no foreign key (emp_no) references employees_test (id) 
发表于 2022-04-17 19:14:34 回复(0)
-- foregin key(key) references table(xx)
drop table audit;
create table audit(
    emp_no int not null,
    create_date datetime not null,
    foreign key(emp_no) references employees_test(id)
);
发表于 2020-07-20 11:22:00 回复(0)

alter table audit add constraint foreign_emp_no foreign key(emp_no) references employees_test(id);

发表于 2025-04-06 18:52:12 回复(0)
alter table audit
add foreign key fk_audit_employees_test (emp_no)
references employees_test (id)
on update cascade
on delete no action
发表于 2025-03-16 22:00:05 回复(0)
alter table audit add constraint fk_id foreign key(emp_no) references employees_test(id)

发表于 2024-11-08 15:30:10 回复(0)
alter table `audit`add foreign key (`emp_no`) references`employees_test`(`id`) on update cascade  on delete no action;
发表于 2024-09-13 18:26:53 回复(0)
alter table audit add constraint foreign key(emp_no) references employees_test(id)
发表于 2024-07-29 22:15:47 回复(0)
alter table `audit` add foreign key(`EMP_no`) references `employees_test`(`ID`);

发表于 2024-07-18 11:09:54 回复(0)