2015-02-11 88 views
1
create database MC120203207 

create table employee 
(
    id varchar (15) not null, 
    Name nvarchar (50) not null, 
    Address nvarchar (75) not null, 
    DoB date not null, 
    primary key(id) 
); 

create table Payroll 
(
    id varchar (10)not null, 
    Allowances float not null, 
    Msalary float not null, 
    References employee(id), 
    primary key(id) 
); 

create table Hourly 
( 
    id varchar (10)not null, 
    HourlyRate float not null, 
    References employee(id), 
    primary key (id) 
); 

Create Table Projects 
(
    id varchar (10) not null, 
    Code int (10) not null, 
    Description varchar (50) not null, 
    primary key (code) 
); 

Insert into employee (id, name, address, DoB) values ('MC120203207', 'Aurang Zeb Khan', 'Bannu', 27-02-1980) 
Insert into employee (id, name, address, DoB) values ('BC120201875', 'Alam Zeb Khan', 'Bannu', 02-08-1992) 

有在查詢一個問題,請大家幫忙解決它......它顯示的信息SQL查詢錯誤有助於解決

消息142,級別15,狀態2,0行
'TABLE'約束定義的語法不正確。

Msg 142,Level 15,State 2,Line 0
定義'TABLE'約束的語法不正確。

+2

你沒有定義任何** **列在'Payroll'或'Hourly'引用'employee.id'!嘗試:'僱員VARCHAR(15)引用員工(ID)' – 2015-02-11 06:09:18

回答

2

你需要對你的表一引用employee.id - 只是寫references employee(id)僅靠不夠的!

,請注意一個事實,即你Payroll(或Hourly定義外鍵列必須有完全相同的的數據類型爲你引用的列。

試試這個:

create table Payroll 
(
    id varchar (10)not null, 
    Allowances float not null, 
    Msalary float not null, 
    employeeId varchar(15) references employee(id), 
    primary key(id) 
); 
+0

你是對的@marc_s&另外,如果你想使你的所有這些MC120203207數據庫中的表,那麼你需要第一線後寫上「使用MC120203207」。 – 2015-02-11 06:17:33

+0

它的工作和 「查詢已成功執行」 創建表的工資 ( ID VARCHAR(10)NOT NULL, 津貼浮不爲空, Msalary浮不爲空, 僱員VARCHAR(15)引用員工(ID), 主key(id) ); 創建表每小時 ( ID VARCHAR(10)不爲空, HourlyRate浮不爲空, EMPLOYEEID VARCHAR(15)的引用僱員(ID), 主鍵(ID) ); – 2015-02-11 07:01:57