Sabyasachi Moitra
Sabyasachi Moitra
Sabyasachi Moitra
moitrasabyasachi@hotmail.com
What is SQL?
• Structured Query Language.
• It is a language to operate databases. In other words, SQL is a
computer language for storing, retrieving, and manipulating data
stored in a database.
• SQL is the standard language for Relational Database System.
• All the Relational Database Management Systems (RDMS) like MySQL,
MS Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL
as their standard database language.
• In the year 1974 SQL appeared.
2
SQL Commands
Category SQL Command Description
CREATE Creates a new table, a view of a
table, or other object in the
database.
Data Definition Language
ALTER Modifies an existing database
(defines the different structures in
object, such as a table.
a database)
DROP Deletes an entire table, a view of a
table or other objects in the
database.
SELECT Retrieves certain records from one
or more tables.
Data Manipulation Language INSERT Creates a record.
(manipulates data in a database)
UPDATE Modifies records.
DELETE Deletes records.
3
SQL Commands (2)
Category SQL Command Description
Data Control Language GRANT Gives a privilege to user.
(controls access to data stored in a REVOKE Takes back privileges granted
database) from user.
Transaction Control Language COMMIT Save the changes to the database.
(control access to data stored in a ROLLBACK Roll back the changes to the
database) previously saved point.
4
Database Objects
Object Description
Table • Basic unit of storage.
• Composed of rows and columns.
Views Logically represents subset of data from one or more
tables.
Sequence • Numeric value generator.
• Automatically generates unique numbers.
Index • Special lookup tables that the database search engine
can use to speed up data retrieval.
• A pointer to data in a table.
• Improves the performance of some queries.
• Indexes are used to retrieve data from the database
very fast.
• Similar to an index in the back of a book.
Synonyms • Gives alternative names to objects.
• Ease referring to a table owned by another user.
• Shorten lengthy object names.
5
CREATE TABLE Statement
Syntax Example
CREATE TABLE table_name CREATE TABLE course
( (
column1 datatype [constraint1], cid VARCHAR2(10) PRIMARY KEY,
column2 datatype [constraint2], cname VARCHAR2(20)
column3 datatype [constraint3], );
....
);
6
Data Types
Data Type Description
CHAR(n) Only character type data.
n maximum length
VARCHAR(n) Alphanumeric type data.
n maximum length
VARCHAR2(n) Same as VARCHAR, only differ in size.
n maximum length
NUMBER(n,s) Numeric type data.
By default accept 10 digits.
n total no. of digits
s no. of digits after the decimal point (default 0)
DATE Date & time.
…..
7
Constraints
Constraints are used to specify rules for data in a table.
Constraint Description
NOT NULL Ensures that a column cannot have a NULL value.
UNIQUE Ensures that all values in a column are different.
PRIMARY KEY A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table.
FOREIGN KEY Uniquely identifies a row/record in another table.
CHECK Ensures that all values in a column satisfies a
specific condition.
DEFAULT Sets a default value for a column when no value is
specified.
…..
8
INSERT INTO Statement
Syntax Example
INSERT INTO table_name INSERT INTO course
(column1,column2,column3,....) (cid,cname)
VALUES(value1,value2,value3,….); VALUES('C0001','CITA');
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Character Functions
31
Character Functions (2)
32
33
34
Number Functions
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Types of Joins
• Cross Join
- Cross product of two tables.
- Same as Cartesian Product.
• Equi Join/Inner Join
- Common attributes among multiple tables.
- Tables are joined on columns that have the same data type & width.
- Same as Natural Join.
• Non-Equi Join
- No common attributes among multiple tables.
• Outer Join
- Selects all rows from the table on the left(/right/both) regardless of whether the other
table has values in common & enters NULL where data is missing.
• Self Join
- Join two instances of same table.
61
CROSS JOIN Statement
SELECT name,city FROM person CROSS JOIN city;
name city
name age weight Harry San Jose
city Harry Austin
Harry 34 80
San Jose Sally San Jose
Sally 28 64
Austin Sally Austin
George 29 70 George San Jose
city
person George Austin
output
62
63
64
65
66
Creating JOINs with the USING
Clause
SELECT department_id, department_name, location_id, city
FROM departments JOIN locations
USING (location_id);
67
68
69
Left Outer Join
SELECT e.last_name,e.department_id,d.department_name
FROM emp e,dept d
WHERE e.department_id=d.department_id(+);
OR
SELECT e.last_name,e.department_id,d.department_name
FROM emp e LEFT OUTER JOIN dept d
ON(e.department_id=d.department_id);
70
Output
71
Right Outer Join
SELECT e.last_name,e.department_id,d.department_name
FROM emp e,dept d
WHERE e.department_id(+)=d.department_id;
OR
SELECT e.last_name,e.department_id,d.department_name
FROM emp e RIGHT OUTER JOIN dept d
ON(e.department_id=d.department_id);
72
Output
73
Full Outer Join
SELECT e.last_name,e.department_id,d.department_name
FROM emp e,dept d WHERE e.department_id=d.department_id(+)
UNION
SELECT e.last_name,e.department_id,d.department_name
FROM emp e,dept d
WHERE e.department_id(+)=d.department_id;
OR
SELECT e.last_name,e.department_id,d.department_name
FROM emp e FULL OUTER JOIN dept d
ON(e.department_id=d.department_id);
74
Output
75
76
77
UPDATE Statement
78
DELETE Statement
79
ALTER Statement
• Used to add, delete, or modify columns in an existing table.
Syntax
• To add a new column:
- ALTER TABLE table_name ADD column_name data type;
• To delete a column:
- ALTER TABLE table_name DROP COLUMN column_name;
• To change data type of a column:
- ALTER TABLE table_name MODIFY column_name data type;
• To add constraint on a column:
- ALTER TABLE table_name ADD constraint_type;
• To delete a constraint:
- ALTER TABLE table_name DROP constraint_type;
• To rename a column:
- ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
• To rename an existing table:
- ALTER TABLE table_name RENAME TO new_table_name;
80
DROP Statement
• Used to remove an object from the database.
• If you drop a table, all the rows in the table is deleted and the table
structure is removed from the database.
• Once a table is dropped we cannot get it back.
Syntax
DROP TABLE table_name;
81
TRUNCATE Statement
• Used to delete all the rows from the table and free the space.
• The schema will remain same.
Syntax
TRUNCATE TABLE table_name;
OR
DELETE FROM table_name WHERE '1’; (equivalent of TRUNCATE)
82
Other Statements
Syntax Example
CREATE VIEW view_name AS CREATE VIEW course_view AS
SELECT column1, column2, ... SELECT cid, cname
FROM table_name FROM course
View WHERE condition; WHERE cname LIKE 'a%';
84
GRANT & REVOKE Statement
Privilege Syntax Example
GRANT privilege [, privilege...] GRANT CREATE SESSION
System Privilege TO user [, user| role, PUBLIC...]; TO babu;
(Gaining access to the database) REVOKE privilege [, privilege...] REVOKE CREATE SESSION
FROM user [, user| role, PUBLIC...]; FROM babu;
GRANT object_priv [(columns)] GRANT
ON object SELECT,INSERT,UPDATE,DELE
TO {user|role|PUBLIC} TE ON course TO babu;
Object Privilege [WITH GRANT OPTION];
(Manipulating the content of the REVOKE {privilege [, REVOKE
database objects) privilege...]|ALL} SELECT,INSERT,UPDATE,
ON object DELETE ON course FROM
FROM {user[, user...]|role|PUBLIC} babu;
[CASCADE CONSTRAINTS];
85
System Privileges
• CREATE SESSION
• CREATE TABLE
• CREATE SEQUENCE
• CREATE VIEW
• CREATE PROCEDURE
86
Object Privileges
87
Role
Example
Create a role CREATE ROLE role1;
Grant privileges to a role GRANT CREATE TABLE, CREATE VIEW
TO role1;
Grant a role to users GRANT role1 TO user1, user2;
88
References
• Courtesy of W3Schools –SQL Tutorial. URL:
http://www.w3schools.com/SQL/
• Courtesy of TutorialsPoint –SQL Tutorial. URL:
http://www.tutorialspoint.com/sql/
• Courtesy of JavaPoint –SQL Tutorial. URL:
http://www.javapoint.com/sql-tutorial
• Ivan Bayross, SQL, PL/SQL The Programming Language of Oracle, 4th
Revised Edition, BPB Publications, 2009
89