CustomerID int NOT NULL LastName varchar(30) NOT NULL FirstName varchar(30) NOT
ID: 3744981 • Letter: C
Question
CustomerID int NOT NULL
LastName varchar(30) NOT NULL
FirstName varchar(30) NOT NULL
StreetNumber varchar(10)
StreetName varchar(30)
City varchar(30)
State varchar(2)
Zipcode varchar(10)
Phone varchar(20) NOT NULL
CONSTRAINT Customer_PK PRIMARY KEY(CustomerID);
VehicleID int NOT NULL
CustomerID int NOT NULL
Year int NOT NULL
Make varchar(20) NOT NULL
Model varchar(20) NOT NULL
LicenseNo varchar(20)
Color varchar(20) NOT NULL
VIN varchar(17)
CONSTRAINT Vehicle_PK PRIMARY KEY(VehicleID)
Above are Create Table statements for Microsoft Azure SQL. The "Customer" table has already been created, but I am having issues in creating the "Vehicle" table. I need to create a foreign key: "CustomerID" within the Vehicle table. Thank you
Explanation / Answer
To create the foreign key we just need to use the keyword foreign key followed by the column id and then
references which means it refers to the table which is given after this keyword and the columnid in the bracket tells that the referring column.
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID));
In the above CustomerID after foreign key tells that the customerid in vehicle is taking reference and the references key is the one tells that the source for reference and customer(customerid) tells that from the customer table it takes the reference for the foreign key.
Inoder to create the foreign key reference the source column/referringcolumn in the source table must be the primary key. Here in the above customerid in the customer table is primary key otherwise foreign key will not be created
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.