Structured Query Language
(SQL)
1
Attribute (or)
Field
Record (or)
Tuple
Fig: Sailors table 2
▪The SQL language has Three parts
-Data Definition Language (DDL)
-Data Manipulation Language (DML)
-Data Control Language(DCL)
Data Definition Language (DDL):
▪DDL supports the creation, deletion, and modification of
definitions for tables and views and indexes
▪Integrity constraints can be defined on tables, either when
the table is created or later
3
Data Manipulation Language (DML):
▪DML allows users to insert, delete, and modify records
Data Control Language (DCL):
▪DCL controls a database, including administrative
privileges and saving data
4
Data type
▪Specifies the kind of data that a field stores
Character data type
VARCHAR2
▪Stores variable-length character data up to a maximum of
4,000 characters
▪Values in different records can have a different number of
characters
5
Number data type
▪Stores negative, positive, fixed and floating point numbers
with precision up to 38 decimal places
Syntax: Fieldname NUMBER[(precision , scale)]
Integer: Fieldname NUMBER (precision)
Fixed Point: Fieldname NUMBER[(precision , scale)]
Date data type
Date
▪Dates from December 31,4712 BC to December 31,4712
AD 6
▪Default format DD-MON-YY
▪Default time format HH:MI:SS AM
Syntax: Fieldname DATE
DDL Commands
-CREATE
-ALTER
-DROP
-TRUNCATE
7
CREATE command
▪Used to create table
Syntax:
CREATE TABLE table-name (Fieldname1 data_type ,
Fieldname2 data_type , …. ….. ….)
Example
CREATE TABLE Sailors (sid NUMBER(2), sname
VARCHAR2(20), rating NUMBER(2))
ALTER command
Adding a new field in to the existing relation
8
▪All rows in the relation(table) are assigned ‘null’ as the
value for the new attributes
Syntax: ALTER TABLE tablename
ADD (fieldname Field_datatype)
Example: ALTER TABLE Sailors
ADD (age NUMBER(2))
Modifying an existing field
Syntax: ALTER TABLE tablename
MODIFY (fieldname new_field_datatype)
Example
ALTER TABLE Sailors MODIFY (age NUMBER(3,1))
9
Deleting an existing field
Syntax: ALTER TABLE tablename
DROP COLUMN Fieldname
Example: ALTER TABLE Sailors DROP COLUMN age
DROP command
▪Used to delete an existing table
Syntax: DROP TABLE tablename
Example: DROP TABLE Sailors
TRUNCATE command
▪TRUNCATE Removes all rows from a table without
backup
Syntax: TRUNCATE table tablename
Example: TRUNCATE table Sailors 10
DML Commands
-INSERT
-DELETE
-UPDATE
-SELECT
INSERT command
Inserting record into a table
Syntax: INSERT INTO table-name VALUES
(field1,field2,…)
Example:
INSERT INTO Sailors values (22,'Dustin',7,45.0)
Inserting a record that has some null attributes
▪Requires identifying the fields that actually get data
Syntax: INSERT INTO table-name (field1,field4) VALUES
(value1,value2)
11
Inserting records from another table
Syntax: INSERT INTO table_name1 SELECT * FROM
table_name2
UPDATE command
For modifying attribute values of (some) tuples in a table
Syntax: UPDATE tablename SET column1=value1,…,
columnn=valuen WHERE condition
Example: UPDATE Sailors SET age=34.5 WHERE sid=22
DELETE command
Removing specified rows from a table
Syntax: DELETE FROM tablename WHERE condition
Example: DELETE FROM Sailors WHERE sid=22
12
Removing all rows from a table
Syntax: DELETE FROM tablename
Example: DELETE FROM Sailors
Fig 4.2 Boats table
13
Fig 4.3 Reserves table
14