Lab 2
Lab 2
Objectives
To write queries using Insert, Select, Update, and Delete Commands in Data Manipulation
Language (DML).
Introduction
Data Manipulation Language (DML) is used by computer programs or database users to
retrieve, insert, delete and update data in a database. Currently, the most popular data
manipulation language is that of SQL, which is used to retrieve and manipulate data in a
Relational database
SELECT
An SQL SELECT statement returns a result set of records from one or more tables. It retrieves
zero or more rows from one or more base tables or views in a database.
Example:
SELECT * FROM student;
* means all columns in a table The SELECT statement has many optional clauses:
INSERT
The number of columns and values must be the same. The values specified (or implied)
by the INSERT statement must satisfy all the applicable constraints (such as primary keys,
CHECK constraints, and NOT NULL constraints). If a syntax error occurs or if any con-
straints are violated, the new row is not added to the table and an error returned instead.
Syntax
• INSERT INTO table (column1, [column2, ... ]) VALUES (value1, [value2, ...])
Example
• student(name,registerno,branchno,section,joindate,mark,emailid) VALUES (’Aarthi’,11306205001,1,’A’,’01-
apr-2006’,99, ’[email protected]’)
7
8 Lab 2: Familirization with DML
UPDATE
A SQL UPDATE statement that changes the data of one or more records in a table. Either all
the rows can be updated, or a subset may be chosen using a condition.
Syntax
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE con-
dition]
Example
UPDATE student SET mark = mark + 2 WHERE registerno = 11306205001;
DELETE
An SQL DELETE statement removes one or more records from a table. A subset may be
defined for deletion using a condition, otherwise all records are removed. Any rows that
match the WHERE condition will be removed from the table. If the WHERE clause is omitted,
all rows in the table are removed. The DELETE statement should thus be used with caution!
Syntax
DELETE FROM table_name [WHERE condition]
Example
DELETE FROM student WHERE registerno = 11306205001;
Summary
1: Insert following Values into the Table Branch.
BRANCHNO BRANCHNAME
1 Civil
2 Computer Science
3 Electrical
4 Electronics
5 Information Technology
6 Instrumentation
7 Mechanical
8 MBA
9 MCA
2: Insert following Values into the Table Student.
3: Display the contents of Student Table.
4: Display the contents of Branch Table.
9
Practise Exercise
1: Create and insert values in Table named EMP