Structured Query
Language (SQL)
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 1
Basics of Table
Table is a bucket where you pour data.
Data in a specific table is associated
with all other items in that table.
In a table, there are basically 3 things-
1. Rows / Records / Tuples
2. Columns / Attributes / Fields
3. Data
Rushdi
Shams,
Dept of
2 CSE, KUET
Basics of Table
In the table, the column goes on in a
horizontal fashion.
ISBN, AUTHOR, PUBLISHER, TITLE, GENRE,
PRINTED are the column names for this table
Rushdi Shams, Dept of CSE, KUET 3
Basics of Table
In the table, the row goes on in a vertical
fashion.
Every row in this table has data for ISBN,
AUTHOR, PUBLISHER, TITLE, GENRE,
PRINTED
Rushdi Shams, Dept of CSE, KUET 4
Basic of Table
Books contain relatively disorganized data
Organize information using model
Resulting in a neatly structured set of
rows and columns
Rushdi Shams, Dept of CSE, KUET 5
Relation / Table
In relational data model, the table is also called
relation. There are set of rules that are applied on the
relations. You must have to know them.
1. A database contains many relations. Every relations in
a database must have distinct names
2. Every column in a relation must have distinct names
3. Every entries in a column must be in the same domain
4. The ordering of columns in a relation is insignificant
Rushdi
Shams,
Dept of
6 CSE, KUET
Relation / Table
(continued)
5. Duplicate rows are not allowed in
a relation
6. The ordering of rows is
insignificant
7. Multiple values are not allowed in
the cells of a relation
8. 2 rows in a relation may contain
the same value for 1 columns but
not in all (deviation of 5)
Rushdi
Shams,
Dept of
7 CSE, KUET
Terminology
The number of rows in a table is
called Cardinality
The number of columns in a table is
called Degree
Rushdi
Shams,
Dept of
8 CSE, KUET
Languages in Database
Systems
Data Definition Language (DDL)
Used to create and modify the
structure of database objects
CREATE
ALTER
DROP
DDL commands execute as soon as
they are issued, and do not need to be
explicitly saved
Rushdi
Shams,
Dept of
9 CSE, KUET
Languages in Database
Systems
Data Modification Language (DML)
Used to insert, view, and modify
database data
INSERT
UPDATE
DELETE
SELECT
DML commands need to be explicitly
saved or rolled back
COMMIT
ROLLBACK
Rushdi
Shams,
Dept of
10 CSE, KUET
Languages in Database
Systems
Data Control Language (DCL)
Privileges, Access Control,
Administrative Rights
Rushdi
Shams,
Dept of
11 CSE, KUET
Data Definition Language (DDL)
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 12
Creating a table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 13
Creating a table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 14
Dropping a table
DROP TABLE table_name;
DROP TABLE cars;
DROP TABLE specs;
DROP TABLE stock;
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 15
Adding a column to a
table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 16
Adding columns to a
table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 17
Modifying a single
column on a table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 18
Modifying columns on a
table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 19
Dropping columns on a
table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 20
Renaming columns on a
table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 21
Data Modification Language (DML)
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 22
Inserting data into table
REMEMBER- when you are inserting values that are
varchar, char or other string types (if any) then you
will have to put ‘’ around that value (e.g.
‘value_for_column_X’).
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 23
Inserting data into table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 24
Showing data in a table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 25
Updating data in a table
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 26
Deleting row from a
table
If you do not put WHERE clause, while using it with DELETE statement
then all the data in that table will be deleted. SO, BE CAREFUL!
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 27
Data Control Language (DCL)
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 28
Types of Database
Privileges
System Privileges
Control the operations that the user can perform
within the database
○ Connecting to the database, creating new tables,
shutting down the database, etc.
Object Privileges
Granted on individual database objects
Controls operations that a user can perform on a
specific object (insert data, delete data, etc.)
When you create an object in your user schema,
you can then grant object privileges on that
object to other database users
Rushdi
Shams,
Dept of
29 CSE, KUET
Database Objects
An Oracle database consists of
multiple user accounts
Each user account owns database
objects
Tables
Views
Stored programs
Rushdi
Shams,
Dept of
30 CSE, KUET
Oracle Naming Standard
Oracle
database objects must
adhere to the Oracle Naming
Standard
1 to 30 characters long
Must begin with a character
Can contain characters, numbers, and
the symbols $, _ , and #
Rushdi
Shams,
Dept of
31 CSE, KUET
Creating New User
Accounts
Done by DBA
Syntax:
CREATE USER username IDENTIFIED BY
password;
Rushdi Shams, Dept of CSE, KUET 32
Trying to access the
database
As we have created a new user and
assigned his password, should we be able
to log into the database with the
username and password?
Rushdi Shams, Dept of CSE, KUET 33
The Answer
Rushdi
Shams,
Dept of
34 CSE, KUET
Granting System Privileges
Done by DBA
Syntax:
GRANT privilege1, privilege2, … TO username;
Rushdi Shams, Dept of CSE, KUET 35
So,now can you log in?
Yes, you can!
Rushdi
Shams,
Dept of
36 CSE, KUET
Example Oracle System
Privileges
Privilege Level Purpose
CREATE SESSION User Connecting to database
CREATE TABLE User Creating tables in current user
schema
UNLIMITED TABLESPACE User Allows user to create schema
objects using as much space as
needed
CREATE USER DBA Creating new users
GRANT ANY PRIVILEGE DBA Granting system privileges to
users
CREATE ANY TABLE DBA Creating tables in any user
schema
DROP ANY TABLE DBA Dropping tables in any user
Rushdi Shams, Dept of CSE, KUET 37
Granting another
privilege
Rushdi
Shams,
Dept of
38 CSE, KUET
Can you create table
yet?
Rushdi
Shams,
Dept of
39 CSE, KUET
The Reason
You have granted your user to create
session (log in) and to create table. But, it
is to create on your default SYSTEM
database. Therefore, you will have to grant
another privilege to your user.
Rushdi Shams, Dept of CSE, KUET 40
… & The Solution
Rushdi
Shams,
Dept of
41 CSE, KUET
Database Roles
Role is a database object that can
be assigned system privileges
Role is then assigned to a user, and
the user inherits the role’s
privileges
Used to easily assign groups of
related privileges to users
Rushdi
Shams,
Dept of
42 CSE, KUET
Creating a Role
Syntax:
CREATE ROLE role_name;
Rushdi Shams, Dept of CSE, KUET 43
Granting Privilege to Role
GRANT privilege1, privilege2, …
TO role_name;
Rushdi Shams, Dept of CSE, KUET 44
Assigning Role to the User
Syntax:
GRANT role_name TO user_name;
Rushdi Shams, Dept of CSE, KUET 45
Revoking System Privileges
Syntax:
REVOKE privilege1, privilege2, …
FROM username;
Rushdi Shams, Dept of CSE, KUET 46
What Happens Next?
Rushdi
Shams,
Dept of
47 CSE, KUET
Oracle Data Types
Data type: specifies type of data
stored in a field
Date, character, number, etc.
Rushdi
Shams,
Dept of
48 CSE, KUET
Character Type Data
Type
VARCHAR2
Variable-length character strings
Maximum of 4,000 characters
Must specify maximum width allowed
Example declaration:
student_name VARCHAR2(30)
Rushdi
Shams,
Dept of
49 CSE, KUET
Character Type Data
Type
CHAR
Fixed-length character data
Maximum size 2000 characters
Must specify maximum width allowed
Example declaration:
student_gender CHAR(1)
Rushdi
Shams,
Dept of
50 CSE, KUET
Character Type Data
Type
NCHAR
Used for alternate alphabets
Rushdi
Shams,
Dept of
51 CSE, KUET
Number Data Type
NUMBER
stores values between 10-130 and 10126
General declaration format:
variable_name NUMBER(precision,
scale)
Rushdi
Shams,
Dept of
52 CSE, KUET
Number Data Type
Number type (integer, fixed point,
floating point) specified by
precision and scale
Precision: total number of digits on
either side of the decimal point
Scale: number of digits to right of
decimal point
Rushdi
Shams,
Dept of
53 CSE, KUET
Number Data Type
Whole number with no digits to
right of decimal point
Precision is maximum width
Scale is omitted
Sample declaration:
s_age NUMBER (2)
Rushdi
Shams,
Dept of
54 CSE, KUET
Fixed Point Numbers
Contain a specific number of
decimal places
Precision is maximum width
Scale is number of decimal places
Sample declaration:
item_price NUMBER(5, 2)
Rushdi
Shams,
Dept of
55 CSE, KUET
Floating Point Numbers
Contain a variable number of
decimal places
Precision and scale are omitted
Sample declaration:
s_GPA NUMBER
Rushdi
Shams,
Dept of
56 CSE, KUET
Date Data Type
DATE
Stores dates from 1/1/4712 BC to 12/31/4712
AD
Stores both a date and time component
Default date format:
DD-MON-YY
example: 05-JUN-03
Sample declaration:
s_dob DATE
Rushdi
Shams,
Dept of
57 CSE, KUET
SELECT command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 58
SELECT command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 59
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 60
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 61
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 62
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 63
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 64
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET,
Bangladesh 65