0% found this document useful (0 votes)
0 views12 pages

CH 12 Structured Query Language Part 01

The document provides an overview of Structured Query Language (SQL), detailing its role in querying and modifying relational databases. It categorizes SQL statements into five types: Data Definition Language (DDL), Data Retrieval Commands, Data Manipulation Language (DML), Transaction Control Commands, and Data Control Language (DCL). Additionally, it explains various SQL commands, data types, constraints, and the creation and management of tables and views.

Uploaded by

s6972459
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views12 pages

CH 12 Structured Query Language Part 01

The document provides an overview of Structured Query Language (SQL), detailing its role in querying and modifying relational databases. It categorizes SQL statements into five types: Data Definition Language (DDL), Data Retrieval Commands, Data Manipulation Language (DML), Transaction Control Commands, and Data Control Language (DCL). Additionally, it explains various SQL commands, data types, constraints, and the creation and management of tables and views.

Uploaded by

s6972459
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

7/18/2023

Structured Query Language (SQL) is a


Chapter : 12 database sub – language for querying and
modifying Relational Databases. SQL is a
standard database language for use with
Relational Data – Base Management Systems
Structured Query Language (RDBMSs).

(SQL) SQL Statements :


SQL statements can be divided into following
five categories :
Made By : (1.) Data – Definition Language (DDL)
SATISH MAMNANI Commands.
1 CREATE , ALTER , DROP , RENAME etc. 2

(2.) Data Retrieval Commands. Data – Definition Language (DDL) :


SELECT Command. DDL Commands are subset of SQL
commands. These commands are used to
(3.) Data – Manipulation Language (DML)
perform three main functions on database
Commands.
objects (such as tables, views etc. ).
INSERT , UPDATE , DELETE etc.
(4.) Transaction Control Commands.
These functions are :
COMMIT, ROLLBACK, SAVEPOINT etc.
•Defining or creating database objects through
(5.) Data – Control Language (DCL) CREATE command.
Commands.
•Modifying the structure or definition of
GRANT , REVOKE etc. database objects through ALTER statement.
3 4

•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

( a. ) Procedural DML : SQL Data Types :


As the name specifies , procedural DML not Data can be classified into different data –
only requires a user to specify what data is types based on their characteristics.
needed, but also the procedure of how to
( a. ) NUMBER :
retrieve the data.
It may contain a number with or without a
( b. ) Non – Procedural DML :
decimal point and a sign. Numbers can be
Non – Procedural DML requires a user only to specified in three forms as :
specify what data is needed without giving
( I. ) NUMBER :
details of procedure to retrieve that data.
In this form we don’t specify the width or
DML Commands : INSERT, UPDATE, DELETE,
precision. Default precision will be taken.
SELECT commands come under Non –
Procedural DML. 7 8

( II. ) NUMBER(P) : It is used mainly for attributes where decimal


numbers are required, such as salary,
In this form, we specify the width or precision
percentage etc.
without any decimal point. This form is mainly
used for attributes such as DeptNo, Age, ( b. ) character :
Empno etc.
Character data type contain fixed length
( III. ) NUMBER(P,S) : character data. It is abbreviated as CHAR and
is specified as,
In this form, we specify both precision P and
scale S (the precision P is total number of CHARACTER [ (SIZE) ]
decimal digits and scale S is the number of
{or}
digits to the right of decimal point).
char [ (SIZE) ]
The precision can range from 1 to 38 and the
scale can range from –84 to 127. 9 10

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 ,

For Ex : SQL > SELECT 12 * 12 FROM dual;


DD  Day of the Month
* Dummy table ‘dual’ is used to see current date
MON  Abbreviated three letters form of month
or time or to perform simple calculations and
RR  Year of the Century. get their result on SQL prompt.
13 14

SQL Commands : SQL > SELECT * FROM Employee ;


Two main points you must remember about ( or )
SQL commands are :
SQL > SELECT *
(1.) Every SQL command must be terminated
FROM Employee ;
with a semi – colon ( ; ).
( 1. ) DDL Commands :
(2.) SQL is a case – insensitive language.
( a. ) CREATE TABLE Command :
NOTE :
All data in a Relational Database is stored in the
SQL commands consist of one or more
form of relations, also called tables. Each table
clauses. Clauses begin with a keyword and
in the database is created using CREATE
consist of keywords and arguments. For Ex:
TABLE command. It is a DDL command.
15 16

Ex : (1.) Create a table EMPLOYEE which


Syntax : has the following columns :
CREATE TABLE <table name> Column Data Type Description of Column
Name
( <column name> <data type> [ (SIZE) ] EmpNo Number(3) Employee Identity
[ , <column name> <data type> [ (SIZE) ]…] ); Number
Ename Varchar2(30) Employee Name
NOTE :
Hire_Date Date Date of Joining of
When a table is created, it must have at least Employee
one column. Also, at this point, table is empty,
Job Varchar2(15) Job title of employee
it has no data. Only the definition of table is
stored in the database. This definition is called Salary Number(7,2) Basic Salary of
‘schema’ of that table. Employee
DeptNo Number(2) Department Number of
17 Employee 18

