Tuesday 15 June 2010

sql server - Error in creating tables in SQL -



sql server - Error in creating tables in SQL -

i'm trying create few tables in new database, when seek create them creates couple of errors. i'm using microsoft sql server management studio.

the errors seem in end of code i'm trying add together constraints foreign keys. help appreciated give thanks you.

here's code, it's meant generate 3 tables 1 table containing 2 foreign keys other tables matching column names.

create table customers ( customerid int not null primary key identity, contactname varchar(50) null, company varchar(45) null, phone varchar(12) null, ) create table shippers ( shipperid int not null primary key identity, company varchar(45) null, phone varchar(12) null, ) create table orders ( orderid int not null primary key identity, orderdate datetime null, shippeddate datetime null, shipperid int null, freight decimal null, customerid int null, constraint fk_orders_shippers foreign key shipperid references shippers(shipperid) on delete no action on update no action constraint fk_orders_customers foreign key customerid references customers(customerid) on delete no action on update no action )

and these errors get:

msg 102, level 15, state 1, line 21 wrong syntax near 'shipperid'.

msg 102, level 15, state 1, line 23 wrong syntax near 'action'.

msg 102, level 15, state 1, line 28 wrong syntax near 'action'.

any ideas problem is?

the error messages point couple of surplus commas, missing comma , improper foreign key syntax due missing parentheses.

this right version:

create table customers ( customerid int not null primary key identity, contactname varchar(50) null, company varchar(45) null, phone varchar(12) null -- surplus comma removed ) create table shippers ( shipperid int not null primary key identity, company varchar(45) null, phone varchar(12) null -- surplus comma removed ) create table orders ( orderid int not null primary key identity, orderdate datetime null, shippeddate datetime null, shipperid int null, freight decimal null, customerid int null, constraint fk_orders_shippers foreign key (shipperid) -- parentheses added references shippers(shipperid) on delete no action on update no action , -- comma added constraint fk_orders_customers foreign key (customerid) -- parentheses added references customers(customerid) on delete no action on update no action )

sql sql-server

No comments:

Post a Comment