Professional Documents
Culture Documents
Chapter 3 Database
Chapter 3 Database
CHAPTER – III
Characteristics of SQL
i) It is extremely flexible
(vii) INTEGER
(viii) SMALLINT
DDL Commands
CREATE TABLE:
Syntax:
SQL> Create table tablename (Column1 data type, Column2 data type, Column3 data
type, …………... Column n data type m),
Example:
SQL> Create table employee (Name varchar2 (15), Eid number (6),
ALTER TABLE:
This command is used to add or drop or modify the attributes from the existing table.
SYNTAX:
Adding an attribute:
ALTER TABLE <table_name> ADD column_name datatype;
Dropping an attribute:
ALTER TABLE <table_name> DROP COLUMN column_name;
Modifying an attribute:
ALTER TABLE <table_name> MODIFY column_name newdatatype;
Renaming an attribute:
ALTER TABLE <table_name> RENAME COLUMN <old-column_name>
<new-column_name>;
DROP TABLE:
This command is used to remove a relation from an SQL database. This command deletes not
only the records of the table but also the entire schema of the database.
SYNTAX:
TRUNCATE TABLE
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.
SYNTAX:
RENAME TABLE
SYNTAX:
DML commands
DML commands let the user insert, modify and delete the data in the database. SQL
INSERT:
Insert command is used to insert the values into the table. There are three ways to insert a
record into the database.
Syntax:
INSERT INTO table_name(col1,col2,…….,coln)
VALUES(val1,val2,……,valn);
2. Inserting Values:
Syntax:
INSERT INTO table_name VALUES(val1,val2,……...,valn);
Syntax:
SELECT:
The select statement is used to query a database. This statement is used to retrieve the
information from the database. The SELECT statement can be used in many ways. They
are…
To select specified number of columns from the table the following command is used.
Syntax:
To select all columns from the table * is used instead of column names.
Syntax:
UPDATE:
Syntax:
UPDATE table_name SET column name =new value WHERE column name=some value;
DELETE:
Syntax:
TCL commands
TCS manages all changes made by the DML commands. Some of the transaction,
COMMIT
Use the COMMIT statement to end your current transaction and make permanent all
ROLLBACK
Use the ROLLBACK statement to undo work done in the current transaction or to
SAVEPOINT
SAVEPOINT savepoint-name;
DCL commands
DCL commands manages permanent change and restores the changes with the
GRANT and
REVOKE
The GRANT command is used to give various privileges to tables for users. These
Syntax
Privilege Description
Object
The name of the database object that you are granting privileges for. In the case of
User
Example
For example, if you wanted to grant SELECT, INSERT, UPDATE, and DELETE
privileges on a table called SUPPLIERS to a user name smith, you would run the
You can also use the ALL keyword to indicate that you wish ALL permissions to be
For example:
Once you have granted privileges, you may need to revoke some or all of these
privileges. To do this, you can run a revoke command. You can revoke any
Syntax
Privilege Description
object
The name of the database object that you are revoking privileges for. In the case of
user
The name of the user that will have these privileges revoked.
Example
suppliers from a user named anderson, you would run the following REVOKE
statement:
If you wanted to revoke ALL privileges on a table for a user named anderson, you
NOTE:
Example:
Show user;
9 Prepared by Dr.M.S.Saravanan, Professor, Dept. of Information Science, CCI,
Haramaya University.
HARAMAYA UNIVERSITY
COLLEGE OF COMPUTING AND INFORMATICS
DEPARTMENT OF INFORMATION SCIENCE
INSC 1064 - ADVANCED DATABASE
maintain accuracy and integrity of the data in the database. The major types of
i) Domain constraints
Date Function
They operate on date values and produce outputs, which also belong to date data type
except for months, between, date function returns a number.
1. Add_month
This function returns a date after adding a specified date with specified number of months.
Syntax: Add_months(d,n); where d-date n-number of months
Example: Select add_months(sysdate,2) from dual;
2. last_day
It displays the last date of that month.
Syntax: last_day (d); where d-date
Example: Select last_day (‘1-jun-2009’) from dual;
3. Months_between
It gives the difference in number of months between d1 & d2.
Syntax: month_between (d1,d2); where d1 & d2 -dates
4. next_day
It returns a day followed the specified date.
Syntax: next_day (d,day);
Example: Select next_day (sysdate,’wednesday’) from dual
Numerical Functions
Character Functions
Group Functions
A group function returns a result based on group of rows.
1. avg
Example: select avg (total) from student;
2.max
Example: select max (percentagel) from student;
3.min
Example: select min (marksl) from student;
4. sum
Example: select sum(price) from product;
The query within another is known as a sub query. A statement containing sub query is called
parent statement. The rows returned by sub query are used by the parent statement.
The following operators can be used for Sub Queries
in
not in
all
SQL> Select * from emp1 where eid in(select eid from emp2);
SQL> Select * from emp1 where eid not in(select eid from emp2 where aid>60);
JOINS
Joins Types
INNER JOIN
OUTER JOIN
INNER JOIN
The Inner Join is a join operation that joins two tables by their common column. This
operation is similar to the setting relation of two tables
SELECT a.comcol, a.col1, b.col2, expr1, expr2 FROM table1 a, table2 b WHERE a.comcol
= b.comcol
SELECT s.class, s.name, s.id, m.type FROM student s, music m WHERE s.id=m.id ORDER
BY class, name
Example2:
SQL> Select a.eid,a.ename,a.esalary, b.eadd from emp1 a, emp2 b where a.eid =b.eid;
SELECT ename, job, dname FROM emp, dept where emp.deptno (+) != dept.deptno;
Example2:
Oracle Objects
VIEWS
Views are used to view the selected columns as per the user need.
Views are four types, they are…
CREATE
UPDATE
ALTER
SELECT
NOTE:
To ACCESS a VIEW ----need to give permission in user login……
Example:
CREATE VIEW:
SYNTAX:
CREATE VIEW <view_name> AS (SELECT <col1, col2, col2… coln> FROM <table-name>);
UPDATE:
SYNTAX:
UPDATE view_name SET column name =new value WHERE column name=some value;
ALTER:
SELECT:
SYNTAX:
CREATING A VIEW
View created.
EMPNAME EMPNO
--------------- ---------
Tamil 1
Selvi 2
Siva 3
Raja 4
Josh 5
ALTERING A VIEW
UPDATING A VIEW
1 row updated.
DISPLAYING A VIEW
EMPNAME EMPNO
--------------- ---------
Tamil 1
Kamala 2
Siva 3
Raja 4
Josh 5
SEQUENCES
Applications most often use these numbers when they require a unique value in a
Example:
Sequence Created.
On execution, Oracle will create a sequence 'ANU'. Its start with value is 1, incrementing the
sequence number by 1. Maximum value that it can generate is 100.
Example:
1 row created.
INDEXES
An index is a performance-tuning method of allowing faster retrieval of records. An index
creates an entry for each value that appears in the indexed columns.
Create an Index
Syntax
The syntax for creating an index in Oracle/PLSQL is:
UNIQUE
It indicates that the combination of values in the indexed columns must be unique.
index_name
The name to assign to the index.
table_name
The name of the table in which to create the index.
column1, column2, ... column_n
The columns to use in the index.
COMPUTE STATISTICS
It tells Oracle to collect statistics during the creation of the index. The statistics are
then used by the optimizer to choose a "plan of execution" when SQL statements are
executed.
Example
Let's look at an example of how to create an index in Oracle/PLSQL.
Table created.
COLUMN_A COLUMN_B
------------------- ----------------
1 2
2 4
3 6
4 8
5 10
5 rows selected.
Index created.
COLUMN_A COLUMN_B
------------------- ----------------
1 2
2 4
3 6
3 rows selected.