0% found this document useful (0 votes)
16 views14 pages

MySql Exercise 1 - Create Table

Uploaded by

Mercy Chinyuku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

MySql Exercise 1 - Create Table

Uploaded by

Mercy Chinyuku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

MySQL Exercise 1

Create Table
Create Database Syntax
CREATE DATABASE database_name;
Create Database

CREATE DATABASE Exercise1;


Create Table syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Create Table

CREATE TABLE pet (


name VARCHAR(20),
owner VARCHAR(20),
species VARCHAR(20),
sex enum(‘M’,’F’),
birth DATE,
death
DATE,
Primary Key(name)
);
Create Table

Create the following tables using SQL script:


• LECTURER teaches COURSE
• Lecturer(LNum, LName)
• Course (CNum, CName,LNum)
Lecturer table

CREATE TABLE LECTURER (


LNum int(4),
LName varchar(20),
primary key (LNum)
);
Course table

CREATE TABLE COURSE(


CNum varchar(7),
CName varchar (20),
LNum int (4),
primary key (CNum),
Foreign key (Lnum) references LECTURER(LNum)
on delete cascade
on update cascade

);
Foreign key Constraints
• On DELETE
– Restrict
– No action
– Cascade
– Set null
• On Update
– Restrict
– No action
– Cascade
– Set null
Restrict
• RESTRICT option prevents the removal (i.e.
using delete) or modification (i..e using an
update) of rows from the parent table.
• means that any attempt to delete and/or
update the parent will fail throwing an error.
NO Action
PARENT CHILD
A B X A
a1 b1 X1 a1
a2 b2 X2 a1
X3 a2

• There will not be any change in the


referencing rows when the referenced ones
are deleted or updated.
• Same as restrict
Cascade
PARENT CHILD
A B X A
a1 b1 X1 a1
a2 b2 X2 a1
X3 a2

• Delete: the referencing rows will be deleted automatically along with the
referenced ones.
• IF WE DELETE row 1(a1) FROM parent it will delete rows 1 and 2 entries from
child.
• Problem need to keep history
• CASCADEif the parent primary key is changed, the child value will also
change to reflect that.
• IF WE Update in row 1(a1) FROM parent it will update rows 1 and 2 entries
from child.
SET NULL
PARENT CHILD
A B X A
a1 b1 X1 a1
a2 b2 X2 a1
X3 a2

• On Delete/Update the value of referencing


record will be set to NULL automatically along
with the referenced ones.
Create Table
• Create the following tables using SQL script:
Department(DeptName)
Lecturer (LecturerNo, LecturerName,DeptName)
Course(CourseID, CourseName, LecturerNo)
Class(ClassID, CourseID, LecturerNo, Venue, Times)

You might also like