EX - NO:1a Data Definition Languages (DDL) Commands of Base Tables and Views
EX - NO:1a Data Definition Languages (DDL) Commands of Base Tables and Views
EX - NO:1a Data Definition Languages (DDL) Commands of Base Tables and Views
NO:1a
DATA DEFINITION LANGUAGES (DDL) COMMANDS Of Base Tables and Views
Aim:
DDL commands:
✴ CREATE
✴ ALTER
✴ DROP
✴ RENAME
✴ TRUNCATE
SYNTAX’S OF COMMANDS
CREATE TABLE:
To make a new database, table, index, or stored query. A create statement in SQL
creates an object inside of a relational database management system (RDBMS).
1
ALTER A TABLE:
To modify an existing database object. Alter the structure of the database.
To add a column in a table
ALTER TABLE table_name ADD column_name datatype;
To delete a column in a table
ALTER TABLE table_name DROP column column_name;
DROP TABLE:
Delete Objects from the Database
TRUNCATE TABLE:
Remove all records from a table, including all spaces allocated for the records are
removed.
EXERCISE:
Create Table
SQL> create table employee
2(
3 empid varchar(10) primary key,
4 empname varchar2(20) not null,
5 gender varchar2(7) not null,
6 age number(3) not null,
7 dept varchar2(15) not null,
8 dob date not null,
2
SQL> create table salary
2(
3 empid varchar(10) references employee(empid),
4 salary number(10) not null,
5 dept varchar(15) not null,
DESCRIBE TABLE
SQL> desc employee;
Name Null? Type
3
SQL> desc salary;
Name Null? Type
ALTER TABLE
I. ADD:
SQL> alter table employee add(designation varchar2(15));
Table altered.
II. MODIFY
SQL> alter table employee modify (designation varchar2(20));
Table altered.
4
RENAME TABLE
SQL> create table emp
2(
3 empid varchar2(10),
4 empname varchar2(20),
5 age number(3),
6 sex char
7 );
Table created.
EMPID VARCHAR2(10)
EMPNAME VARCHAR2(20)
AGE NUMBER(3)
SEX CHAR(1)
ERROR:
5
Enter value for dept: it
Enter value for age: 22
Enter value for sex: m
old 1: insert into emp values(&no,'&name','&dept',&age,'&sex')
1 arun it 22 m
6
2 bala service 26 m
3 chitra sales 25 f
SQL> commit;
Commit complete.
Commit complete.
DROP TABLE
RESULT:
7
EX.NO:1b
DATA MANIPULATION LANGUAGE (DML) OF BASE TABLES AND VIEWS
Data manipulation language allows the users to query and manipulate data in existing
schema in object. It allows following data to insert, delete, update and recovery data in
schema object.
Aim:
To study DML commands in RDBMS.
DML COMMANDS:
❖ INSERT
❖ UPDATE
❖ DELETE
❖ SELECT
QUERY:
Query is a statement in the DML that request the retrieval of data from database.
❖ The portion of the DML used in a Query is called Query language. The SELECT
statement is used to query a database
SYNTAX OF COMMANDS
INSERT:
Values can be inserted into table using insert commands. There are two types of insert
commands. They are multiple value insert commands (using ‘&’ symbol) single value insert
command (without using ‘&’symbol)
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3,…..);
(OR)
INSERT INTO table_name (column1, column2, column3,….) VALUES
(value1,value2,value3,…..);
8
UPDATE:
This allows the user to update the particular column value using the where clause
condition.
Syntax:
UPDATE <table_name> SET <col1=value> WHERE <column=value>;
DELETE:
This allows you to delete the particular column values using where clause condition.
Syntax:
SELECT:
The select statement is used to query a database .This statement is used to retrieve the
information from the database. The SELECT statement can be used in many ways. They are:
1. Selecting some columns :
To select specified number of columns from the table the
Syntax:
To select all columns from the table * is used instead of column names.
Syntax:
The DISTINCT keyword is used to return only different values (i.e. ) this
command does not select the duplicate values from the table.
Syntax:
9
4. Select using IN:
If you want to get the rows which contain certain values, the best way to do it
is to use the IN conditional expression.
Syntax:
BETWEEN can be used to get those items that fall within a range.
Syntax:
6. Renaming:
The select statement can be used to rename either a column or the entire
table.
Syntax:
Renaming a column:
Renaming a table:
7. Sorting:
The select statement with the order by Clause is used to sort the contents
Syntax:
10
8. To select by matching some patterns:
The select statement along with like clause I is used to match strings. The like
condition is used to specify a search pattern in a column.
Syntax:
SELECT column name FROM table_name WHERE Column name LIKE “% or-“;
The SELECT INTO statement is most often used to create backup copies of
tables or for archiving records.
Syntax:
WHERE condition.
We can use the SELECT statement to select the ‘null’ values also.
For retrieving roes where some of the columns have been defined as NULLs there is a special
comparison operator of the form IS [NOT]NULL.
Syntax:
Syntax:
11
EXERCISE:
INSERT COMMAND
12
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('it9001','arunkumar','male',22,'it','12-jan-1988','23-oct-
2006'
1 row created.
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: acc9001
13
Enter value for doj: 01-jan-1995
Enter value for desig: manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
1 row created.
14
Enter value for dept: service
Enter value for dob: 31-mar-1877
Enter value for doj: 3-jun-1999
Enter value for desig: manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('ser9001','jagadheesh','male',33,'service','31-mar-
1877','3-jun
1 row created.
15
Enter value for empname: suresh
Enter value for gender: male
Enter value for age: 40
Enter value for dept: sales
16
SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);
Enter value for empid: it9002
Enter value for salary: 18000
Enter value for dept: it
Enter value for branch: abt maruthi
17
Enter value for dept: service
Enter value for branch: chennai cars
old 1: insert into salary values('&empid',&salary,'&dept','&branch')
new 1: insert into salary values('ser9001',35000,'service','chennai cars')
1 row created.
18
new 1: insert into salary values('sal9006',17000,'sales ','abt maruthi')
1 row created.
8 rows selected.
DOJ DESIGNATION
19
DOJ DESIGNATION
DOJ DESIGNATION
20
it9001 35000 it abt maruthi
it9002 18000 it abt maruthi
acc9001 35000 accounts cars india
acc9002 26000 accounts cars india
ser9001 35000 service chennai cars
ser9006 12000 service greenland cars
sal9001 40000 sales abt maruthi
sal9006 17000 sales abt maruthi
8 rows selected.
21
1 row created.
UPDATE COMMAND
SQL> update employee set empname = 'arunprasanth' where empid='it9001';
1 row updated.
EMPNAME DESIGNATION
arunprasanth manager
balakrishnan coordinator
kannan manager
magudeshwaran asst manager
jagadheesh manager
22
muruganandam supervisor
suresh manager
sharmila executive
8 rows selected.
SELECT COMMAND
To retrieve particular column
SQL> select empname from emp;
EMPNAME
arun
bala
bakyaraj
chitra
1 arun it 22 m
2 bala accounts 26 m
3 bakyaraj stores 30 m
4 chitra sales 24 f
DELETE COMMAND
To delete particular record
SQL> delete emp where empid=1;
1 row deleted.
23
SQL> select * from emp;
EMPID EMPNAME DEPT AGE S
2 bala accounts 26 m
3 bakyaraj stores 30 m
4 chitra sales 24 f
Table created.
IDNO NUMBER
NAME VARCHAR2(10)
BRANCH VARCHAR2(4)
Table altered.
IDNO NUMBER
24
NAME VARCHAR2 (10)
IDNO NUMBER
1 row created.
1 row created.
1 row created.
25
Enter value for idno: 04
1 row created.
SQL> /
1 row created.
1 ASHOK CSE BE
2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT BE
5 HARRY IT BE
2 rows updated.
26
SQL> select * from student;
1 ASHOK CSE BE
2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT B.TECH
5 HARRY IT B.TECH
1 row deleted.
NOT NULL
1 ASHOK CSE BE
2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT B.TEC
H
varchar(10),branch varchar(6)
); Table created.
27
SQL> desc staff;
NAME VARCHAR2(10)
BRANCH VARCHAR2(6)
1 row created.
SQL> /
1 row created.
SQL> /
ERROR at line 1:
UNIQUE
name varchar(10),
salary number
);
Table created.
ROLLNO NUMBER
NAME VARCHAR2(10)
SALARY NUMBER
1 row created.
SQL> /
1 row created.
SQL> /
ERROR at line 1:
PRIMARY KEY
name varchar(10),
cost number(6)
);
Table created.
30
SQL> desc cars;
NAME VARCHAR2(10)
COST NUMBER(6)
1 row created.
SQL> /
1 row created.
SQL> /
31
insert into cars values(1098,'innova',600000)
ERROR at line 1:
CHECK CONSTRAINT:
rno number(5),
name varchar(10),
);
Table created.
RNO NUMBER(5)
NAME VARCHAR2(10)
SALARY NUMBER(10)
32
SQL> /
1 row created.
SQL> /
ERROR at line 1:
FOREIGN KEY
name varchar(10),
permit number(6)
);
Table created.
33
SQL> desc admin;
NAME VARCHAR2(10)
PERMIT NUMBER(6)
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
1 ASWIN 80
2 ROHIT 67
4 SANJAY 45
5 KAMALINI 35
branch varchar(6),
sec varchar(2)
);
Table created.
35
SQL> insert into course values(&stuid,'&branch','&sec');
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
36
Enter value for sec: A
ERROR at line 1:
1 row deleted.
ERROR at line 1:
1 ASWIN 80
2 ROHIT 67
4 SANJAY 45
SQL> select * from course;
STUID BRANCH SE
1 CSE A
2 CSE A
4 IT A
37
SQL> create table student
idno varchar(4),
name varchar(10),
dept varchar(4),
degree varchar(6),
year number(4)
);
table created.
IDNO VARCHAR2(4)
NAME VARCHAR2(10)
DEPT VARCHAR2(4)
DEGREE VARCHAR2(6)
YEAR NUMBER(4)
38
new 1: insert into student values('a01','aaron','cse','BE',2)
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
39
Enter value for year: 1
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
40
SQL> SELECT * FROM STUDENT;
6 rows selected.
DISTINCT
CSE
ECE
IT
MECH
AARON
AKIL
BENNY
COOK
DANNY
ELAN
6 rows selected.
41
IN
SQL> select * from student where name BETWEEN 'AARON' and 'COOK';
AS
A01
A02
A03
B01
B02
B03
6 rows selected.
SORT
42
B03 ELAN IT B.TECH 1
B02 DANNY MECH BE 1
B01 COOK CSE BE 1
A03 BENNY IT B.TECH 2
A02 AKIL ECE BE 2
A01 AARON CSE BE 2
6 rows selected.
LIKE
43
IS NULL
no rows selected
LOGICAL OR
RESULT:
Thus the data manipulation language (dml) of base tables and views are executed.
44