State Universities and Colleges
Iloilo Science and Technology University
College of Arts and Sciences
La Paz, Iloilo City
Laboratory 3 – Data Definition Language
SQL Data Manipulation Language (DML) - is a syntax for executing queries. But the SQL
language also includes a syntax to update, insert, and delete records.
SELECT - extracts data from a database table
INSERT INTO - inserts new data into a database table
UPDATE - updates data in a database table
DELETE - deletes data from a database table
SELECT – used to retrieve a record or group of records in a table.
Syntax: SELECT * FROM table_name; OR
SELECT column1,column2 … columnN FROM table_name; OR
SELECT column1,column2 … columnN FROM table_name WHERE condition;
Example:
SELECT lastName,firstName FROM persons;
SELECT * FROM persons WHERE lastName = 'Rasmussen' AND age < 30;
INSERT – used to insert a record (row) to your table.
Syntax: INSERT INTO table_name VALUES ('value1', 'value2'… 'valueN'); OR
INSERT INTO table_name (column1,column2..columnN) VALUES ('value1',
'value2'… 'valueN');
Example:
INSERT INTO persons VALUES ('Hetland', 'Camilla', 'Hagabakka 24',
'Sandnes');
INSERT INTO persons (lastname, address) VALUES ('Rasmussen', 'Storgt 67');
INSERT INTO persons (lastName, age) VALUES ('Rasmussen',67);
Note: String values are enclosed in single quote (‘), numbers are not.
UPDATE – used to update a record or group of records in a table;
Syntax: UPDATE table_name SET column1 = 'value1', column2 = 'value2' …column
=’valueN’ WHERE condition;
Example:
UPDATE person SET city = 'Stavanger' WHERE firstname = ‘Camilla';
UPDATE person SET address = 'Stien 12', city = 'Stavanger' WHERE age <= 20;
DELETE – deletes a record or group of records in a table.
Syntax: DELETE FROM table_name; OR
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM person;
DELETE FROM person WHERE lastname = 'Rasmussen';
DELETE FROM person WHERE id = 3;
LAB EXERCISE
In this laboratory exercise, we are going to use the database below.
DB Name: dml_{year-section}_{lastname} eg. dml_2E_palawan
CREATE TABLE department {
dept_id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (dept_id)
}
CREATE TABLE student {
student_id BIGINT NOT NULL AUTO_INCREMENT,
dept_id BIGINT NOT NULL,
fname VARCHAR(50) NOT NULL,
lname VARCHAR(50) NOT NULL,
mi VARCHAR(1) NULL,
bday DATE NULL,
sex VARCHAR(1) NULL,
contact VARCHAR(13) NOT NULL,
reg_date TIMESTAMP NOT NULL,
PRIMARY KEY (student_id),
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
}
After once the database is created. Continue with DML language exercise.
First of all lets check the contents of each table. We need to use SELECT statement to retrieve
all records within each table.
Retrieve records from student table;
As you can see, it whos and Empty set (0.00 sec) because there’s no record in the table.
Same with department.
We are going to insert a new record to department table.
A successful INSERT statement always returns Query OK, 1 row affected. It means 1 record
(row) is created.
To check if we really added a new record, we are going to retrieve the records of department
table.
As you can see, the SELECT statement returns 1 row (record).
We’ll going to add a record to student table also.
When doing insert, make sure that the sequence of values is the same as in the field’s list. With
that said, the number of values should be the same with the number of fields.
If you check the INSERT statement above it uses the following values and and sequence;
student_id 1
dept_id 1
fname ‘Jose’
lname ‘Rizal’
mi ‘P’
bday '1861-06-19'
sex 'M'
contact '09231234235'
reg_date '2024-02-26 10:00:00'
Important Note:
String needs single quote eg. ‘Rizal’
Date values is considered as a string and should be formatted as ‘YYYY-MM-DD’ eg. bday
Timestamp is also considered as a string and should be formatted as ‘YYYY-MM-DD
HH:MM:SS’ eg. reg_date
Integers does not need single quote. eg. student_id
Compare the table above with the list of fields in the student table. Noticed that the sequence is
the same.
Let’s check if the record has been added.
We have 1 row (record) in student table.
You can also specify the fields in your INSERT statement.
When using INSERT statement with fields specified, you need to make sure that the number of
fields and its sequence should match with your values.
6 Fields : student(dept_id,reg_date,fname,lname,contact,sex)
6 Values : VALUES(1,'2024-02-27 11:00:00','Juan','Luna','091912345578','M')
Lets check the records of student table again.
Based on the result, you can see the student_id has a value of 2 even if we didn’t specified a
value for that field. This is due to AUTO_INCREMENT and student_id was declared as
AUTO_INCREMENT FIELD.
Fields that was not included in INSERT statement will have a value of NULL.
Now, let us try updating the values of records in a table.
We will change Jose Rizals contact number
Successful UPDATE will return how many records has been affected.
We also use the keywork WHERE
The WHERE keyword is used to add condition in your query. Using this, you can specify what
record will be affected by your query. In the example, I used WHERE to specify that I will only
records with student_id value is 1.
You can also use other fields for your condition if necessary.
NOTE: UPDATING without using WHERE keyword will update all records in your table
Let us try to update the student table again.
In this example, we updated the record with student_id = 2. If you noticed, you can update
multiple fields. You just need to separate them with comma.
Let’s check the records of student table again.
For removing the records use DELETE keyword.
Like if we want to delete JUAN LUNA.
Just like UPDATE, you can use WHERE keyword in DELETE also.
NOTE: DELETING without using WHERE keyword will delete all records in your table
LAB ACTIVITY 4
In this laboratory exercise, we are going to use the database below.
Replace all records of following table in the database we used for exercise.
Create new records in department table if necessary.
student_id dept_id fname lname mi bday sex contact reg_date
3 1 Carlo Romulo NULL 1924-04-28 M 09191234558 2024-02-27 9:00:00
4 2 Joh Doe D NULL M 09172342367 2024-02-27 9:00:01
5 4 Henry Sy Q 1925-05-21 M 09172342367 2024-02-27 9:00:02
6 3 Joe Spade H 1924-04-30 M 09172342367 2024-02-27 9:00:03
7 1 Sarah Lane R 1924-07-30 M 09172342367 2023-03-27 8:00:04
8 2 Peter Parks K 1924-04-23 M 09152388367 2024-02-27 9:00:05
9 4 John Linon F 1924-06-24 M 09172342367 2024-02-26 7:00:06
10 5 King Doir NULL NULL M 09172342367 2024-02-27 9:00:07
450 5 Carlo Romulo B 1924-09-13 M 09182398767 2024-01-27 9:00:08
For checking:
Show all the records in department and student table.
Have your instructor check your work.