DBE Unit 2
DBE Unit 2
Unit 2
• Start-oracle11g-
• Get Started-
• Application Express-
• Create Database user and Workspace
• Login
• SQL Workshop
SQL Command Categories
• SQL language is divided into the following four
command categories:
• Data definition (Data Definition Language, or DDL)
• Data manipulation (Data Manipulation Language, or
DML)
• Retrieval
• Security and authorization
Data Definition Language (DDL)
• CREATE - to create a new database object
contact_name varchar2(50),
contact_name varchar2(50),
CONSTRAINT fk_p_ref2
FOREIGN KEY (p) REFERENCES ref2(id) );
INSERT
• Insert into table_name values( , , )
• Insert values for All Attributes
• INSERT INTO departments VALUES (280,
'Recreation', 121, 1700);
Example:
DROP TABLE Employee;
TRUNCATE
Syntax:
TRUNCATE TABLE <table name>;
Example:
TRUNCATE TABLE Employee;
RENAME
Syntax:
RENAME <old object name> TO <new object name>
Example:
RENAME temporary to new
Constraints
Not Null Constraint
• If a column in a table is specified as Not Null,
• then it’s not possible to insert a null in such
column.
• It can be implemented with create and alter
commands.
• When we implement the Not Null constraint with
alter command there should not be any null
values in the existing table.
Not Null
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
Unique Constraint
The unique constraint doesn’t allow duplicate values in a
column.
If unique constraint encompasses two or more columns,
no two equal combinations are allowed
CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
DEFAULT Constraint
The DEFAULT constraint is used to insert a default
value into a column.
The default value will be added to all new records, if no
other value is specified.
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT ‘Ichalkaranji'
)
CHECK Constraint
CHECK constraint is used to limit the value range that
can be placed in a column.
• Inner Join
• Left outer join
• Right outer join
• Full outer join
• Natural join
• Cartesian product
SQL Inner Join – Simple Join
• use a comparison operator to match rows from two
tables
• based on the values in common columns from each
table.
• For example, retrieving all rows where the student
identification number is the same in both the students
and courses tables.
• If we want to list all the Consumers with any orders.
• We use the following SELECT statement:
The "Consumers" table:
P_Id LastName FirstName Address City
1 Kumar Ram Delhi AAA
2 Singh Laxman Chandigarh AAA
3 Sharma Sameer Ambala BBB
• SELECT Consumers.LastName,
Consumers.FirstName, Orders.OrderNo
FROM Consumers, orders
WHERE Consumers.P_Id=Orders.P_Id
ORDER BY Consumers.LastName
• The result-set will look like this
LastName FirstName OrderNo
Kumar Ram 12357
Kumar Ram 24562
Sharma Sameer 12355
Sharma Sameer 12356
When to use inner join
Use an inner join when you want to match values from both tables.
SELECT Consumers.LastName,
Consumers.FirstName, Orders.OrderNo
FROM Consumers
LEFT JOIN Orders
ON Consumers.P_Id=Orders.P_Id
ORDER BY Consumers.LastName
• The result-set will look like this:
LastName FirstName OrderNo
Kumar Ram 22456
Kumar Ram 24562
Sharma Sameer 77895
Sharma Sameer 44678
Singh Laxman
Table O
SELECT *
FROM EMP
WHERE EMP_ID in (10000, 10001, 10003, 10005);
SELECT *
FROM suppliers
WHERE supplier_name in ('Apple', 'HP', 'Microsoft');
store_name SUM(Sales)
London $1800
San Diego $250
Boston $700
• Example 2 of SQL Group BY
SELECT COUNT (*) FROM t_state
• The above statement returns the total number of
rows in the table.
• We can use GROUP BY to count the number of
offices in each state.
• With GROUP BY, the table is split into groups by
state, and COUNT (*) is applied to each group in
turn.
• SELECT state, COUNT (*)
FROM t_state
GROUP BY state;
• we have a table name Orders.
Orders (O_Id, OrderDate, OrderPrice, Customer)
• we want to find the total sum (total order) of each
customer.
SELECT Customer,SUM(OrderPrice)
FROM Orders
GROUP BY Customer
• Returns a list of Department IDs along with the sum
of their sales for the date of January 1, 2000.
• UNION ALL
• Combines the results of two SELECT statements into
one result set.
• UNION
• Combines the results of two SELECT statements into
one result set, and then eliminates any duplicate
rows from that result set.
• MINUS
• Takes the result set of one SELECT statement, and
removes those rows that are also returned by a
second SELECT statement.
• INTERSECT
• Returns only those rows that are returned by each of
two SELECT statements.
• SELECT CUST_NBR, NAME FROM CUSTOMER
• WHERE REGION_ID = 5
• UNION
• SELECT C.CUST_NBR, C.NAME FROM
CUSTOMER C
• WHERE C.CUST_NBR IN
• (SELECT O.CUST_NBR FROM CUST_ORDER O,
EMPLOYEE E WHERE O.SALES_EMP_ID =
E.EMP_ID AND E.LNAME = 'MARTIN');
• The following query uses both MINUS and UNION
ALL to compare two tables for equality.
• (SELECT * FROM CUSTOMER_KNOWN_GOOD
MINUS SELECT * FROM CUSTOMER_TEST)
UNION ALL (SELECT * FROM CUSTOMER_TEST
MINUS SELECT * FROM
CUSTOMER_KNOWN_GOOD);
• CREATE TABLE table1 (
• id INTEGER NOT NULL PRIMARY KEY,
• city VARCHAR(10) NOT NULL);
select *
from employee
where employee-name in
(select employee-name
from works
where company-name = ’First Bank Corporation’ and
salary >= 10000)
• Modify the database so that Jones who was working
for City Bank now works for First Bank Corporation.
• Modify the database so that Jones who was working
for City Bank now works for First Bank Corporation.
update works
set company-name = ’ First Bank Corporation’
where person-name = ’Jones’
• SQL Examples
• Given the Academic Institute database composed of
following tables.
person (pid, pname, paddress, uid)
UIDData(uid, age)
student (rollno, pid, class, dept)
faculty (fid, pid, dept)
Studentattendance(rollno, perattendance);
Studentmarks(rollno, subject, marks)
Studentfee(rollno, feepaid, feedues)
Lectures(fid, class, subject)