3
7/18/2023

Sol: SQL command for creating the above Constraints: ( or Restrictions ) :


defined EMPLOYEE table is :
Constraints are the means by which you can
MySQL > CREATE TABLE Employee prevent invalid data entry into the table.
( Empno Number (3) , Types of Constraints :
EName Varchar2 (30) , (1.) NOT NULL
Hire_Date Date , (2.) UNIQUE
Job Varchar2 (15) , (3.) PRIMARY KEY
Salary Number (7,2) , (4.) FOREIGN KEY
DeptNo Number (2) ); (5.) CHECK
19 20

You can create a constraint either : (A.) Column Level :


A constraint is defined at column level when it
is to be applied on a single column. All the
At the same time when the table is created
constraints can be defined at this level.
using CREATE TABLE command or
After the table has been created , using
ALTER TABLE command. (B.) Table Level :
A constraint is defined at table level when it is
to be applied on more than one column. All the
NOTE : Constraints can be defined at two levels
constraints except NOT NULL constraint can be
when the table is created.
defined at table level.

21 22

(1.) NOT NULL : (2.) UNIQUE :


NOT NULL constraint on a column ensures that The UNIQUE constraint specifies a column or
the column cannot contain a NULL value. It can
be defined only at the column level. combination of columns whose value must be
unique for all rows in the table, that is, no two

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

Class NUMBER (2) ); level.

23 24

4
7/18/2023

Column Level : Table Level :


UNIQUE constraint is defined at column level UNIQUE constraint is defined at table level
when it references a single column. when it references one or more columns.
For Ex:
For Ex: MySQL > CREATE TABLE Student
MySQL > CREATE TABLE Student ( Rollno Number (3) ,
( Rollno Number (3) UNIQUE , Name Varchar2 (25) NOT NULL ,
Name Varchar2 (25) NOT NULL , Class NUMBER (2)
Class NUMBER (2) ); UNIQUE (Rollno) );
25 26

NOTE : Column Level :


Column(s) included in UNIQUE constraints PRIMARY KEY constraint is defined at column
definition is called ‘unique key’. level when it references a single column.
(III.) PRIMARY KEY :
The PRIMARY KEY constraint is a column or a For Ex:
set of columns that ensures two things :
MySQL > CREATE TABLE Student
Unique identification of each row in the table.
( Rollno Number (3) PRIMARY KEY ,
No column that is part of the PRIMARY KEY
constraint can contain a NULL value. Name Varchar2 (25) ,
This constraint can be defined at the column or Class NUMBER (2) );
table level. 27 28

NOTE : Table Level :

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

(IV.) FOREIGN KEY : Column Level : (Tables : EMPLOYEE & DEPARTMENT)

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

NOTE – 1 : ( b. ) CREATE VIEW :

Now you cannot enter zero or negative value


for Salary of an Employee. One of the main function of a DBMS is to
provide security to the database. VIEW’s are
powerful database objects to provide security
to the database.
NOTE – 2 :

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

VIEWs are the means by which


information relevant to a person is only
For Ex :
presented and rest of the information is hidden
from him / her.
SYNTAX : MySQL > CREATE VIEW EmpView
AS SELECT EmpNo, Ename, Job
MySQL > CREATE VIEW <view name> FROM EMPLOYEE ;
AS SELECT <column list>
FROM <table name> MySQL > SELECT * FROM EmpView ;
[ WHERE <condition> ] ;
37 38

Advantages of Views : ( c. ) DROP TABLE :


Dropping a table not only deletes the data
contained in the table but it also removes the
(1.) Restricts data access thus provide security definition of its structure (i.e. name of the table
to the data. and information about its columns) from the
database. It is a DDL statement.
(2.) Makes complex queries easy. SYNTAX :

(3.) Allows data – independence. MySQL > DROP TABLE <table_name> ;

(4.) Present different views of the same data.


For Ex :

39
MySQL > DROP TABLE Employee ; 40

( d. ) DROP VIEW : ( e. ) ALTER TABLE :


A view contains no data of its own,
therefore when a view is dropped no loss of
Tables can be altered in one of the three
data is involved.
ways :
SYNTAX :
MySQL > DROP VIEW <view_name> ;
( I. ) By adding a column to an existing table.
( II.) By changing a column’s definition (data –
For Ex :
type ).
MySQL > DROP VIEW Empview ;
(III.) By dropping a column of the table.

41 42

7
7/18/2023

( I. ) Adding Column to an existing table : For Ex :


An existing table can be altered , i.e. the
definition of a table can be changed by adding
( 1.) MySQL > ALTER TABLE Employee
a new column in an existing table.
ADD DOJ Date ;

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

( II. ) Changing a Column’s definition : For Ex :


Changing a column’s definition include To change the definition of column salary
changing the data – type or size of an existing
column. in EMPLOYEE table from NUMBER(7,2) to
NUMBER(8,2), the SQL command is :

SYNTAX :
MySQL > ALTER TABLE <table_name> ( 1.) ALTER TABLE Employee
MODIFY ( <column_name> MODIFY ( Salary NUMBER (8,2) ) ;
<data_type> [ (SIZE) ] ) ;

