You are on page 1of 6

Practice

Cities

1. Create the table and insert the values.


2. A list of all cities who scored over 60F temperature?
3. A list of all IDs of cities who scored more than 2 mm
of rainfall?
4. Show the Long_w and Lat_n of all cites which have D
in their name.
Client

1) Create this table.


2) Insert all data.
3) Show all the last_names of all the persons in this table.
4) Show only the last_names contains letter ‘h’.
5) Show the persons whom have letter K in their
first_name and age less than 30.
6) Add two rows to this table.
7) Delete the first row.
FOREIGN KEY
 A FOREIGN KEY is a key used to link two tables together.

 A FOREIGN KEY is a field (or collection of fields) in one


table that refers to the PRIMARY KEY in another table.

 The table containing the foreign key is called the child table,
and the table containing the candidate key is called the
referenced or parent table.

Look at the following two tables:


Example 1:

Persons

Orders

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons (P_Id));
Example 2:

Clients

Order

CREATE TABLE orders


(
O_Id int NOT NULL,
Order_No int NOT NULL,
S_Id int,
PRIMAY KEY (O_Id),
FOREIGN KEY (S_Id) REFERENCES Clients (S_Id));
Example 3:

Agents

Customer

CREATE TABLE customer(


cust_code char(6) NOT NULL PRIMARY KEY,
cust_name char(25),
cust_city char(25),
agent_code char(6),
FOREIGN KEY(agent_code)
REFERENCES agents (agent_code)
) ;

You might also like