How does CONSTRAINT statement know which field sb PRIMARY KEY?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I don't see how the following code snippet from page 100 in ADO.NET core reference creates the primary key out of the ID field

CREATE TABLE NewTable (NewTableID int NOT NUL
CONSTRAINT PK_NewTable PRIMARY KEY
OtherField varchar(32)

Its not clear to me whether the field is ID or NewTableID. Its also not clear how the constraint statement knows which field is referred to. Is it just tht the constraint statement comes after the NewTableID declaration

polynomial5d
 
I don't see how the following code snippet from page 100 in ADO.NET core
reference creates the primary key out of the ID field:
CREATE TABLE NewTable (NewTableID int NOT NULL
CONSTRAINT PK_NewTable PRIMARY KEY,
OtherField varchar(32))

Its not clear to me whether the field is ID or NewTableID. Its also not
clear how the constraint statement knows which field is referred to. Is it
just tht the constraint statement comes after the NewTableID declaration?

Yes, why not?
 
Hi,

It does not create primary key for ID field, because ID field does not
exist. What it does here is it creates table where PK based on NewTableID
field. It happens because you specify primary constraint when you declare
your field. If you have a look into your code closely, then you will see
that there is no comma between first and second lines in your script, which
means that second line of script belong to the declaration of the first
field

CREATE TABLE NewTable (NewTableID int NOT NULL CONSTRAINT PK_NewTable
PRIMARY KEY, ....

and here you specify that NewTableID is primary key and name of the index
for this primary key is PK_NewTable
 
Back
Top