Docsity

Connecting Tables Using Primary Key and Foreign Key, Lecture notes of Engineering

Toggle sidebar
bg1
Connecting Tables Using Primary Key and Foreign Key
1. PRIMARY KEY
A unique identifier for each row of data.
Cannot be duplicated.
Cannot be null.
Should be from a parent table.
Also known as the foreign key.
2. FOREIGN KEY
Can be not unique.
Can be duplicated.
Can be null.
Should also exists in the parent table by using a constraint.
Please see sample tables below:
Primary Key = contact_id of my_contacts table (parent table).
Foreign Key = contact_id of interests table.
!
!
!
!
!
!
!
Primary Key Syntax!
!
!
!
!
Page 1
MySQL
Creating Tables with Primary Key and Foreign Key
pf3
pf4

Partial preview of the text

Download Connecting Tables Using Primary Key and Foreign Key and more Lecture notes Engineering in PDF only on Docsity!

Connecting Tables Using Primary Key and Foreign Key

1. PRIMARY KEY

A unique identifier for each row of data.

Cannot be duplicated.

Cannot be null.

Should be from a parent table.

Also known as the foreign key.

2. FOREIGN KEY

Can be not unique.

Can be duplicated.

Can be null.

Should also exists in the parent table by using a constraint.

Please see sample tables below:

Primary Key = contact_id of my_contacts table (parent table).

Foreign Key = contact_id of interests table.

Primary Key Syntax

MySQL

Creating Tables with Primary Key and Foreign Key

PRIMARY KEY SYNTAX EXPLAINED

Number INT NOT NULL AUTO_INCREMENT, Number - create a column name called Number INT - set the Number column to Integer. NOT NULL - make the Number column not null. AUTO_INCREMENT - Make the Number column auto increment. PRIMARY KEY(Number) Indicates that the Number column is the primary key holder. My_Grocery_Lists Table with Primary Keys ( this will also be called the parent table ) CREATE TABLE My_Grocery_Lists ( Number INT NOT NULL AUTO_INCREMENT, Grocery_Items VARCHAR(30), Price DEC(6,2), Expiry_Date DATE, Stocks VARCHAR(20), PRIMARY KEY(Number) ); INSERT INTO My_Grocery_Lists VALUES ( 1, 'Milk', 40.2, '2023-01-10', '3 boxes' ); INSERT INTO My_Grocery_Lists VALUES ( 2, 'Rice', 160.2, '2023-02-15', '3 kl.' ); INSERT INTO My_Grocery_Lists VALUES ( 3, 'Pork', 120.2, '2022-02-10', '2 kl.' ); INSERT INTO My_Grocery_Lists VALUES ( 4, 'Chicken', 118.2, '2022-02-10', '3 kl.' ); INSERT INTO My_Grocery_Lists VALUES ( 5, 'Beef', 125.2, '2022-02-10', '3 kl.' ); SELECT * FROM My_Grocery_Lists;

RESULT:

Reminder: Do not put single quotes on non-decimals/numbers.

SYNTAX EXPLAINED

INSERT INTO Brands VALUES ( 1, 'Nestle', 1); INSERT INTO - command used for inserting values. Brands - the column where the values will be inserted. VALUES - the values to be inserted. 1 - the primary key of Nestle on this table. Nestle - the brand of milk. 1 - the foreign key ( the milks ID on the parent table (My_Grocery_Lists). INSERT INTO Brands VALUES ( 2, 'Japanese Rice',2); INSERT INTO - command used for inserting values. Brands - the column where the values will be inserted. VALUES - the values to be inserted. 2 - the primary key of Japanese Rice on this table. Japanese Rice - the brand of rice. 2 - the foreign key ( the rice ID on the parent table (My_Grocery_Lists). INSERT INTO Brands VALUES ( 3, 'Pork Roll', 3); INSERT INTO - command used for inserting values. Brands - the column where the values will be inserted. VALUES - the values to be inserted. 3 - the primary key of Pork Roll on this table. Pork Roll - the brand of rice. 3 - the foreign key ( the pork ID on the parent table (My_Grocery_Lists). INSERT INTO Brands VALUES ( 4, 'Magnolia', 4); INSERT INTO - command used for inserting values. Brands - the column where the values will be inserted. VALUES - the values to be inserted. 4 - the primary key of Magnolia on this table. Magnolia- the brand of rice. 4 - the foreign key ( the chicken ID on the parent table (My_Grocery_Lists). INSERT INTO Brands VALUES ( 5, 'Monterey', 5); INSERT INTO - command used for inserting values. Brands - the column where the values will be inserted. VALUES - the values to be inserted. 5 - the primary key of Monterey on this table. Monterey- the brand of rice. 5 - the foreign key ( the beef ID on the parent table (My_Grocery_Lists). Important: A foreign key is important in connecting tables as it enables relationship to the parent table. This will make other foreign keys unacceptable on other tables if it is not present on the parent table. Created By: mju2022 | Feb 13,2022 | 12:46 PM | LNC