SQL (STRUCTRED QUERY
LANGUAGE)
Relational Model
Uses a collection of tables to represent data and relationship among these data.
RDBMS stands for Relational Database Management System.
It’s a program that lets you to create ,update and administer a relational database.
Most of the RDBMS use SQL to access the database.
The data in RDBMS is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
A relation is a two-dimensional table made up of rows and columns. Each relation
also called a table,stores data about entities
Tuples -The rows in a relation are called tuples. They represent specific occurrences
(or records)of an entity.
Each row consists of a sequence of values, one for each column in the table.
Attributes -- The column in a relation is called attribute. The attributes represent
characteristics of an entity.
Domain For each attribute there is a set of permitted values called domain of that
attribute
SQL is a comprehensive database language:
It has statements for data definition, query, and update.
It is DDL ,DML and DCL.
It has facilities for defining views on the database, for specifying security and authorization,
for defining integrity constraints, and for specifying transaction controls.
It also has rules for embedding SQL statements into a general-purpose programming language
such as Java or COBOL or C/C+ +.
SQL uses the terms table, row, and column for the formal relational model terms
relation, tuple, and attribute, respectively.
MySQL -it’s a relational database management system (RDBMS) that uses structured
query language(SQL).
DDL
The main SQL command for data definition is the CREATE statement, which can be
used to create schemas, tables (relations), and domains (as well as other constructs
such as views, assertions, and triggers).
Create Command
Create schema
create schema company authorization John;
A schema is created via the CREATE SCHEMA statement, which include a schema name and
an authorization identifier.
An SQL schema is identified by a schema name, and includes an authorization identifier to
indicate the user or account who owns the schema, as well as descriptors for each element in
the schema.
Schema elements include tables, constraints, views, domains, and other constructs (such as
authorization grants) that describe the schema.
Create table
The CREATE TABLE command is used to specify a new relation by giving it a name
and specifying its attributes and initial constraints.
The attributes are specified first, and each attribute is given a name, a data type to
specify its domain of values, and any attribute constraints, such as NOT NULL.
The key, entity integrity, and referential integrity constraints can be specified within
the CREATE TABLE statement after the attributes are declared, or they can be added
later using the ALTER TABLE command .
CREATE TABLE COMPANY.EMPLOYEE ...
It can explicitly make the EMPLOYEE table part of the COMPANY schema.
CREATE TABLE EMPLOYEE . . .
The relations declared through CREATE TABLE statements are called base tables (or base
relations);
This relation and its tuples are actually created and stored as a file by the DBMS.
In SQL the attributes in a base table are considered to be ordered in the sequence in which
they are specified in the CREATE TABLE statement.
The rows (tuples) are not considered to be ordered within a relation.
Create table tablename (c1 datatype(size) constraints , c2 datatype(size)
constraints ...........);
Create table employee (name varchar(15) NOT NULL, DOB date,eid int(10) NOT
NULL);
Data Types Available for Attributes
Numeric data types include integer numbers of various sizes (INTEGER or INT, and
SMALLINT) and floating-point (real) numbers of various precision (FLOAT or REAL, and
DOUBLE PRECISION).
Formatted numbers can be declared by using DECIMAL(i,j)- or DEC(i,j) or NUMERIC(i,j)-
where i, the precision, is the total number of decimal digits and j, the scale, is the number of
digits after the decimal point.
The default for scale is zero, and the default for precision is implementation-defined.
Character String :Character-string data types are either fixed length or variable
length.
Fixed length : char(n),character(n)
n denotes the number of characters.
Variable length:VARCHAR(n) or CHAR VARYING(n) or CHARACTER
VARYING(n), where n is the maximum number of characters.
Bit-string data types
They are either of fixed length or varying length.
Fixed Length: BIT(n)
Varying length: BIT VARYING(n) where n is the maximum number of bits.
The default for n, the length of a character string or bit string, is 1.
Literal bit strings are placed between single quotes but preceded by a B to distinguish
them from character strings; for example,
B'10101’.
Boolean:A boolean data type has thhree possible values TRUE or FALSE or
UNKNOWN.
In SQL, because of the presence of NULL values, a three-valued logic is used, so a
third possible value for a boolean data type is UNKNOWN.
Date and time
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 Constraints
SQL constraints are used to specify the rules for the data in a table.
NOT NULL: This constraint ensures that a column cannot have a null value.
Null value is not permitted for an attribute.
CREATE table student(S_id int(10) NOTNULL,Name varchar(60), Age int);
UNIQUE: This constraint ensures that all values in a column are different.
CREATE table student(S_id int(10) UNIQUE,Name varchar(60), Age int);
DEFAULT: It sets a default value for a column.
CREATE table student(S_id int(10) default 100,Name varchar(60), Age int);
CHECK: It ensures that all values in a column specifies a specific condition.
It can restrict attribute or domain values.
Eg: create table department(dno INT(10) NOT NULL CHECK(Dno>0 AND Dno
<21));
PRIMARY KEY: It uniquely identifies each row in a table.
It’s a combination of not nulll and unique.
CREATE TABLE CUSTOMERS( ID INT NOT NULL PRIMARY KEY , NAME VARCHAR (20) NOT NULL, AGE INT NOT
NULL, ADDRESS CHAR (25) );
• A foreign key is used to link two tables together
• This is sometimes called a referencing key.
• Foreign Key is a column or a combination of columns whose values match a
Primary Key in a different table.
• Foreign Key: It uniquely identifies each row or record in another table.
• Create table table name (colum1 datatype (size) constraints ,col2
datatype(size) ,foreignkey(columnname) references anothertablename(primary key
col name);
Employee(EID,Name,Salary,Dno);
Department(Dnumber,Dname,Location);
Schema Evolution Commands
Drop Command
The DROP command can be used to drop named schema elements, such as tables,
domains, or constraints
To remove schema
Drop schema schema name;
Drop schema company;
There are two drop behavior options: CASCADE and RESTRICT.
DROP SCHEMA COMPANY CASCADE;
It will remove the COMPANY database schema and all its tables, domains, and other
elements.
DROP SCHEMA COMPANY RESTRICT;
Schema is dropped if it has no elements in it.Otherwise drop command won’t be
executed.
Drop table table name;
Drop table employee;
Drop table employee cascade;
With CASCADE option, all constraints and views that reference the table are dropped
automatically from the schema, along with the table itself.
Drop table employee restrict;
With the RESTRICT option a table is dropped only if it is not referenced in any
constraints.
Alter Command
The definition of a base table or of other named schema elements can be changed by
using the ALTER command.
a)Adding an attribute
Alter table tablename add column columnname datatype(size) constraints;
Alter table employee add column job varchar(12);
b)Removing an attribute
Alter table tablename drop column columnname ;
c)modify existing table
Alter table tablename modify columnname newdatatype (new size);
d)Change column name
Alter table tablename change old columnname newcolumn name datatype (size);
Commands for Modifying Database
In SQL, three commands can be used to modify the database: INSERT, DELETE, and
UPDATE.
Insert Command
INSERT is used to add a single tuple to a relation.
We must specify the relation name and a list of values for the tuple.
The values should be listed in the same order in which the corresponding attributes
were specified in the CREATE TABLE command.
Eg insert into employee values(‘John’,’1985-10-03’,30000,D1);
If we want to insert only 2 attributes
insert into employee (Name,Dno) values(‘John’,D1);
Attributes not specified here are set to default or to NULL.
The DELETE Command
The DELETE command removes tuples from a relation.
It includes a WHERE clause to select the tuples to be deleted.
Tuples are explicitly deleted from only one table at a time.
Eg: delete from employee; (delete all records in employee)
delete from employee where name =’John’;
delete from employee where salary between 30,000 and 50,000;
delete can operate on only one relation at a time.
Update command
The UPDATE command is used to modify attribute values of one or more selected
tuples.
A WHERE clause in the UPDATE command selects the tuples to be modified from a
single relation.
SET clause in the UPDATE command specifies the attributes to be modified and and
their new values.
Eg: update student set course =’MCA’; (updates all tuples)
update student set course=’MCA’ where regno=131’;
Eg:change the location and controlling department number of project number 5 to
trivandrum and 10 respectively.
Update project set plocation =’trivandrum’ and dnum=10 where pno=5;
Basic SQL Queries
Select statement
It’s used for retrieving information from a database.
Select <attribute list>
From <table list>
Where condition;
<attribute list> is a list of attribute names whose values are to be retrieved by the query.
<table list> is a list of the relation names required to process the query.
<condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the
query.
Employee(fname,lname,ssn,DOB,address,gender,salary,dno);
Department(dname,dnumber,Mno,M_Start_Date);
Project(pname,pno,plocation,dnum);
Eg : Retrieve the birthdate and address of employee whose name is John Smith.
Select DOB,address
From employee
Where fname=’John’ and lname =’Smith’;
Select all dname from department;
Select distinct dname from department; (Eliminates duplicates)
Display the first name of employees working in CS department and whose salary is
greater than 10000.
Select fname from employee,department where employee.dno =department.dnumber
and dname=’CS’ and salary >10000;
Select fname from employee where dno=10 and salary >10000;
In SQL, the basic logical comparison operators for comparing attribute values with
one another and with literal constants are =, <, <=, >, >=, and <>.
Unspecified where clause and use of asterisk
a) select *
From table name;
Select *
From employee;
It will retrieve all the rows in employee.
Select *
From employee
where dno=5;
Select *
From employee,department
Where dname=’Research’ and dno=dnumber;
It will retrieve the details of all employees working in research department.
Select *
From employee , department;
It will retrieve the Cross product of employee and department relations
Select ssn
From employee;
It will retrieve all employees ssn.
A missing WHERE clause indicates no condition on tuple selection.
If more than one relation is specified in the FROM clause and there is no WHERE
clause, then the CROSS PRODUCT -all possible tuple combinations-of these
relations is selected.
Select ssn,dname
From employee,department;
Ambigous Attribute Names
In SQL the same name can be used for two (or more) attributes if the attributes are in
different relations.
If a query refers to two or more attributes with the same name, we must qualify the
attribute name with the relation name to prevent ambiguity.
Qualify the attribute: It is done by prefixing the relation name to the attribute name
and separating the two by a period.
Employee(name,ssn,DOB,address,gender,salary,dno);
Department(name,dno,Mno,M_Start_Date);
Project(name,pno,plocation,dno);
Eg)Retrieve the name and address of all employees working in finance department.
Select employee.name,address
From employee,department
Where department.name=’finance’ and department.dno=employee.dno;
Retrieve the name and location of all projects run by finance department.
Select project.name,plocation
From project,department
Where department.name=’finance’ and department.dno=project.dno;
Aliases or tuple variables: Alternative relation names declared.
Eg:Select E.name,address
From employee as E,department as D
Where D.name=’finance’ and D.dno=E.dno;
E and D -> Aliases or tuple variables
It is also possible to rename the relation attributes within the query in SQL by giving
them aliases.
Eg: from employee as E(N,sn,db,add,ge,sa,dn)
Renaming attributes of results of a relation
Select name as E_Name and eid as E_ID
From employee;
Ordering the display of tuples
Order by clause
SQL allows the user to order the tuples in the result of a query by the values of one or
more attributes, using the ORDER BY clause.
Instructor(ID,Name,Dept_name,Salary);
Eg)Display the all instructors in the physics department in alphabetic order.
Select name from instructor where Dept_name=’Physics’ order by name;
Order by lists the results in ascending order.
To specify the sorting order we can specify desc for descending and asc for
ascending.
Select * from instructor order by salary desc,name asc;
Substring Pattern Matching and Arithmetic operations
Pattern matching: Performed on strings using the operator like.
Partial strings are specified using two reserved characters
% : replaces an arbitrary number of zero or more characters.
_ : replaces any single character
Find the names of all departments in which their name includes substring finance
Select name
From department
Where name like ‘%finance%’;
Display the name of all employees whose name starts with J and N is the third
character.
Select name
from employee
Where name like ‘J_N%’;
If an underscore or % is needed as a literal character in the string, the character
should be preceded by an escape character, which is specified after the string using
the keyword ESCAPE.
Eg: ‘ab\%cd%’escape’\’
ab%cd
Arithmetic Operations on Queries
Addition(+),subtraction(-),multiplication(*) and division(/) can be applied to numeric
values or attributes with numeric domains.
Give a 20% hike in salary for all employees.
Select name,1.2*salary as updated salary
from employee;
For date and time -operations are incrementing (+) or decrementing (-) .
Date and time can be incremented or decremented by an interval.
An interval value is the result of the difference between two date and time values.
Between
It’s a Comparison operator .
Retrieve the name of all employees whose salary is between 30,000 and 50,000.
Select name from employee
Where salary >=30,000 and salary<=50,000;
Select name
from employee
where salary between 30,000 and 50,000;
Retrieve the name of all employees whose salary is between 30,000 and 50,000 and
works in dno=5.
Select name
from employee
where (salary between 30,000 and 50,000) and dno=5;
SET Operations
Two relations on which we apply the operation have same attributes and attributes
should appear in same order in both the relations and they should have similar
datatypes.
The different set operations are union,intersection and except.
section(courseid,secid,sem,year,classroomno);
Display the set of all courses taught either in winter 2009 or in summer 2020 or both.
(select courseid from section where sem=’winter’ and year=’2009)
Union
(select courseid from section where sem=’summer’ and year=’2020)
Union operation automatically eliminates duplicates.
If we want to retain duplicates give union all instead of union.
Intersection
Display the set of all courses taught in winter 2009 and in summer 2020.
(select courseid from section where sem=’winter’ and year=’2009)
intersect
(select courseid from section where sem=’summer’ and year=’2020)
Intersect operation automatically eliminates duplicates.
If we want to retain duplicates give intersect all instead of intersect.
Except
Display the set of all courses taught in winter 2009 but not in summer 2020.
(select courseid from section where sem=’winter’ and year=’2009)
except
(select courseid from section where sem=’summer’ and year=’2020)
Except operation automatically eliminates duplicates.
If we want to retain duplicates give except all instead of except.
Aggregate Functions in SQL
Functions that takes a collection of values as input and return a single value.
There are mainly 5 built in aggregate functions.
1)Average: avg
2)Minimum: min
3)Maximum:max
4)Total:sum
5)Count:count
●
Employee(name,ssn,Address,salary,dno);
●
Department(dnumber,dname,dloc);
●
Project(pname,pnumber,plocation,dnum);
●
Workson(esn,pno,hours);
Eg)Dislpay the average salary of employees in department number 5.Also display the
highest and lowest salary in department number 5.
Select avg(salary),max(salary),min(salary) from employee where dno=5;
Eg)Display the total salary of all employees in department number 5.
Select sum(salary) from employee where dno=5;
Eg) Retrieve the total number of employees in the company
Select count(*)
From employee;
Display the number of employees in the finance department.
Select count(*)
From employee as E,department as D
Where E.dno=D.dnumber and D.dname=’finance’;
Asterisk (*) refers to the rows (tuples).
COUNT (*) returns the number of rows in the result of the query.
We can also use the COUNT function to count values in a column rather than tuples.
Select count (distinct salary)
From employee;
Aggregation with grouping
If we want to apply the aggregate functions to a group of sets of tuples in a
relation,we can use group by clause.
The attribute or attributes in a group by clause are used to form groups.
Tuples with same values on all attributes in the group by clause are placed in one
group.
Eg) For each department,retrieve the department number ,no of employees in
department and their average salary.
Select dno,count(*),avg(salary)
From employee
Group by dno;
Eg) For each project , retrieve the project number ,project name, and the number of
employees who works on that project.
Select pnumber,pname,count(*)
From project ,works-on
Where pnumber=pno
Group by pnumber,pname;
Sometimes we want to retrieve the values of these functions only for groups that
satisfy certain conditions.
SQL provides a HAVING clause, which can appear in conjunction with a GROUP BY
clause, for this purpose.
HAVING provides a condition on the group of tuples associated with each value of
the grouping attributes.
Only the groups that satisfy the condition are retrieved in the result of the query.
Eg)For each project on which more than two employees work, retrieve the project
number, the project name, and the number of employees who work on the project.
Select pnumber,pname,count(*)
From project ,works-on
Where pnumber=pno
Group by pnumber,pname
Having count(*) >2;
Eg)Display the name and average salary of each department having average salary greater
than or equal to 30,000.
Select name ,avg(salary) from employee,department where dno=dnumber group by dno
having avg(salary)>=30000;
It’s also possible to use explicit set of value in where clause.Such a set is enclosed
in paranthesis in SQL.
Eg) Retrieve the social security numbers of all employees who works on project
numbers 1,2,or 3.
Select distinct essn from works on where pno in (1,2,3);
Eg) Retrieve the details of projects located at bangalore and Kochi
Select * from project where plocation in (‘Bangalore’,’Kochi’);
Join operation
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
JOIN: Returns records that have matching values in both tables.
Employee
Eno Name Dno
101 John 1
102 George 2
103 Maya 1
104 Meera 5
Department
Dnumber Dname Dloc
1 CS Kochi
2 EC Tvm
3 CS Kochi
4 ME Tvm
Select * from employee join department on Dno=Dnumber;
ENo Name Dno Dnumber Dname
Dloc
101 John 1 1 CS Kochi
102 George 2 2 EC Tvm
103 Maya 1 1 CS Kochi
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from
the right table.
The result is null from right side if there is no match.
Select * from employee left join department on Dno=Dnumber;
Eno Name Dno Dnumber Dname
Dloc
101 John 1 1 CS Kochi
102 George 2 2 EC Tvm
103 Maya 1 1 CS Kochi
104 Meera 5 NULL NULL NULL
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table.
Select * from employee right join department on Dno=Dnumber;
Eno Name Dno Dnumber Dname
Dloc
101 John 1 1 CS Kochi
102 George 2 2 EC Tvm
103 Maya 1 1 CS Kochi
NULL NULL NULL 3 CS Kochi
NULL NULL NULL 4 ME Tvm
FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table.
MySQL doesn’t offer a syntax for full outer join.
Full outer join can be implemented using the union of left and right join.
(Select * from employee left join department on Dno=Dnumber)
Union
(Select * from employee right join department on Dno=Dnumber
VIEWS
A view in SQL terminology is a single table that is derived from other tables.
These other tables could be base tables or previously defined views.
A view does not necessarily exist in physical form; it is considered a virtual table, in
contrast to base tables, whose tuples are actually stored in the database.
Create view
The view is given a (virtual) table name (or view name), a list of attribute names, and
a query to specify the contents of the view.
Create view V1 as select name,dname,dloc
From employee,department
Where dno=dnumber;
Create view workson
As select name,pname,hours
From employee,project,works on
Where ssn=esn and pno =pnumber;
Create view dept_info(Dept_name,No of empl,Total_Sal) as
as select dname,count(*),sum(salary)
From department,employee
Where dnumber=dno and group by dname;
DROP VIEW command to dispose a view.
DROP VIEW view name;
Drop view V 1;