Date post: | 05-Apr-2017 |
Category: |
Engineering |
Upload: | sunita-aher |
View: | 35 times |
Download: | 0 times |
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 1
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.
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
simply use &column1. For inserting remaining rows, use ‘/’.
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 2
SQL> insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city');
Enter value for customer_name: Adams
Enter value for customer_street: Spring
Enter value for customer_city: Pittsfield
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Adams','Spring',' Pittsfield')
1 row created.
SQL> /
Enter value for customer_name: Brooks
Enter value for customer_street: Senator
Enter value for customer_city: Brooklyn
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Brooks','Senator',
'Brooklyn')
1 row created.
SQL> /
Enter value for customer_name: Curry
Enter value for customer_street: North
Enter value for customer_city: Rye
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Curry','North','R', 'Rye')
1 row created.
SQL> /
Enter value for customer_name: Glenn
Enter value for customer_street: Sand Hill
Enter value for customer_city: Woodside
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Glenn','Sand Hill', '
Woodside')
1 row created.
SQL> /
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 3
Enter value for customer_name: Green
Enter value for customer_street: Walnut
Enter value for customer_city: Stamford
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Green','Walnut',' Stamford')
1 row created.
SQL> /
Enter value for customer_name: Hayes
Enter value for customer_street: Main
Enter value for customer_city: Harrison
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Hayes','Main',' Harrison')
1 row created.
SQL> /
Enter value for customer_name: Johnson
Enter value for customer_street: Alma
Enter value for customer_city: Palo Alto
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Johnson','Alma',' Palo
Alto')
1 row created.
SQL> /
Enter value for customer_name: Jones
Enter value for customer_street: Main
Enter value for customer_city: Harrison
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Jones','Main',' Harrison')
1 row created.
SQL> /
Enter value for customer_name: Lindsay
Enter value for customer_street: Park
Enter value for customer_city: Pittsfield
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 4
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')new 1: insert into
customer(customer_name,customer_street,customer_city) values('Lindsay','Park',' Pittsfield')
1 row created.
SQL> /
Enter value for customer_name: Smith
Enter value for customer_street: North
Enter value for customer_city: Rye
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Smith','North','Rye')
1 row created.
SQL> /
Enter value for customer_name: Turner
Enter value for customer_street: Putnam
Enter value for customer_city: Stamford
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Turner','Putnam', '
Stamford')
1 row created.
SQL> /
Enter value for customer_name: Williams
Enter value for customer_street: Nassau
Enter value for customer_city: Princeton
old 1: insert into customer(customer_name,customer_street,customer_city)
values('&customer_name','&customer_street','&customer_city')
new 1: insert into customer(customer_name,customer_street,customer_city) values('Williams','Nassau', '
Princeton')
1 row created.
SQL> select * from customer
2 ;
CUSTOMER_NAME CUSTOMER_STREET
-------------------- ------------------------------
CUSTOMER_CITY
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 5
------------------------------
Adams Spring
Pittsfield
Brooks Senator
Brooklyn
Curry North
Rye
CUSTOMER_NAME CUSTOMER_STREET
-------------------- ------------------------------
CUSTOMER_CITY
------------------------------
Glenn Sand Hill
Woodside
Green Walnut
Stamford
Hayes Main
Harrison
CUSTOMER_NAME CUSTOMER_STREET
-------------------- ------------------------------
CUSTOMER_CITY
------------------------------
Johnson Alma
Palo Alto
Jones Main
Harrison
Lindsay Park
Pittsfield
CUSTOMER_NAME CUSTOMER_STREET
-------------------- ------------------------------
CUSTOMER_CITY
------------------------------
Smith North
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 6
Rye
Turner Putnam
Stamford
Williams Nassau
Princeton
12 rows selected.
The branch relation
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> /
Enter value for branch_name: Downtown
Enter value for branch_city: Brooklyn
Enter value for assets: 9000000
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('Downtown','Brooklyn',9000000)
1 row created.
SQL> /
Enter value for branch_name: Mianus
Enter value for branch_city: Horseneck
Enter value for assets: 400000
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('Mianus','Horseneck',400000)
1 row created.
SQL> /
Enter value for branch_name: North Town
Enter value for branch_city: Rye
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 7
Enter value for assets: 3700000
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('North Town','Rye',3700000)
1 row created.
SQL> /
Enter value for branch_name: Perryridge
Enter value for branch_city: Horseneck
Enter value for assets: 1700000
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('Perryridge','Horseneck',1700000)
1 row created.
SQL> /
Enter value for branch_name: Pownal
Enter value for branch_city: Bennington
Enter value for assets: 300000
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('Pownal','Bennington',300000)
1 row created.
SQL> /
Enter value for branch_name: Redwood
Enter value for branch_city: Palo Alto
Enter value for assets: 2100000
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('Redwood','Palo Alto',2100000)
1 row created.
SQL> /
Enter value for branch_name: Round Hill
Enter value for branch_city: Horseneck
Enter value for assets: 8000000
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('Round Hill','Horseneck',8000000)
1 row created.
SQL> select * from branch;
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 8
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.
The account relation
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> /
Enter value for account_number: A-102
Enter value for branch_name: Perryridge
Enter value for balance: 400
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-102','Perryridge',400)
1 row created.
SQL> /
Enter value for account_number: A-201
Enter value for branch_name: Brighton
Enter value for balance: 900
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-201','Brighton',900)
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 9
1 row created.
SQL> /
Enter value for account_number: A-215
Enter value for branch_name: Mianus
Enter value for balance: 700
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-215','Mianus',700)
1 row created.
SQL> /
Enter value for account_number: A-217
Enter value for branch_name: Brighton
Enter value for balance: 750
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-217','Brighton',750)
1 row created.
SQL> /
Enter value for account_number: A-222
Enter value for branch_name: Redwood
Enter value for balance: 700
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-222','Redwood',700)
1 row created.
SQL> /
Enter value for account_number: A-305
Enter value for branch_name: Round Hill
Enter value for balance: 350
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-305','Round Hill',350)
1 row created.
SQL> select * from account;
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 10
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
A-305 Round Hill 350
7 rows selected.
The depositor relation
SQL> insert into depositor(customer_name,account_number) values('&customer_name','&account_number');
Enter value for customer_name: Hayes
Enter value for account_number: A-102
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
new 1: insert into depositor(customer_name,account_number) values('Hayes','A-102')
1 row created.
SQL> /
Enter value for customer_name: Johnson
Enter value for account_number: A-101
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
new 1: insert into depositor(customer_name,account_number) values('Johnson','A-101')
1 row created.
SQL> /
Enter value for customer_name: Johnson
Enter value for account_number: A-201
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
new 1: insert into depositor(customer_name,account_number) values('Johnson','A-201')
1 row created.
SQL> /
Enter value for customer_name: Jones
Enter value for account_number: A-217
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 11
new 1: insert into depositor(customer_name,account_number) values('Jones','A-217')
1 row created.
SQL> /
Enter value for customer_name: Lindsay
Enter value for account_number: A-222
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
new 1: insert into depositor(customer_name,account_number) values('Lindsay','A-222')
1 row created.
SQL> /
Enter value for customer_name: Smith
Enter value for account_number: A-215
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
new 1: insert into depositor(customer_name,account_number) values('Smith','A-215')
1 row created.
SQL> /
Enter value for customer_name: Turner
Enter value for account_number: A-305
old 1: insert into depositor(customer_name,account_number) values('&customer_name','&account_number')
new 1: insert into depositor(customer_name,account_number) values('Turner','A-305')
1 row created.
SQL> select * from depositor;
CUSTOMER_NAME ACCOUNT_NU
-------------------- ----------
Hayes A-102
Johnson A-101
Johnson A-201
Jones A-217
Lindsay A-222
Smith A-215
Turner A-305
7 rows selected.
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 12
The loan relation
SQL> insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount);
Enter value for loan_number: L-11
Enter value for branch_name: Round Hill
Enter value for amount: 900
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount)
new 1: insert into loan(loan_number,branch_name,amount) values('L-11','Round Hill',900)
1 row created.
SQL> /
Enter value for loan_number: L-14
Enter value for branch_name: Downtown
Enter value for amount: 1500
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount)
new 1: insert into loan(loan_number,branch_name,amount) values('L-14','Downtown',1500)
1 row created.
SQL> /
Enter value for loan_number: L-15
Enter value for branch_name: Perryridge
Enter value for amount: 1500
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount)
new 1: insert into loan(loan_number,branch_name,amount) values('L-15','Perryridge',1500)
1 row created.
SQL> /
Enter value for loan_number: L-16
Enter value for branch_name: Perryridge
Enter value for amount: 1300
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amoun)
new 1: insert into loan(loan_number,branch_name,amount) values('L-16','Perryridge',1300)
1 row created.
SQL> /
Enter value for loan_number: L-17
Enter value for branch_name: Downtown
Enter value for amount: 1000
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount)
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 13
new 1: insert into loan(loan_number,branch_name,amount) values('L-17','Downtown',1000)
1 row created.
SQL> /
Enter value for loan_number: L-23
Enter value for branch_name: Redwood
Enter value for amount: 2000
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount)
new 1: insert into loan(loan_number,branch_name,amount) values('L-23','Redwood',2000)
1 row created.
SQL> /
Enter value for loan_number: L-93
Enter value for branch_name: Mianus
Enter value for amount: 500
old 1: insert into loan(loan_number,branch_name,amount) values('&loan_number','&branch_name',&amount)
new 1: insert into loan(loan_number,branch_name,amount) values('L-93','Mianus',500)
1 row created.
SQL> select * from loan;
LOAN_NUMBE BRANCH_NAME AMOUNT
---------- --------------- ----------
L-11 Round Hill 900
L-14 Downtown 1500
L-15 Perryridge 1500
L-16 Perryridge 1300
L-17 Downtown 1000
L-23 Redwood 2000
L-93 Mianus 500
7 rows selected.
The borrower relation
SQL> insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number');
Enter value for customer_name: Adams
Enter value for loan_number: L-16
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Adams','L-16')
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 14
1 row created.
SQL> /
Enter value for customer_name: Curry
Enter value for loan_number: L-93
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Curry','L-93')
1 row created.
SQL> /
Enter value for customer_name: Hayes
Enter value for loan_number: L-15
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Hayes','L-15')
1 row created.
SQL> /
Enter value for customer_name: Jackson
Enter value for loan_number: L-14
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Jackson','L-14')
insert into borrower(customer_name,loan_number) values('Jackson','L-14')
*
ERROR at line 1:
ORA-02291: integrity constraint (TESHIFT16.SYS_C009862) violated - parent key
not found
SQL> /
Enter value for customer_name: Jones
Enter value for loan_number: L-17
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Jones','L-17')
1 row created.
SQL> /
Enter value for customer_name: Smith
Enter value for loan_number: L-11
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Smith','L-11')
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 15
1 row created.
SQL> /
Enter value for customer_name: Smith
Enter value for loan_number: L-23
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Smith','L-23')
1 row created.
SQL> /
Enter value for customer_name: Williams
Enter value for loan_number: L-17
old 1: insert into borrower(customer_name,loan_number) values('&customer_name','&loan_number')
new 1: insert into borrower(customer_name,loan_number) values('Williams','L-17')
1 row created.
OR use following command to insert the data into relations
insert into customer(customer_name,customer_street,customer_city) values('Adams','Spring','Pittsfield');
insert into customer(customer_name,customer_street,customer_city) values('Brooks','Senator','Brooklyn');
insert into customer(customer_name,customer_street,customer_city) values('Curry','North','Rye');
insert into customer(customer_name,customer_street,customer_city) values('Glenn','Sand Hill','Woodsight');
insert into customer(customer_name,customer_street,customer_city) values('Green','Walnut','Stamford');
insert into customer(customer_name,customer_street,customer_city) values('Hays','Main','Harrison');
insert into customer(customer_name,customer_street,customer_city) values('Johnson','Alma','Palo Alto');
insert into customer(customer_name,customer_street,customer_city) values('Jones','Main','Harrison');
insert into customer(customer_name,customer_street,customer_city) values('Lindsay','Park','Pittsfield');
insert into customer(customer_name,customer_street,customer_city) values('Smith','North','Rye');
insert into customer(customer_name,customer_street,customer_city) values('Turner','Putnam','Stamford');
insert into customer(customer_name,customer_street,customer_city) values('Williams','Nassau','Princeton');
insert into branch(branch_name,branch_city,assets) values('Brighton','Brooklyn',7100000);
insert into branch(branch_name,branch_city,assets) values('Downtown','Brooklyn',9000000);
insert into branch(branch_name,branch_city,assets) values('Mianus','Horseneck',400000);
insert into branch(branch_name,branch_city,assets) values('North Town','Rye',3700000);
insert into branch(branch_name,branch_city,assets) values('Perryridge','Horseneck',1700000);
insert into branch(branch_name,branch_city,assets) values('Pownal','Bennington',300000);
insert into branch(branch_name,branch_city,assets) values('Redwood','Palo Alto',2100000);
insert into branch(branch_name,branch_city,assets) values('Round Hill','Horseneck',8000000);
insert into account(account_number,branch_name,balance) values('A-101','Downtown',500);
insert into account(account_number,branch_name,balance) values('A-102','Perryridge',400);
insert into account(account_number,branch_name,balance) values('A-201','Brighton',900);
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 16
insert into account(account_number,branch_name,balance) values('A-215','Mianus',700);
insert into account(account_number,branch_name,balance) values('A-217','Brighton',750);
insert into account(account_number,branch_name,balance) values('A-222','Redwood',700);
insert into account(account_number,branch_name,balance) values('A-305','Round Hill',350);
insert into depositor(customer_name,account_number) values('Hays','A-102');
insert into depositor(customer_name,account_number) values('Johnson','A-101');
insert into depositor(customer_name,account_number) values('Johnson','A-201');
insert into depositor(customer_name,account_number) values('Jones','A-217');
insert into depositor(customer_name,account_number) values('Lindsay','A-222');
insert into depositor(customer_name,account_number) values('Smith','A-215');
insert into depositor(customer_name,account_number) values('Turner','A-305');
insert into loan(loan_number,branch_name,amount) values('L-11','Round Hill',900);
insert into loan(loan_number,branch_name,amount) values('L-14','Downtown',1500);
insert into loan(loan_number,branch_name,amount) values('L-15','Perryridge',1500);
insert into loan(loan_number,branch_name,amount) values('L-16','Perryridge',1300);
insert into loan(loan_number,branch_name,amount) values('L-17','Downtown',1000);
insert into loan(loan_number,branch_name,amount) values('L-23','Redwood',2000);
insert into loan(loan_number,branch_name,amount) values('L-93','Mianus',500);
insert into borrower(customer_name,loan_number) values('Adams','L-16');
insert into borrower(customer_name,loan_number) values('Curry','L-93');
insert into borrower(customer_name,loan_number) values('Hays','L-15');
insert into borrower(customer_name,loan_number) values('Jones','L-17');
insert into borrower(customer_name,loan_number) values('Smith','L-11');
insert into borrower(customer_name,loan_number) values('Smith','L-23');
insert into borrower(customer_name,loan_number) values('Williams','L-17');
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 17
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];
SQL> desc student
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO CHAR(10)
FIRST_NAME CHAR(30)
MIDDLE_NAME CHAR(30)
LAST_NAME CHAR(30)
ADDRESS CHAR(50)
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
MOBILE_NUMBER CHAR(10)
SQL> insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,mobil
e_number) values('&roll_no','&first_name','&middle_name','&last_name','&address','&city',&pincode,'&
state',&mobile_number);
Enter value for roll_no: TECSE-16
Enter value for first_name: Namrata
Enter value for middle_name: M
Enter value for last_name: Bura
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 18
Enter value for address: Bhadrawati peth
Enter value for city: Solapur
Enter value for pincode: 413005
Enter value for state: Maharashtra
Enter value for mobile_number: 9421067511
old 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
new 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
1 row created.
SQL> /
Enter value for roll_no: TECSE-26
Enter value for first_name: Pradnya
Enter value for middle_name: N
Enter value for last_name: Dudam
Enter value for address: Sakhar Peth
Enter value for city: Solapur
Enter value for pincode: 413005
Enter value for state: Maharashtra
Enter value for mobile_number: 7745865343
old 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
new 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
1 row created.
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
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 19
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-26 Pradnya N
Dudam
Sakhar Peth Solapur
413005 Maharashtra 7745865343
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
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 20
DELETE Command The syntax for the DELETE statement in SQL is:
DELETE FROM table
[WHERE conditions];
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
SQL> delete from student where roll_no='TECSE-26';
1 row deleted.
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 21
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata Minesh
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 22
Practice Problem Statement
The instructor relation The course relation
The prereq relation The department relation
The section relation The teaches relation
The classroom relation The department relation The advisor relation
Mrs. Sunita M Dol, CSE Dept
SQL Manual Page 23
The student relation
The takes relation The time_slot relation
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/