Dbms Lab First Pages
Dbms Lab First Pages
SQL is the standard language for Relation Database System. All relational database
management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres
and SQL Server uses SQL as standard database language.
SQL PROCESS:
When you are executing an SQL command for any RDBMS, the system determines
the best way to carry out your request and SQL engine figures out how to interpret the
task.
There are various components included in the process. These components are:
o Query Dispatcher
o Optimization engines
o Classic Query Engine
o SQL query engine etc.
Classic query engine handles all non-SQL queries but SQL query engine won’t handle
logical files.
DBMS (CS) 1
SQL Query
Query Language
Processor Parser + Optimizer
Physical Database
DBMS (CS) 2
BASIC DATA TYPES:-
Data type Syntax Explanation(if applicable)
numeric numeric (p, s) Where p is a precision value; s is a scale value.
For example, numeric (6,2) is a number that has 4
digits before the decimal and 2 digits after the
decimal.
float float (p) Where p is a precision value.
character char (x) Where x is the number of characters to store. This
data type is space padded to fill the number of
characters specified.
character varying varchar2 (x) Where x is the number of characters to store. This
data type does NOT space pad.
date date Stores year, month, and day values.
Ex: 2013-05-03
time time Stores the hour, minute, and second values.
Ex: 15:51:36
timestamp timestamp Stores year, month, day, hour, minute, and second
values.
Ex:2013-05-03 15:51:36
Integer Integer
Number (width, decimal or point): It provides support to all types of numeric values.
Note:
Maximum 40 digits allow.
By default, if no width is specified, it is taken as eight (8).
By default, if no point is specified, zero (0) is taken into consideration.
DBMS (CS) 3
Ex:- a) Number (7,2)
Output: 9999.99
SQL: SQL is fourth generation non-procedural language. The SQL is further divided
into three sub-languages for various purposes:
1. DDL (Data Definition Language)
2. DML (Data Manipulation Language)
3. DCL (Data Control Language)
DBMS (CS) 4
NOTE: The difference between delete and truncate is delete will delete rows from the table
and if rollback applied, data can be given back whereas in truncate, we can not rollback. If
any row deleted, it is deleted. No triggers are fired.
EXAMPLE
We will present a number of sample queries using the following table definitions:
Sailors (sid: integer, sname: string, rating: number, age: real)
Boats (bid: integer, bname: string, colour: string)
Reserves (sid: integer, bid: integer, day: date)
SAILORS TABLE
sid sname rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
64 Horatio 7 35.0
71 Zorba 10 16.0
74 Horatio 9 35.0
85 Art 3 25.5
95 Bob 3 63.5
BOATS TABLE
bid bname colour
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
RESERVES TABLE
sid bid day
22 101 10/10/98
22 102 10/10/98
22 103 10/8/98
22 104 10/7/98
31 102 11/10/98
31 103 11/6/98
31 104 11/12/98
64 101 9/5/98
64 102 9/8/98
74 103 9/8/98
DBMS (CS) 5
DDL COMMANDS
SQL: CREATE TABLE
Syntax: CREATE TABLE table-name (Column_name1 datatype (size) constraint,
Column_name1 datatype (size) constraint, …. );
Example:
CREATE TABLE sailors
{
sid NUMBER
sname VARCHAR2(20)
rating NUMBER
age NUMBER (3,1)
};
DBMS (CS) 6
SQL: ALTER TABLE
Alter command is used to:
1. Add a new column.
2. Modify the existing column definition.
3. To include or drop integrity constraint.
DBMS (CS) 7
SQL> desc sailors;
Result:
If there is no further use of records stored in a table and the structure has to be
retained then the records alone can be deleted.
DBMS (CS) 8
DML COMMANDS
SQL: INSERT VALUES IN TABLE
This is used to add one or more rows to a table. The values are separated by commas
and the data types char and date are enclosed in apostrophes.
The values must be entered in the same order as they are defined.
Inserting more than one record using a single insert commands Syntax:
Example:
SQL> insert into sailors values (&sid, &sname, &rating, &age);
Enter value for sid: 22
Enter value for sname: ‘Dustin’
Enter value for rating: 7
Enter value for age: 45.0
Old 1: insert into sailors value (&sid, &sname, &rating, &age)
New 1: insert into sailors value (22, ‘Dustin’, 7, 45.0)
Result:
1 row created.
Insert into <table_name (column names to which data is to be inserted) > values (list
of values);
DBMS (CS) 9
SQL: SELECT VALUES FROM TABLE
Result:
10 rows selected.
DBMS (CS) 10
74 Horatio
85 Art
95 Bob
10 rows selected.
Result:
SNAME
---------------------------
Dustin
Brutus
Lubber
Andy
Rusty
Horatio
Zorba
Art
Bob
9 rows selected.
DBMS (CS) 11
Lubber 8
Andy 8
Rusty 10
Horatio 7
Zorba 10
Horatio 9
Art 3
Bob 3
Select command with order by clause:
Syntax: Select column_name1, ……, column_namen from table name where condition
orderby column_name;
Example: Select sid, sname from sailors order by rating;
Result:
SID SNAME
-------- ---------------------------------
29 Brutus
85 Art
95 Bob
22 Dustin
64 Horatio
31 Lubber
32 Andy
74 Horatio
58 Rusty
71 Zorba
10 rows selected.
DBMS (CS) 12
Select command to insert records:
Used to insert the same values as of the existing table
10 rows selected.
DBMS (CS) 13
SQL: Delete command
After inserting row in a table, we can also delete them if required. The delete
command consists of a clause from clause followed by an optional where clause.
DCL commands
SQL: commit command
This will commit (write to database) the transactions done by the DML.
After inserting, updating or deleting the transactions the user does not want to commit
the changes, then the user can rollback the transaction using the command:
DBMS (CS) 14
SQL: GRANT command
The grant statement provides various types of access to database objects such as
tables, views and sequences and so on.
It is used to create users and grant access to the database.
It requires database administrator (DBA) privilege, except that a user can change their
password.
A user can grant access to their database to their database objects to other users.
Syntax:
GRANT <object privilege> ON <object_name> TO <username> [WITH GRANT
OPTION];
DBMS (CS) 15