45 46

( III. ) Dropping a column from the Table : For Ex :


A column is dropped from the table if it is ( 1.) To drop the column Grade from the
no longer required. student table, SQL command is :
SYNTAX : ALTER TABLE <table_name> MySQL > ALTER TABLE student
DROP COLUMN <column_name> ; DROP COLUMN Grade ;
You can also drop more than one column
from a table with single statement :
( 2.) To drop the column DOJ from the
Syntax for this is : Employee table, SQL command is :
ALTER TABLE <table_name> MySQL > ALTER TABLE Employee
DROP ( <column1> [, <column2>… ); DROP COLUMN DOJ ;
47 48

8
7/18/2023

(2. ) Data – Manipulation Language (DML) Tables can be updated in one of the five
commands : ways :

DML commands are the most commonly


used SQL commands. These are used to ( I. ) Updating single row and single column.

manipulate existing objects of a database such ( II.) Updating multiple rows and single column.

as tables. (III.) Updating multiple columns and single row.


(IV.) Updating multiple columns and multiple
( a. ) UPDATE …. SET …. :
rows.
UPDATE statement changes or modifies
( V.) Setting column values to NULL.
the value of specified column in a table.
49 50

For Ex :
SYNTAX : SPORTS
RollNo Class Name Game Grade Marks

MySQL > UPDATE <table_name>


10 7 Sameer Cricket B 61
SET <column_name> = <value>
11 8 Sujit Tennis A 75
[ , <column_name> = <value> … ]
[ WHERE <condition> ] ; 12 7 Kamal Swimming B 60

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

( III. ) Updating multiple columns and single


Result : row :
For Ex : To change the game to cricket and
RollNo Class Name Game Grade Marks class to 8 of student with Roll Number 13, the
SQL command is :
10 7 Sameer Cricket A 61 MySQL > UPDATE SPORTS
11 8 Sujit Tennis A 75 SET Class = 8 , Game = ‘Cricket’
WHERE RollNo = 13 ;
12 7 Kamal Swimming A 65 Result :
13 9 Veena Tennis A 49 RollNo Class Name Game Grade Marks
13 8 Veena Cricket C 49

55 56

( IV. ) Updating multiple columns and multiple


rows : MySQL > UPDATE SPORTS
SET Grade = ‘A’ , Marks=Marks + 10
To update multiple rows, you can simply
WHERE Game = ‘Tennis’ ;
include the column which does not identify
each row uniquely in the WHERE clause or
omit the WHERE clause. Result :
For Ex :
RollNo Class Name Game Grade Marks
To increase Marks by 10 and changing
the Grade to ‘A’ of all the students who play 11 8 Sujit Tennis A 85
Tennis, SQL command is :
13 9 Veena Tennis A 59

57 58

( V. ) Setting Column values to NULL :


Result :
We can replace the values in a column by
NULLs as any other value. RollNo Class Name Game Grade Marks
For Ex :
10 7 Sameer Cricket 61
To set the Grade of all students to NULL,
SQL command is : 11 8 Sujit Tennis 75

12 7 Kamal Swimming 60
MySQL > UPDATE SPORTS
13 9 Veena Tennis 49
SET Grade = NULL ;

59 60

10
7/18/2023

( b. ) INSERT… INTO command : ( 1. ) Omitting column list in INSERT clause :

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.

SYNTAX : For Ex : To INSERT following record :


{15, 10, ‘Arpit’ , ‘Cricket’ , ‘A’ 78}
INSERT INTO <table_name> [ <column_list> ] in SPORTS table, SQL command is :

VALUES ( <list of values> ) ; MySQL > INSERT INTO SPORTS


VALUES (15, 10, ‘Arpit’, ‘Cricket’ , ‘A’, 78 ) ;
61 62

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

NOTE : A NULL value will be inserted


automatically for the columns which we
have omitted in column list. Above SQL command can also be written as,

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

( c. ) DELETE statement: For Ex :


DELETE statement is used to delete rows (I. ) To delete all the records from the SPORTS
from a table.
table with their Grades equal to ‘C’ or NULL, the
SYNTAX :
SQL command is :
DELETE [FROM] <table_name>
MySQL > DELETE FROM SPORTS
[ WHERE <condition> ] ;
WHERE Grade = ‘C’ OR Grade IS NULL ;
( I. ) DELETE with WHERE clause :
SYNTAX :
DELETE [FROM] <table_name> NOTE : The operation IS NULL to check for

WHERE <condition> ; NULLs in the Grade column.


67 68

For Ex : ( II. ) DELETE without WHERE clause :

(II. ) To delete all the records from the SPORTS SYNTAX :

table related with Swimming, the SQL command DELETE [FROM] <table_name> ;

is : For Ex : To delete all the records from SPORTS


table, SQL commands is :
MySQL > DELETE FROM SPORTS ;
MySQL > DELETE FROM SPORTS Now,
WHERE Game = ‘Swimming’ ; MySQL > SELECT * FROM SPORTS ;
Will give message ‘No Rows Selected’.

69 70

71

12

You might also like