1. Database System Sunita M. Dol
Page 1
HANDOUT#02
Aim:
Implementation of SQL DML commands: Insert, Update, and Delete
Theory:
The SQL(Structured Query Language) Language has several parts:
1. Data-definition language (DDL). The SQL DDL provides commands for defining
relation schemas, deleting relations, and modifying relation schemas.
2. Interactive data-manipulation language (DML). The SQL DML includes a query
language based on both the relational algebra and the tuple relational calculus. It
includes also commands to insert tuples into, delete tuples from, and modify tuples in
the database.
3. View definition. The SQL DDL includes commands for defining views.
4. Transaction control. SQL includes commands for specifying the beginning and
ending of transactions.
5. Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL
statements can be embedded within general-purpose programming languages, such as
C, C++, Java, PL/I, Cobol, Pascal, and Fortran.
6. Integrity. The SQL DDL includes commands for specifying integrity constraints that
the data stored in the database must satisfy. Updates that violate integrity constraints
are disallowed.
7. Authorization. The SQL DDL includes commands for specifying access rights to
relations and views.
Domain Types in SQL:
char(n). Fixed length character string, with user-specified length n.
varchar(n). Variable length character strings, with user-specified maximum length
n.
int. Integer (a finite subset of the integers that is machine-dependent).
smallint. Small integer (a machine-dependent subset of the integer domain type).
numeric(p,d). Fixed point number, with user-specified precision of p digits, with n
digits to the right of decimal point.
real, double precision. Floating point and double-precision floating point numbers,
with machine-dependent precision.
float(n). Floating point number, with user-specified precision of at least n digits.
2. Database System Sunita M. Dol
Page 2
DML
The SQL DML includes a query language based on both the relational algebra and the tuple
relational calculus. It includes also commands to
insert tuples into,
delete tuples from, and
modify tuples in the database.
INSERT Command
It is possible to write the INSERT INTO statement in two ways.
• Method 1: The first way specifies both the column names and the values to be
inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
• Method 2: If you are adding values for all the columns of the table, you do not
need to specify the column names in the SQL query. However, make sure the
order of the values is in the same order as the columns in the table. The INSERT
INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
You can populate data into a table through select statement over another table provided
another table has a set of fields, which are required to populate first table. Here is the
syntax:
INSERT INTO first_table_name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM second_table_name
[WHERE condition];
Insert multiple rows in relation:
• Method 1:The syntax for the INSERT ALL statement:
INSERT ALL
INTO mytable (column1, column2,... column_n) VALUES (expr1,
expr2,... expr_n)
INTO mytable (column1, column2,... column_n) VALUES (expr1,
expr2,... expr_n)
INTO mytable (column1, column2,... column_n) VALUES (expr1,
expr2,... expr_n)
SELECT * FROM dual;
• Method 2: The syntax for the INSERT statement is
INSERT INTO table_name (column1, column2,...columnN.) VALUES
(&column1, &column2, ... &columnN)
If the type of attribute is CHAR or VARCHAR then use single quote in values
e.g. ‘&column1’ else simple use &column1. For inserting remaining rows, simply
used ‘/’.
3. Database System Sunita M. Dol
Page 3
UPDATE Command
The syntax for the UPDATE statement when updating a table in SQL is:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
[WHERE conditions];
OR
The syntax for the SQL UPDATE statement when updating a table with data from
another table is:
UPDATE table1
SET column1 = (SELECT expression1
FROM table2
WHERE conditions)
[WHERE conditions];
OR
The syntax for the SQL UPDATE statement when updating multiple tables (not permitted
in Oracle) is:
UPDATE table1, table2, ...
SET column1 = expression1,
column2 = expression2,
...
WHERE table1.column = table2.column
[AND conditions];
DELETE Command
The syntax for the DELETE statement in SQL is:
DELETE FROM table
[WHERE conditions];
Queries and Output:
CREATE Command and Adding Constraints on Relation
SQL> create table branch
2 (branch_name char(15),
3 branch_city char(15),
4 assets numeric(16,2),
5 primary key(branch_name),
6 check(assets>=0));
Table created.
4. Database System Sunita M. Dol
Page 4
SQL> desc branch;
Name Null? Type
----------------------------------------- -------- ----------------------------
BRANCH_NAME NOT NULL CHAR(15)
BRANCH_CITY CHAR(15)
ASSETS NUMBER(16,2)
INSERT Command
SQL> insert into branch(branch_name,branch_city,assets)
values('&branch_name','&branch_city',&assets
);
Enter value for branch_name: Brighton
Enter value for branch_city: Brooklyn
Enter value for assets: 7100000
old 1: insert into branch(branch_name,branch_city,assets)
values('&branch_name','&branch_city',&assets)
new 1: insert into branch(branch_name,branch_city,assets)
values('Brighton','Brooklyn',7100000)
1 row created.
.
.
SQL> select * from branch;
BRANCH_NAME BRANCH_CITY ASSETS
--------------- --------------- ----------
Brighton Brooklyn 7100000
Downtown Brooklyn 9000000
Mianus Horseneck 400000
North Town Rye 3700000
Perryridge Horseneck 1700000
Pownal Bennington 300000
Redwood Palo Alto 2100000
Round Hill Horseneck 8000000
8 rows selected.
SQL> create table account
2 (account_number char(10),
3 branch_name char(15),
5. Database System Sunita M. Dol
Page 5
4 balance numeric(12,2),
5 primary key(account_number),
6 foreign key(branch_name)references branch,
7 check(balance>=0));
Table created.
SQL> desc account;
Name Null? Type
----------------------------------------- -------- ----------------------------
ACCOUNT_NUMBER NOT NULL CHAR(10)
BRANCH_NAME CHAR(15)
BALANCE NUMBER(12,2)
SQL> insert into account(account_number,branch_name,balance)
values('&account_number','&branch_name'
,&balance);
Enter value for account_number: A-101
Enter value for branch_name: Downtown
Enter value for balance: 500
old 1: insert into account(account_number,branch_name,balance)
values('&account_number','&branch_name'
,&balance)
new 1: insert into account(account_number,branch_name,balance) values('A-
101','Downtown',500)
1 row created.
.
.
.
SQL> select * from account;
ACCOUNT_NU BRANCH_NAME BALANCE
---------- --------------- ----------
A-101 Downtown 500
A-102 Perryridge 400
A-201 Brighton 900
A-215 Mianus 700
A-217 Brighton 750
A-222 Redwood 700
6. Database System Sunita M. Dol
Page 6
A-305 Round Hill 350
7 rows selected.
SQL> create table student(roll_no char(10),first_name char(30),middle_name
char(30),last_name char(3
0),address char(50),city char(20),pincode numeric(6,0),state char(20),mobile_number integer);
Table created.
UPDATE Command
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata M
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-26 Pradnya N
Dudam
Sakhar Peth Solapur
413005 Maharashtra 7745865343
7. Database System Sunita M. Dol
Page 7
SQL> update student set middle_name='Minesh' where roll_no='TECSE-16';
1 row updated.
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata Minesh
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-26 Pradnya N
Dudam
Sakhar Peth Solapur
413005 Maharashtra 7745865343
DELETE Command
SQL> delete from student where roll_no='TECSE-26';
1 row deleted.
8. Database System Sunita M. Dol
Page 8
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata Minesh
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
Conclusion:
We have studied the various DML commands like
a. INSERT command
b. DELETE command
c. UPDATE command
References:
• Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) sixth edition.
• Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) fifth edition.
• http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
• http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
• http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/