Académique Documents
Professionnel Documents
Culture Documents
CREATE TABLE
Staff ( StaffNo, Salary, Lname)
CREATE TABLE Staff (
StaffNo INTEGER,
Salary FLOAT,
Lname VARCHAR(20)
);
Data Types
Integer (INT, INTEGER & SMALLINT)
Real numbers (FLOAT, REAL, DOUBLE)
Formatted numbers DECIMAL(i,j) or DEC(i,j) or NUMERIC(i,j)
Character strings
Fixed length: CHAR(n) or CHARACTER(n)
Variable length: VARCHAR(n)
Date and Time: DATE, TIME, TIME(i) and DATETIME data types
Example :
Professor(P_SSN, Pname, PAge, NIC, Rank)
Candidate Keys
UNIQUE
CREATE TABLE Professor(
P_SSN CHAR(5),
PName VARCHAR(10),
PAge INTEGER,
NIC CHAR(10),
Rank INTEGER,
PRIMARY KEY (P_SSN),
UNIQUE (NIC)
);
ALTER TABLE
ALTER TABLE can be used to add or drop a column, change a
column definition.
DROP TABLE
Used to remove a relation and its definition
The relation can no longer be used in queries, updates, or any
other commands since its description no longer exists
DROP TABLE Staff;
or
DROP TABLE Staff RESTRICT/CASCADE;
RESTRICT drops if no other constraints (such as foreign keys
or views exist)
CASCADE drops all referential constraints
Actions
SET NULL
CASCADE
SET DEFAULT
Example:
Employee(eid, ename, salary, deptno)
INSERT Statement
Inserting a single row
INSERT INTO Department VALUES ("A0001", "IT", "Colombo");
DELETE Statement
Deleting tuples from table
Deleting all records
UPDATE Statement
Updating tuples in a table
UPDATE <table>
SET <column> = <expression>
WHERE <selection condition>
SELECT Statement
Basic form of the SQL SELECT statement
SELECT <attribute-list>
FROM <table-list>
WHERE <condition>
<attribute-list> is a list of column names
<table-list> is a list of tables which the query accesses
<condition> is the condition that the output rows must satisfy
SELET ename
FROM Employee;
department A0001
SELECT ename
FROM Employee
WHERE deptNo = A0001;
Kandy
SELECT DISTINCT dname
FROM Department
WHERE location = Colombo or location = Kandy;
FROM Employee
WHERE deptno=A0001 AND salary>10000;
JOINS
List the employee information with their
department details who are working in
departments
SELECT *
FROM Employee, Department
WHERE deptNo = dno;
working departments
ALIASES
& RENAME
SELECT ename
FROMEmployee
WHERE ename LIKE Kama_;
Ordering of results
Using ORDER BY clause &
ASC (default) and DESC clauses
SELECT
FROM
WHERE
ORDER BY
IS NULL
In SQL, NULLs are considered to be distinct from every other
null.
List the employees who does not have salary
SELECT
FROM
WHERE
ename
Employee
salary IS NULL
IS NOT NULL
ename
Employee
salary IS NOT NULL;
SELECT
FROM
MIN(salary), MAX(salary),AVG(salary)
Employee;
Grouping:
Usually, we want aggregations on certain parts of the relation.
Purchase(pid, product, date, price, quantity)
HAVING Clause:
SELECT
FROM
WHERE
GROUP BY
HAVING
NESTING OF QUERIES
Print
Find nic and names of employees who work for both project 1
and project 3.
Emp( nic, name
Works_On( enic, pnum
SELECT
FROM
WHERE
e.nic, e.name
Emp e, Works_On w
e.nic = w.enic AND
w.pnum = 3 AND
e.nic IN (SELECT e1.nic
FROM Emp e1, Works_On w1
WHERE e1.nic = w1.enic AND
w1.pnum = 1);
e.nic, e.name
Emp e, Works_On w
e.nic = w.enic AND
e.nic NOT IN
(SELECT enic
FROM Works_On
WHERE pnum = 5);
SELECT
FROM
WHERE
E. NAME
Employee E
E.NIC IN (
SELECT ENIC
FROM Dependent
WHERE GENDER = E.GENDER);
EXISTS
Can be used to check the existence of set
Previous example
SELECT
FROM
WHERE
E.NAME
Employee E
EXISTS (
SELECT *
FROM Dependent
WHERE ENIC = E.NIC AND
GENDER = E.GENDER);
NOT EXISTS
List the names of employees who do not have any
dependents.
SELECT
FROM
WHERE
E.NAME
Employee AS E
NOT EXISTS (
SELECT *
FROM Dependent
WHERE ENIC = E.NIC);