0% found this document useful (0 votes)
9 views23 pages

MySql Exercise 2 - Update Records

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)
9 views23 pages

MySql Exercise 2 - Update Records

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/ 23

MySQL Exercise 2

Update Table
CRUD
CRUD
• C-Create or Insert a record
• R- Read or retrieve a record
• U- Update or Modify a record
• D-Delete a record
Exercise
1. Create database Exercise2
2. Create the following tables
a) Customer(cnum, cname, cadd)
b) Orders(onum, odate,cnum)
c) Marks(snum, sname, quiz1, quiz2)
3. Insert into the customer table the following record
(1001, John, Nguruve st)
4. Update the record in orders table by changing the
order date of a record.
5. Delete all students with marks less than 50 for quiz 1
1. Create database Exercise2
CREATE DATABASE Exercise2;
2a) Create table from the relational
schema:
Customer(cnum, cname, cadd)

Create table customer(


cnum int not null,
cname varchar(20),
cadd varchar(50),
primary key(cnum)
);
2b) Create table from the relational
schema:
Orders(onum, odate,cnum)

Create table orders(


onum char(4) not null,
odate date not null,
cnum int,
primary key (onum),
constraint foreign key(cnum) references customer(cnum)
on delete cascade
on update cascade
);
2c) Create table from the relational
schema:Marks(snum, sname,
quiz1, quiz2)
Create table student (
snum char (7) not null,
sname varchar (20),
quiz1 int not null,
quiz2 int not null,
primary key (snum)
);
Insert record - Syntax
INSERT INTO table_name (
field1, field2,...fieldN )
VALUES ( value1, value2,...valueN );
3. Insert into the customer table
the following record
(1001, John, Nguruve st)
String values are enclosed in single or double
quotes

INSERT INTO customer(cnum, cname, cadd)


VALUES (1001, 'John', 'Nguruve st');
UPDATE record Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
4. Update the record in orders table by
changing the order date of a record.

UPDATE orders
SET odate = '2017-09-10'
WHERE onum = 'a123';
DELETE Syntax

DELETE FROM table_name [WHERE Condition];


5. Delete all students with marks less than
50 for quiz 1

DELETE FROM student


WHERE quiz1<50
Exercise
• In a college database. A lecturer can teach up
to a maximum of 3 courses. Every lecturer
must have a course to teach. For every
lecturer, employee id, name and office is
recorded. A course may be offered in a
semester or not at all. For a course, course id,
course name and the department to which it
belongs is recorded. The day, times and venue
for each class is also recorded.
1. Develop a conceptual model for the scenario
2. Translate the model to sets of relations
3. Implement the relational schemas using
MySQL
4. Populate the tables with values given in the
next table
COURSE
CourseID CourseName Department
LECTURER
CUIT201 Database ICT
LecturerID LecturerName Office
CUMT105 OR Maths
E1001 Chinyuku E7
CUIT206 OOP ICT
E1002 Kavu E8
CUIT208 Software Eng ICT
E1003 Rugube E10
CUPE105 Logic Design Engineering
E1004 Phiri E8
CUIT213 Data Comm Engineering
E1005 Moyo E26
CUEB401 Database Business

CLASS
ClassID LecturerID CourseID Day Time Venue
C1001 E1002 CUIT206 Monday 0800 Tlab
C1002 E1001 CUIT201 Monday 1000 Tlab
C1003 E1005 CUPE105 Wednesday 1400 NEC2
C1005 E1004 CUMT105 Friday 1200 NEC3
Lecturer table
CREATE TABLE Lecturer (
LecturerID CHAR(5),
LecturerName VARCHAR(25),
Office CHAR(5),
PRIMARY KEY (LecturerID)
);
Course Table
CREATE TABLE Course(
CourseID Char(10),
CourseName VARCHAR(25),
Department VARCHAR (25),
PRIMARY KEY (CourseID)
);
Class Table
Create TABLE Class(
ClassID char(5),
LecturerID Char(5),
CourseID CHAR(10),
DAY VARCHAR(15),
Time TIME,
Venue VARCHAR (10),
FOREIGN KEY(LecturerID) REFERENCES lecturer(LecturerID),
FOREIGN KEY (CourseID) REFERENCES course(CourseID)
ON DELETE CASCADE
);
• Write SQL scripts to:
1. Insert a record for a new lecturer
(E1010, Moyo, E30)
2. Modify Class table such that you move all
lectures on Monday to Friday
3. Delete all courses with name Database from
course table
Insert
1. Insert a record for a new lecturer
(E1010, Moyo, E30)

INSERT INTO Lecturer(LecturerID, LecturerName, Office)


VALUES ('E1010', 'Moyo', 'E30');
Modify
Modify Class table such that you move all lectures on
Monday to Friday

UPDATE Class
SET Day = 'Friday'
WHERE Day = 'Monday';
• Delete all courses with name Database from course
table
DELETE FROM course
WHERE CourseName='Database';

You might also like