CH 12 Structured Query Language Part 01
CH 12 Structured Query Language Part 01
•Removing the definition of database objects •Inserting new records into a table or relation
from the database through DROP statement. through INSERT commands.
NOTE : The definitions of all the database •Modifying existing records through UPDATE
objects is stored in a permanent table called statement.
Data – Dictionary. Thus, Data – Dictionary
•Deleting unwanted records through DELETE
stores data about data called “Meta – Data”.
statement.
Data – Manipulation Language (DML) :
DML Commands are subset of SQL
Types of DML :
commands. These commands are used to
query and manipulate the database. Functions DML is of two types :
performed by DML commands can be
described as follows :
5 6
1
7/18/2023
Here, SIZE is optional. When size is specified, Remember that a maximum size must be
it implies to fixed length character data of specified when we declare an attribute as
length : size. Minimum and Maximum value for VARCHAR2 data type. Minimum and Maximum
the size are 1 and 2000 respectively. It is used value for the size are 1 and 4000 respectively.
mainly for attributes such as Item_code, the It is used mainly for attributes such as Name
length of which will remain same for all the of Employee, Name of Department, the length
items. of which varies frequently.
( c. ) VARCHAR2 : ( d. ) DATE :
VARCHAR2 data type contain varying length DATE is a data type which is used to store date
character data. and time values ranging from January 1, 4712
B.C. to December 31, 9999. It is defined as,
It is specified as,
DATE
VARCHAR2 (SIZE) 11 12
2
7/18/2023
When an attribute such as hire_date, For Ex : SQL > SELECT sysdate FROM dual ;
date_of_admission are declared to be of DATE
Here, SELECT & FROM SQL keywords.
data – type, date is stored in a standard format
which is : Sysdate Pre – defined function
DD – MON – RR (As in Oracle) Dual dummy table to complete the syntax of
SQL.
Where ,
3
7/18/2023
21 22
For Ex: rows of the table can have duplicate values for
MySQL > CREATE TABLE Student the column(s) constrained as UNIQUE. UNIQUE
( Name Varchar2 (25) NOT NULL , constraint can be defined at the column or table
23 24
4
7/18/2023
PRIMARY KEY constraint is applied on the A composite PRIMARY KEY constraint can only
be defined at the table level.
Rollno column because NULL values should
For Ex:
not be allowed. Also, duplicate value for Rollno
MySQL > CREATE TABLE Student
is not allowed.
( Rollno Number (3) ,
Name Varchar2 (25) ,
Class NUMBER (2) ,
PRIMARY KEY (Name , Class ) );
29 30
5
7/18/2023
The FOREIGN KEY ( or Referential integrity Following is the SQL command for applying
FOREIGN KEY constraint on DeptNo of
constraint ) designates a column or
EMPLOYEE at column level.
combination of columns as a Foreign key and
For Ex: MySQL > CREATE TABLE EMPLOYEE
establishes its relationship with a Primary key ( Empno Number (3) ,
or unique key in a different (or sometimes even Ename Varchar2 (30) ,
same) table. Job Varchar2 (20) ,
Salary Number (7,2) ,
This relationship can be set when creating the
DeptNo Number (2)
table both at column level and table level. REFERENCES DEPARTMENT (DeptNo) ) ; 32
31
Table level : Following is the SQL command for (V.) CHECK Constraint :
applying FOREIGN KEY constraint on DeptNo CHECK constraints allows us to specify an
of EMPLOYEE at table level. expression for the column(s) that must be
For Ex: MySQL > CREATE TABLE EMPLOYEE satisfied by every value entered in that column.
For Ex :
( Empno Number (3) ,
MySQL > CREATE TABLE EMPLOYEE
Ename Varchar2 (30) ,
Job Varchar2 (20) , ( Empno Number (3) ,
Salary Number (7,2) , Ename Varchar2 (30) ,
DeptNo Number (2) Job Varchar2 (20) ,
FOREIGN KEY (DeptNo) REFERENCES
Salary Number (8,2) CHECK (Salary >0 ) ,
DEPARTMENT (DeptNo) );
33 DeptNo Number (2) ); 34
It can also be defined at the table level if A VIEW is defined as a logical table based
condition to be satisfied involves more than on a database table or another view. A VIEW
contains no data of its own but it acts like a
one column. window through which data from a table can be
35
viewed or changed. 36
6
7/18/2023
39
MySQL > DROP TABLE Employee ; 40
41 42
7
7/18/2023
SYNTAX :
( 2.) MySQL > ALTER TABLE Employee
MySQL > ALTER TABLE <table_name>
ADD commission INT
ADD <column_name> <data_type>
CHECK (commission <= 500) ;
[ constraint definition ] ;
43 44
SYNTAX :
MySQL > ALTER TABLE <table_name> ( 1.) ALTER TABLE Employee
MODIFY ( <column_name> MODIFY ( Salary NUMBER (8,2) ) ;
<data_type> [ (SIZE) ] ) ;
45 46
8
7/18/2023
(2. ) Data – Manipulation Language (DML) Tables can be updated in one of the five
commands : ways :
manipulate existing objects of a database such ( II.) Updating multiple rows and single column.
For Ex :
SYNTAX : SPORTS
RollNo Class Name Game Grade Marks
13 9 Veena Tennis C 49
51 52
( I. ) When updating single row and single ( II. ) When updating multiple rows and single
column : To change the marks of student column :
with Roll Number 12 from 60 to 65, SQL
If you want to modify more than one row
command is:
in a table, then either omit WHERE clause or
MySQL > UPDATE SPORTS use the non – primary key column in WHERE
SET Marks = 65 clause.
WHERE RollNo = 12 ;
For Example :
Result :
To set the Grade of all students in
RollNo Class Name Game Grade Marks
SPORTS to ‘A’ , SQL command is :
12 7 Kamal Swimming B 65
MySQL > UPDATE SPORTS
Only updated record is shown. 53
SET Grade = ‘A’ ; 54
9
7/18/2023
55 56
57 58
12 7 Kamal Swimming 60
MySQL > UPDATE SPORTS
13 9 Veena Tennis 49
SET Grade = NULL ;
59 60
10
7/18/2023
INSERT command is used to store data When we omit column list in the INSERT
clause, values listed in the VALUES clause
into a table. should be according to the order and data type
of column in the table. Further, a value must be
NOTE : (Table SPORTS is considered ) provided for each column in the table.
MySQL > SELECT * FROM SPORTS ; ( 2. ) Listing columns in the INSERT clause :
Result : We may optionally list the columns in
RollNo Class Name Game Grade Marks
INSERT clause, when we do not want to supply
values for all the columns.
10 7 Sameer Cricket B 61
11 8 Sujit Tennis A 75 For Ex : To add a record :
12 7 Kamal Swimming B 60 {14, 9, ‘Sumit’ , ‘Basketball’ , NULL, NULL}
13 9 Veena Tennis C 49 in the SPORTS table, SQL statement is :
15 10 Arpit Cricket A 78 MySQL > INSERT INTO SPORTS (RollNo, Class,
Name, Game )
NOTE : Character and Date data – type
values must be enclosed in single quotes. VALUES (14, 9, ‘Sumit’, ‘Basketball’ ) ;
63 64
Result :
MySQL > INSERT INTO SPORTS
RollNo Class Name Game Grade Marks
10 7 Sameer Cricket B 61 VALUES (14, 9, ‘Sumit’, ‘Basketball’,
11 8 Sujit Tennis A 75
NULL, NULL ) ;
12 7 Kamal Swimming B 60
13 9 Veena Tennis C 49
NOTE : Column with NOT NULL constraint must
15 10 Arpit Cricket A 78
14 9 Sumit Basketball be supplied a value.
65 66
11
7/18/2023
table related with Swimming, the SQL command DELETE [FROM] <table_name> ;
69 70
71
12