Order Database
Order Database
Select Ename
From Employee
Where salary > (Select avg(salary) from employee );
1.Count the customers with grades above Bangalore’s
average.
select grade, count(*) as COUNT
from customer where grade >
(select avg(grade)
from customer
where city ='bangalore');
3. List all the salesman and indicate those who have and don’t have
customers in their cities (Use UNION operation.)
select sname, 'exists' as samecity
from salesman s
where city in
(select city
from customer c
where s.sid = c.sid)
union
select sname, 'not exists' as samecity
from salesman s where
city not in
(select city
from customer c
where s.sid = c.sid);
In SQL, View is called as Virtual table
View a qurey result for which we give a separate name
Student(name, usn, sem, Dob, Phone Address, Marks, feestatus)
Additional Query:
1. Select the name of the salesmans who gets highest commission.
2. Count the number of orders that each customers have done.
3. More than 3 orders.
CONSTRAINT
• MySQL CONSTRAINT is used to define rules to allow or restrict what
values can be stored in columns.
• The purpose of inducing constraints is to enforce the integrity of a
database.
• MySQL CONSTRAINTS are used to limit the type of data that can be
inserted into a table.
• MySQL CONSTRAINTS can be classified into two types - column level
and table level.
• The column level constraints can apply only to one column where as
table level constraints are applied to the entire table.
• MySQL CONSTRAINT is declared at the time of creating a table.
MySQL CONSTRAINTs are :
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
CONSTRAINT DESCRIPTION
NOT NULL In MySQL NOT NULL constraint allows to specify that a column can not contain any NULL
value. MySQL NOT NULL can be used to CREATE and ALTER a table.
The UNIQUE constraint in MySQL does not allow to insert a duplicate value in a column.
UNIQUE The UNIQUE constraint maintains the uniqueness of a column in a table. More than one
UNIQUE column can be used in a table.
PRIMARY KEY A PRIMARY KEY constraint for a table enforces the table to accept unique data for a specific
column and this constraint creates a unique index for accessing the table faster.
A FOREIGN KEY in MySQL creates a link between two tables by one specific column of both
FOREIGN KEY tables. The specified column in one table must be a PRIMARY KEY and referred by the
column of another table known as FOREIGN KEY.
A CHECK constraint controls the values in the associated column. The CHECK constraint
CHECK
determines whether the value is valid or not from a logical expression.
In a MySQL table, each column must contain a value ( including a NULL). While inserting
DEFAULT data into a table, if no value is supplied to a column, then the column gets the value set as
DEFAULT.
NOT NULL Constaint
CREATE TABLE Student(s_id int NOT NULL, Name varchar(60), Age int);
UNIQUE Constaint
CREATE TABLE Student(s_id int NOT NULL UNIQUE, Name
varchar(60), Age int);
PRIMARY KEY Constraint
CREATE table Student (s_id int PRIMARY KEY, Name
varchar(60) NOT NULL, Age int);
SET NULL
• The foreign key values in the referencing row are set to NULL when
the referenced row is updated or deleted.
• This is only possible if the respective columns in the referencing
table are nullable.
• Due to the semantics of NULL, a referencing row with NULLs in
the foreign key columns does not require a referenced row.
SET DEFAULT
• Similar to SET NULL, the foreign key values in the referencing row
are set to the column default value when the referenced row is
DEPARTMENT
Dno DeptName Location
10 Finance Charlotte
20 Infosys New York
30 Marketing Woodbridge
40 Accountant California
PROJECT
PID PName Dept_no
1 Product-X 10
2 Product-Y 10
3 Product-Z 20
4 Product-A 30
5 Product-B 40
SELECT * FROM user_constraints WHERE table_name = '<your table name>'
AND constraint_name = '<your constraint name>';
Data Retrieval:
SELECT [DISTINCT | ALL] attribute-list
FROM tablename-list – DISTINCT will eliminate
[WHERE condition ] duplication in the output
[GROUP BY expr [, expr]] while ALL will keep all
[HAVING condition] duplicated rows.
[ORDER BY {expr} [ASC | DESC]]
• select * from dept; • select max(salary)
from emp
• select deptname group by positionid;
from dept
where deptid='10'; • select deptname
from dept,emp
• select lname,fname
where dept.deptid=emp.deptid and
from emp
order by lname desc; emp.empid='111';
Figure: SQL statement processing
order
Customer
CustomerID CustomerName ContactName Address City PostalCode Country
4 Around the Horn Thomas 120 Hanover Sq. London WA1 1DP UK
Hardy
Employee
Database 1:
Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No,
Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)