SQL Basic & DDL Commands
SQL Basic & DDL Commands
04/21/19
100 John 50000 Manager
94
06/20/19
101 Greg 25000 Clerk
94
Query:
SELECT empname,salary FROM emp;
Output:
EMPNAME SALARY
John 50000
Greg 25000
4. Modify values
Query:
UPDATE emp SET salary = salary + 1000;
Output:
2 row(s) updated.
Query:
SELECT * FROM emp;
Output:
04/21/199
100 John 51000 Manager
4
06/20/199
101 Greg 26000 Clerk
4
5. Delete values
Query:
DELETE FROM emp WHERE empno = 100;
Output:
1 row(s) deleted.
Query:
SELECT * FROM emp;
Output:
RESULT:
Thus the basic SQL queries were successfully executed and verified.
Ex. No: 2 DATA DEFINITION LANGUAGE (DDL)
Date :
AIM:
To write the SQL queries using DDL Commands with and without
constraints.
DDL STATEMENTS
CREATE TABLE
ALTER TABLE
DROP TABLE
SYNTAX:
1. Create Table
The CREATE TABLE statement is used to create a relational table
CREATE TABLE table_name
(
column_name1 data_type [constraints],
column_name1 data_type [constraints],
column_n
ame1 data_type [constraints],
……..
);
2. Alter Table
The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table
a. To Add a column
ALTER TABLE table_name ADD column_name datatype
3. Drop Table
Used to delete the table permanently from the storage
DROP TABLE table_name
QUERIES:
1. CREATE THE TABLE (with no constraint)
Query:
CREATE TABLE emp
(
empno NUMBER,
empname VARCHAR2(25),
dob DATE,
salary NUMBER,
designation VARCHAR2(20)
);
Output:
Table Created
Query:
DESC emp;
Output:
Co
Ta m
ble Colum Data Len Preci Sc Primar Null Def me
n Type gth sion ale y Key able ault nt
EM EMPN NUMB
22 - - - - -
P O ER
EMPNA VARCH
255 - - - - -
ME AR2
DOB DATE 7 - - - - -
SALAR NUMB
22 - - - - -
Y ER
DESIG
VARCH
NATIO 20 - - - - -
AR2
N
Query:
DESC emp;
Output:
Ta
ble Colum Data Len Preci Sc Primar Null Def Com
n Type gth sion ale y Key able ault ment
EM EMPN NUMB
22 - - - - -
P O ER
EMPNA VARCH
255 - - - - -
ME AR2
DOB DATE 7 - - - - -
SALAR NUMB
22 - - - - -
Y ER
DESIG
VARCH
NATIO 20 - - - - -
AR2
N
DEPAR VARCH
50 - - - - -
TMENT AR2
b. MODIFY
//To alter the table emp by modifying the size of the attribute
department
Query:
ALTER TABLE emp MODIFY (department VARCHAR2(100));
Output:
Table Altered
Query:
DESC emp;
Output:
Com
Ta Data Len Preci Sc Primar Null Def men
ble Column Type gth sion ale y Key able ault t
EM NUMB
EMPNO 22 - - - - -
P ER
EMPNA VARC
255 - - - - -
ME HAR2
DOB DATE 7 - - - - -
NUMB
SALARY 22 - - - - -
ER
DESIGN VARC
20 - - - - -
ATION HAR2
DEPAR VARC
100 - - - - -
TMENT HAR2
c. DROP
// To alter the table emp by deleting the attribute department
Query:
ALTER TABLE emp DROP(department);
Output:
Table Altered
Query:
DESC emp;
Output:
Com
Ta Data Len Preci Sc Primar Null Def men
ble Column Type gth sion ale y Key able ault t
EM NUMB
EMPNO 22 - - - - -
P ER
EMPNA VARC
255 - - - - -
ME HAR2
DOB DATE 7 - - - - -
NUMB
SALARY 22 - - - - -
ER
DESIGN VARC
20 - - - - -
ATION HAR2
d. RENAME
// To alter the table name by using rename keyword
Query:
ALTER TABLE emp RENAME TO emp1 ;
Output:
Table Altered
Query:
DESC emp1;
Output:
Pre
Ta Colum Data Len cis Sc Prima Null Def Com
ble n Type gth ion ale ry Key able ault ment
EM EMPN NUMB
22 - - - - -
P1 O ER
EMPN VARC
255 - - - - -
AME HAR2
DOB DATE 7 - - - - -
SALAR NUMB
22 - - - - -
Y ER
DESIG
VARC
NATIO 20 - - - - -
HAR2
N
DEPAR VARC
100 - - - - -
TMENT HAR2
3. DROP
//To delete the table from the database
Query:
DROP TABLE emp1;
Output:
Table Dropped
Query:
DESC emp1;
Output:
Ouubject to be described could not be found.
CONSTRAINT TYPES:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
QUERIES:
1. CREATE THE TABLE
Query:
CREATE TABLE student
(
studentID NUMBER PRIMARY KEY,
sname VARCHAR2(30) NOT NULL,
department CHAR(5),
sem NUMBER,
dob DATE,
email_id VARCHAR2(20) UNIQUE,
college VARCHAR2(20) DEFAULT 'MEC'
);
Output:
Table created.
Query:
DESC student;
Output:
Query:
CREATE TABLE exam
(
examID NUMBER ,
studentID NUMBER REFERENCES student(studentID),
department CHAR(5) NOT NULL,
mark1 NUMBER CHECK (mark1<=100 and mark1>=0),
mark2 NUMBER CHECK (mark2<=100 and mark2>=0),
mark3 NUMBER CHECK (mark3<=100 and mark3>=0),
mark4 NUMBER CHECK (mark4<=100 and mark4>=0),
mark5 NUMBER CHECK (mark5<=100 and mark5>=0),
total NUMBER,
average NUMBER,
grade CHAR(1)
);
Output:
Table created.
Query:
ALTER TABLE student ADD CONSTRAINT pr
PRIMARY KEY (examid);
Output:
Table altered.
Query:
CREATE TABLE stu_details
(
reg_no number,
stu_name varchar2(30),
DOB date,
address varchar2(30),
city char(30),
primary key(reg_no, stu_name)
);
Output:
Table created.
Query:
DESCstu_details
Output:
RESULT:
Thus the SQL queries using DDL Commands with and without
constraints were successfully executed and verified.