Reputation: 8276
I created the ERD of my system and now I would like to create a SQL Code.
So should a SQL Code look like this?:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL FOREIGN KEY
);
Upvotes: 1
Views: 363
Reputation: 29619
Your "FOREIGN KEY" declaration is incomplete - you need to say which table/column the foreign key references.
If you just want to get the table built,
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL);
should work.
Upvotes: 2
Reputation: 70638
Your problem is with the FOREIGN KEY
part of your query, you are not defining the foreign key there. If you now remove that, your query will work, but without a defined FK:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL
);
If you want to create the foreign key, you need to do something like this (with the correct table and column):
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL REFERENCES Group(Group_ID)
);
Upvotes: 3