The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

SQL Syntax

SQL Syntax

Super Learner
0
Votes
1
Answer

For creating a foreign key will the below code be appropriate?
CREATE TABLE items
(
item_id VARCHAR(255),
item VARCHAR(255),
unit_price NUMERIC(10,2),
company_id VARCHAR(255) FOREIGN KEY,
PRIMARY KEY(item_id)
);

#, #
1 Answer

0
Votes

A foreign key refers to a column in another table. So you need to use References key word and specify the other table name and column in the foreign key constraint. 
 
This is what the script will look like in MySQL. 
CREATE TABLE items
(
item_id VARCHAR(255),
item VARCHAR(255),
unit_price NUMERIC(10,2),
company_id VARCHAR(255) FOREIGN KEY,
PRIMARY KEY(item_id),
FOREIGN KEY (company_id)  REFRENCES company(company_id));

×
LAST CHANCE
Ready to Learn Data Science?
50% OFF