Click here to Skip to main content
15,881,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE TABLE [presence](
[id_presence] [int] IDENTITY (1, 1) NOT NULL ,
[tgl] [datetime] NULL ,
[nip] [char] (2) CONSTRAINT [fk_nips] FOREIGN KEY [nip]
REFERENCES [school] ([NIP] )


error:
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'nip'.

what's the problem?

What I have tried:

I have tried to change anything that close to nip but still error. please how to fix it?
Posted
Updated 20-Jul-20 2:48am
v2
Comments
Shao Voon Wong 19-Jul-20 2:52am    
nip is a foreign key column that references the school table. Has the school table (with the NIP column) been created on the database before running this SQL statement?

When I try Solution 1 I get
Quote:
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near 'nip'.
In the line indicated. Why? Adding that comma is incorrect and there is a missing closing bracket at the end of the query
SQL
CREATE TABLE [presence](
[id_presence] [int] IDENTITY (1, 1) NOT NULL ,
[tgl] [datetime] NULL ,
[nip] [char] (2), --added a comma here
CONSTRAINT [fk_nips] FOREIGN KEY [nip] -- << -- Error is here
REFERENCES [school] ([NIP] )
When I try Solution 2 I also get an error
Quote:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near ')'.
Why. Still missing the closing bracket
SQL
CREATE TABLE [presence](
[id_presence] [int] IDENTITY (1, 1) NOT NULL,
[tgl] [datetime] NULL,
[nip] [char] (2) CONSTRAINT [fk_nips] FOREIGN KEY ([nip])
REFERENCES [school] ([NIP]) -- << -- Error is here 
Here is the correct solution. Why? Only difference between the original version and the working version is that closing bracket. Judicious use of whitespace can help spot things like this.
SQL
CREATE TABLE [presence]
(
	[id_presence] [int] IDENTITY (1, 1) NOT NULL ,
	[tgl] [datetime] NULL ,
	[nip] [char] (2) CONSTRAINT [fk_nips] 
	FOREIGN KEY ([nip]) REFERENCES [school] ([NIP])
);
I did actually test my version, which clearly the previous two posters did not.
 
Share this answer
 
Comments
Maciej Los 20-Jul-20 9:49am    
5ed!
CHill60 20-Jul-20 10:30am    
Thank you! I really do sound angry in that last line though :-)
I think you might be missing a comma at the end of [nip] [char] (2) as a column definition of Table [presence]

Try:
SQL
CREATE TABLE [presence](
[id_presence] [int] IDENTITY (1, 1) NOT NULL ,
[tgl] [datetime] NULL ,
[nip] [char] (2), --added a comma here
CONSTRAINT [fk_nips] FOREIGN KEY [nip]
REFERENCES [school] ([NIP] )
 
Share this answer
 
Comments
Maciej Los 20-Jul-20 9:50am    
5ed!
Please try putting parenthesis across [nip] on the line of CONSTRAINT.
He assumed that, you have already defined the table 'school' with NIP as primary key.
SQL
CREATE TABLE [presence](
[id_presence] [int] IDENTITY (1, 1) NOT NULL,
[tgl] [datetime] NULL,
[nip] [char] (2) CONSTRAINT [fk_nips] FOREIGN KEY ([nip])
REFERENCES [school] ([NIP])
 
Share this answer
 
v2

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