且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sql中创建外键

更新时间:2023-01-30 13:50:26

 创建 员工

employeeId int not null
employeeUsername varchar 50 null
employeePassword varchar 50 null
employeeName varchar 50 null
employeeContact int null
employeeEmail varchar 30 null
约束 PK_Employee PRIMARY KEY (employeeId)


创建 表格艺术家
{
artistId INT NOT NULL CONSTRAINT FK_artist_artistId
FOREIGN KEY REFERENCES 员工(employeeId), - ON DELETE CASCADE如果你想
artistUsername varchar 50 null
a rtistPassword varchar 50 null
artistName varchar 50 null
artistContactno int null
artistAddress varchar 50 null
artistEmail varchar 50 null
artistDob datetime not not null
artistRegdate datetime null
) ;





制表


 创建 员工

employeeId int null
employeeUsername varchar 50 null
employeePassword varchar 50 null
employeeName varchar 50 null
employeeContact int null
employeeEmail varchar 30 null
primary key (employeeId)
);

创建 table 艺术家

artistId int null
artistUsername varchar 50 null
artistPassword varchar 50 null
artistName varchar 50 null
artistContactno int null
artistAddress varchar 50 null
artistEmail varchar 50 null
artistDob datetime null
artistRegdate datetime null
主要 密钥(artistId),
employee_employeeId int null 约束 FK_art_emp_Id
FOREIGN KEY REFERENCES 员工(empl oyeeId)
);





代码块已更正,制表


试一试

CREATE TABLE上的SQL FOREIGN KEY约束

I am creating two tables employee and artist.employee table is created successfully but i am getting error while creating artist table with foreign key.
i have written following query:

create table employee
(
employeeId int not null,
employeeUsername varchar(50) not null,
employeePassword varchar(50) not null,
employeeName varchar(50) not null,
employeeContact int not null,
employeeEmail varchar(30) not null,
primary key(employeeId)

);

Create table artist
(
artistId int not null,
artistUsername varchar(50) not null,
artistPassword varchar(50) not null,
artistName varchar(50) not null,
artistContactno int not null,
artistAddress varchar(50) not null,
artistEmail varchar(50) not null,
artistDob datetime not null,
artistRegdate datetime not null,
primary key (artistId),
employee_employeeId int references employee
);


i am getting error as:

Foreign key 'employee' references invalid column 'employee' in referencing table 'artist'.

create table employee
(
   employeeId int not null,
   employeeUsername varchar(50) not null,
   employeePassword varchar(50) not null,
   employeeName varchar(50) not null,
   employeeContact int not null,
   employeeEmail varchar(30) not null,
   Constraint PK_Employee PRIMARY KEY(employeeId)
)
 
Create table artist
{
   artistId INT NOT NULL CONSTRAINT FK_artist_artistId
      FOREIGN KEY REFERENCES Employee(employeeId), --ON DELETE CASCADE this is optional if you want to 
   artistUsername varchar(50) not null,
   artistPassword varchar(50) not null,
   artistName varchar(50) not null,
   artistContactno int not null,
   artistAddress varchar(50) not null,
   artistEmail varchar(50) not null,
   artistDob datetime not null,
   artistRegdate datetime not null
);



tabulation


create table employee
(
   employeeId int not null,
   employeeUsername varchar(50) not null,
   employeePassword varchar(50) not null,
   employeeName varchar(50) not null,
   employeeContact int not null,
   employeeEmail varchar(30) not null,
   primary key(employeeId)
);
 
Create table artist
(
   artistId int not null,
   artistUsername varchar(50) not null,
   artistPassword varchar(50) not null,
   artistName varchar(50) not null,
   artistContactno int not null,
   artistAddress varchar(50) not null,
   artistEmail varchar(50) not null,
   artistDob datetime not null,
   artistRegdate datetime not null,
   primary key (artistId),
   employee_employeeId int not null constraint FK_art_emp_Id
      FOREIGN KEY REFERENCES Employee(employeeId)
);



Code block corrected, tabulation


try it
SQL FOREIGN KEY Constraint on CREATE TABLE