INTRODUCTION
SQL: It is structured query language, basically used to pass the query to retrieve and manipulate the
information from database. Depending upon the nature of query, SQL is divided into different
components:
SQL is divided into the following
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Retrieval Language (DRL)
Transaction Control Language (TCL)
Data Control Language (DCL)
DDL -- create, alter, drop, truncate, rename
DML -- insert, update, delete
DRL -- select
TCL -- commit, rollback, savepoint
DCL -- grant, revoke
DDL: The Data Definition Language (DDL) is used to create the database (i.e. tables, keys, relationships
etc), maintain the structure of the database and destroy databases and database objects.
Eg. Create, Drop, Alter, Rename, Truncate
i. CREATE TABLE : It is used to create the table.
SYNTAX
Create table <table_name> (col1 datatype1(size),
col2 datatype2(size),
…
coln datatypen(size));
Ex: SQL> create table student (no number (2) ,
name varchar (10) ,
marks number (3));
ii. DROP statements: To destroy an existing database, table, index, or view. If a table is dropped all
records held within it are lost and cannot be recovered.
Syntax: DROP TABLE table_name;
1
iii. ALTER statements: To modify the structure of an existing database object.
a. Adding new columns:
Syntax:
Alter table table_name Add(New_columnName1 datatype(size), New_columnName2
datatype(size), ………);
b. Dropping a columns from a table :
Syntax:
Alter table table_name DROP column columnName
c. Modifying Existing columns:
Syntax:
Alter table table_name Modify (columnName1 New datatype(Newsize));
USING ALTER
This can be used to add or remove columns and to modify the precision of the datatype.
a) ADDING COLUMN
Syntax: alter table <table_name> add <col datatype>;
Ex: SQL> alter table student add sdob date;
b) REMOVING COLUMN
Syntax: alter table <table_name> drop <col datatype>;
Ex: SQL> alter table student drop column sdob;
c) INCREASING OR DECREASING PRECISION OF A COLUMN
Syntax: alter table <table_name> modify <col datatype>;
Ex: SQL> alter table student modify marks number(5);
*Note: To decrease precision the column should be empty.
d) RENAMING COLUMN
Syntax: alter table <table_name> rename column <old_col_name> to <new_col_name>;
Ex: SQL> alter table student rename column marks to smarks;
2
iv.Truncate statements: To destroy the data in an existing database, table, index, or view. If a table is
truncated all records held within it are lost and cannot be recovered but the table structure is
maintained.
Syntax : TRUNCATE TABLE table_name;
v. USING RENAME
This will be used to rename the database object;
Syntax: rename <old_table_name> to <new_table_name>;
Ex: SQL> rename student to stud;
***
3
Week 1- Lab Exercises
Aim : Creation of Tables: Client_master, Product_master, Sales_master using DDL commands
Procedure:
Step 1: Creation of Table
CREATE TABLE CLIENT_MASTER(
CLIENT_NO CHAR(6) PRIMARY KEY,
NAME VARCHAR2(20) NOT NULL,
ADDRESS VARCHAR2(100),
CITY VARCHAR2(15),
PINCODE NUMBER(6),
STATE VARCHAR2(20),
BAL_DUE(8,2));
Step 2: Inserting records into the Table CLIENT_MASTER table:
INSERT INTO Client_Master(ClientNo,Name,City,PinCode,State,BalDue)
VALUES('C00001','Ivan Bayross','Mumbai',400054,'Maharashtra',15000);
INSERT INTO Client_Master(ClientNo,Name,City,PinCode,State,BalDue)
VALUES('C00002','Mamta Muzumdar','Madras",780001,'Tamil Nadu',0);
Modifying the Table definition:
To change the format of an existing table , we can use ALTER command
4
Sample queries
Q1: Retrieve the names, city and state of all the clients.
Solution: SELECT NAME, CITY, STATE FROM CLIENT_MASTER;
Q2: Print the structure of Client_Master table.
Solution: DESC CLIENT_MASTER;
Q3: List the names of all clients having ‘a’ as the second letter in their names.
Solution: SELECT NAME FROM CLIENT_MASTER WHERE NAME LIKE ‘_a%’;
Q4: List the names of all clients having ‘a’ as the second letter in their city name.
Solution: SELECT NAME FROM CLIENT_MASTER WHERE CITY LIKE ‘_a%’;
Q5: List all the clients who stay in ‘Bombay’ or ‘Delhi’.
Solution: SELECT NAME FROM CLIENT_MASTER WHERE CITY = ‘Bombay’ OR ‘Delhi’;
Q6: List all the clients who are located in Madras.
Solution:SELECT NAME FROM CLIENT_MASTER WHERE CITY = ‘Madras’;
Q7: List all clients whose Bal_Due is greater than the value 1000.
5
Solution: SELECT NAME FROM CLIENT_MASTER WHERE BAL_DUE > 1000;
Q8: List the names, city and state of clients who are not in the state of ‘Maharashtra’.
Solution: SELECT NAME, CITY, STATE FROM CLIENT_MASTER WHERE NOT STATE =
‘Maharashtra’;
------------------------------------------------------------------------------------------------------------------------------
B) Creation of table PRODUCT_MASTER
Step 1:
Table Name: PRODUCT_MASTER
CREATE TABLE PRODUCT_MASTER(PRODUCTNO varchar2(6),
DESCRIPTION varchar2(15),
PROFITPERCENT number(4,2),
UNITMEASURE varchar2(10),
QTYONHAND number(8),
REORDERLVL number(8),
SELLPRICE number(8,2),
COSTPRICE number(8,2));
Step 2:
Inserting records into the table:
INSERT INTO Product_Master VALUES('P00001','T-Shirts',5,'Piece',200,50,350,250);
INSERT INTO Product_Master VALUES('P03453','Shirts',6,'Piece',150,50,500,350);
INSERT INTO Product_Master VALUES('P06734','Cotton Jeans',5,'Piece',100,20,600,450);
INSERT INTO Product_Master VALUES('P07865','Jeans',5,'Piece',100,20,750,500);
------------------------------------------------------------------------------------------------------------
6
C) Creation of Table Salesman_master:
CREATE TABLE SALESMAN_MASTER(
SALESMAN_NO CHAR(6),
SALESMANNAME VARCHAR2(20),
ADDRESS1 VARCHAR2(30),
ADDRESS2 VARCHAR2(30),
CITY VARCHAR2(20),
PINCODE NUMBER(6),
STATE VARCHAR2(20),
SALAMT NUMBER(8,2),
TGTTOGET NUMBER(6,2),
YTDSALES NUMBER(6,2),
REMARKS VARCHAR2(60));
INSERT THE FOLLOWING DATA INTO THE TABLE:
SALESMAN SALESMANN ADDRE ADDRE CITY PINCO STATE SALA TGTTO YTDSA REMAR
_NO AME SS1 SS2 DE MT GET LES KS
S00001 Aman A/14 Worli Mum 4000 Maharas 3000 100 50 Good
bai 02 htra
S00002 Omkar 65 Narim Mum 4000 Maharas 3000 200 100 Good
an bai 01 htra
S00003 Raj P-7 Bandr Mum 4000 Maharas 3000 200 100 Good
a bai 32 htra
S00004 Ashish A/5 Juhu Mum 4000 Maharas 3500 200 150 Good
bai 44 htra
7
Sample Queries:
***