SQL Server在一个表中插入多个外键

时间:2016-07-03 07:40:12

标签: sql sql-server insert foreign-keys

所以我对SQL很新,目前正在学习,只是搞乱基本数据库来实现优化/高效功能等。我在数据库中有3个表,其中一个表包含2个引用的外键数据库中的其他2个表。我不确定如何INSERT SELECT同时NOT NULL进入这些列,因为它们都是CREATE TABLE。我知道我可以用不同的方式设计数据库,但我很确定它可能,所以我想弄明白。以下是CREATE TABLE engine_info ( e_id INT IDENTITY(1,1) NOT NULL, e_year DATE NOT NULL, e_mark NVARCHAR(10) NOT NULL, e_size DECIMAL(1,1) NOT NULL, e_gearbox NVARCHAR(10) NOT NULL, e_gearratio DECIMAL(5,5) NULL, e_power INT NOT NULL, CONSTRAINT pk_einfo PRIMARY KEY (e_id) ); CREATE NONCLUSTERED INDEX nc1_einfo ON engine_info(e_year, e_mark, e_size, e_power); CREATE TABLE chassis_info ( c_id INT IDENTITY(1,1) NOT NULL, suspension NVARCHAR(10) NOT NULL, axle_size NVARCHAR(10) NOT NULL, caliper_size NVARCHAR(10) NOT NULL, disk_size NVARCHAR(10) NOT NULL, CONSTRAINT pk_cinfo PRIMARY KEY (c_id) ); CREATE NONCLUSTERED INDEX nc1_cinfo ON chassis_info(caliper_size, disk_size); CREATE TABLE manufacturer ( m_id INT IDENTITY(1,1) NOT NULL, manufacturer NVARCHAR(30) NULL, model NVARCHAR(30) NULL, e_id INT NOT NULL, c_id INT NOT NULL, CONSTRAINT pk_minfo PRIMARY KEY (m_id), CONSTRAINT fk1_minfo FOREIGN KEY (e_id) REFERENCES engine_info (e_id), CONSTRAINT fk2_minfo FOREIGN KEY (c_id) REFERENCES chassis_info (c_id) ); CREATE NONCLUSTERED INDEX nc1_minfo ON manufacturer(manufacturer, model); GO 语句:

{{1}}

0 个答案:

没有答案