Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

SQL
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'.
Posted
Updated 4-Apr-14 4:58am
v2

SQL
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
 
Share this answer
 
v5
SQL
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
 
Share this answer
 
v3
 
Share this answer
 
Comments
Maciej Los 4-Apr-14 10:59am    
+5
King Fisher 5-Apr-14 0:03am    
thank you. sir ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900