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

Lab2 - DML1

The document discusses how to perform DML operations like insert, update, and delete on tables in a database. It provides examples of inserting new rows with and without specifying column names, updating existing rows by setting column values and using where clauses, and deleting rows using where clauses to filter which rows to delete.

Uploaded by

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

Lab2 - DML1

The document discusses how to perform DML operations like insert, update, and delete on tables in a database. It provides examples of inserting new rows with and without specifying column names, updating existing rows by setting column values and using where clauses, and deleting rows using where clauses to filter which rows to delete.

Uploaded by

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

Cairo University

Faculty of Computers and Artificial Intelligence


Information Systems Department

Database I, Year 2022/2023


Lab - 2
DML1 [Insert, Update, Delete]

1. Inserting New Rows

It is possible to write insert into statements in two ways:

1- specifies both the column names and the values to be inserted:

INSERT INTO <tablename> [<column list>]


VALUES (<expression>{[,<expression>]})

2- if you are adding all values to all columns, you don’t need to spcify column names

INSERT INTO <tablename>


VALUES (value1,value2,…..)
Hint: you should make sure the order of the values is in the same order as the columns
in the table.

Example 1:

❖ Insert new student into the student table with the following properties:
Student id=1 , Name= Ali, Age=19, and City= ‘Cairo’

INSERT INTO Student (SSN,Name,Age,City) VALUES (1,'Ali',19,'Cairo')

INSERT INTO Student VALUES (2,’Wafaa',19,'Cairo',null,null)


///////////////////////////////////////////////////////////////////////////////////
insert into Student values (3,'Wafaa',19,'Cairo','IS',null) → this raise ad error as major is the FK
and not inserted yet into department … you should insert this row first into Department
table
INSERT INTO department values ('IS','Information System')

1
2. Updating Values in Rows

UPDATE <tablename>
SET <column name>=<expression>,<column name>=<expression>
[WHERE <Condition>]

Example 1:

❖ Increase the age of students by 1

update Student
set age=age+1

Example 2: (Using Where Clause)

❖ Change the name of the STudent with SSN 1 to be ‘AHMED’

update Student
set name='ahmed'
where SSN=1

Example 3: (Update more than one cloumn with a condition)

❖ Change the name of the STAFF with id 1 back to ‘ALI’ and let his age be 18

update Student
set name='Ali',
age=18
where SSN=1

3. Deleting rows from a table

DELETE
FROM <table name>
[WHERE <Condition>]

Example 1:

❖ Delete all rows of table Student (Don’t try it)

delete from Student

2
Example 2:

❖ Delete the Student with SSN 2

delete from Student


where SSN=2

Example 3: (Using more than one condition)

❖ Delete the STudent that has an id 1 or 2

delete from Student


where SSN=2 or
SSN=1

BETTER STATEMENT THAT DOES THE SAME JOB OF THE PREVIOUS ONE :

delete from Student


where SSN IN (1,2);

You might also like