3.1_SQL (1)
3.1_SQL (1)
Assistant Professor
Dept. of CSE- AIML
ATMECE, Mysuru
Database Management
Systems
Module – 3
SQL
Introduction
The main SQL command for data definition is the CREATE statement, which can be
used to create schemas, tables (relations) as well as other constructs such as views,
assertions, and triggers.
Base tables (or base relations): The relations declared through CREATE TABLE
statements.
This means that the table and its rows are actually created and stored as a file by the
DBMS.
Virtual tables : does not have their data stored as a file.
The DATE data type has ten positions, and its components are YEAR, MONTH, and
DAY in the form YYYY-MM-DD.
The TIME data type has at least eight positions, with the components HOUR,
MINUTE, and SECOND in the form HH:MM:SS.
SQL Data Definition and Data Types
• A timestamp data type (TIMESTAMP) includes the DATE and TIME fields .
• PRIMARY KEY
• NOT NULL
• CHECK
• DEFAULT
• UNIQUE
• FOREIGN KEY
Student (sid,Sname,cid)
PK FK
Course(cid,cname)
PK
The CHECK clause can also be used in conjunction with the CREATE
DOMAIN statement.
PRIMARY KEY clause does not allow duplicate values and null values.
There can be only one primary key in a table.
Create table dept (Dnumber INT PRIMARY KEY);
Here in the table dept, Dnumber column is the primary key and in this
column there should be no duplicate and also null values.
Referential Integrity – This means the values of the primary key column of
a table should match with the foreign key column of same table or another
table.
The default action that SQL takes for an integrity violation is to reject the
update operation that will cause a violation, which is known as the
RESTRICT option.
• parent row refers to the row of the table which is containing primary key
• child row refers to the row of the table which is containing foreign key.
• Set NULL : Sets the column value to NULL when you delete or update the
parent table’s row.
• CASCADE : CASCADE will propagate the change when the parent changes. If
you delete or update a row, rows in constrained tables that reference that
row will also be deleted or updated.
• SET DEFAULT : SET DEFAULT option causes you to set the default value in the
child table column when the parent table row is deleted/updated.
i) Create a table with primary key by giving a name for primary key:
ii) Create a table with primary key and foreign key by specifying names
for both foreign keys with on delete set null and on update cascade
• CREATE TABLE EMPLOYEE (
• Emp_no int,
• Dno INT DEFAULT 1,
• SSN varchar(9) primary key,
• Super_ssn varchar(9),
• CONSTRAINT EMPSUPERFK FOREIGN KEY (Super_ssn) REFERENCES
EMPLOYEE(Ssn) ON DELETE SET NULL,
• CONSTRAINT EMPDEPTFK FOREIGN KEY(Dno) REFERENCES
DEPARTMENT(Dnumber)ON DELETE SET NULL ON UPDATE CASCADE);
Specifying Constraints in SQL
SQL has one basic statement for retrieving information from a database:
The SELECT statement
The SELECT-FROM-WHERE Structure of Basic SQL Queries
In SQL, the basic logical comparison operators for comparing attribute values
with one another and with literal constants are =, <=, >, >=, and <>.
Basic Retrieval Queries in SQL
• Select can be used to join two tables using a common column between
the two tables:
SELECT * FROM
employee2, department2
WHERE Dnumber = Dno and Dname = 'Research’;
The condition Dnumber = Dno is called a join condition, because it combines two
tuples(two records or rows): one from DEPARTMENT and one from EMPLOYEE
Example 1 : Retrieve all the information of all employees who work for the
‘Research’ department.
Example 2: Retrieve the name and address of all employees who work for the
‘Research’ department
• Example 2:
• select employee2.fname,employee2.lname,project.project_no
• from employee2,department2,project
• WHERE DEPARTMENT2.Dnumber = EMPLOYEE2.Dno and
• project.Dnumber = department2.Dnumber and Dname = 'Research’;
• Here also the ambiguity is prevented by specifying the table names with the
columns.
Basic Retrieval Queries in SQL
We use the keyword DISTINCT in the SELECT clause, meaning that only
distinct tuples should remain in the result.
Example 1: select distinct Dno from employee;
Example 2: SELECT DISTINCT Dno FROM DEPARTMENT2, EMPLOYEE2;
Example 3: ( SELECT DISTINCT Dno
FROM DEPARTMENT2, EMPLOYEE2
WHERE employee2.Dno = department2.Dnumber AND employee2.lname =
'Guptha’ )
UNION
(SELECT DISTINCT Dnumber
FROM employee2, project
WHERE employee2.Dno = project.Dnumber AND employee2.lname =
'Guptha' );
Basic Retrieval Queries in SQL
• Ascending Order:
• SELECT *FROM employee2, department2
• where employee2.Dno = department2.Dnumber
• order by employee2.fname ASC;
• Descending Order:
• SELECT *FROM employee2, department2
• where employee2.Dno = department2.Dnumber
• order by employee2.fname DESC;
Basic Retrieval Queries in SQL
The DELETE Command: The DELETE command removes tuples(rows) from a relation(table). It
includes a WHERE clause, similar to that used in an SQL query, to select the tuples to be
deleted.
• vi) Query to get the average salary and count of employees from each
department
• SELECT Dno, COUNT(*) as Emp_in_each_dept, AVG (Salary) as
avg_salary
• FROM EMPLOYEE
• GROUP BY Dno;
• vii) Query to get the Nth highest salary from the table (This query can
be used to find 1st highest, 2nd highest , 3rd highest and so on…)
• select * from
• (select * , dense_rank() over (order by salary desc) as
DenseRank from Employee)
• employee
• where DenseRank = 4;
Subqueries:
• Subqueries: A subquery is a SQL query nested inside a larger
query. It can be used in a SELECT, INSERT, UPDATE, or DELETE
statement
• Subqueries used with the select:
• SELECT emp_name
• FROM EMPLOYEE
WHERE (SELECT COUNT(*) FROM DEPENDENT
WHERE Ssn = Essn ) >= 2;
Here Essn is called the employee Ssn which is stored in the dependents table
and Employee table has the SSN Value.
Subqueries used with the insert:
insert into customers1(Customername, city, Country)
(select Suppliername, city, country from suppliers where country =
'germany');
Subqueries: