SQL Query错误有助于解决

时间:2015-02-11 06:06:25

标签: sql sql-server

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)

查询中存在问题,请帮助解决它...它显示消息

  

Msg 142,Level 15,State 2,Line 0
  用于定义'TABLE'约束的语法不正确。

     

Msg 142,Level 15,State 2,Line 0
  用于定义'TABLE'约束的语法不正确。

1 个答案:

答案 0 :(得分: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)
);