Chapter4: SQL-The Relational Database Standard T2: Page 245-284
Chapter4: SQL-The Relational Database Standard T2: Page 245-284
Chapter4: SQL-The Relational Database Standard T2: Page 245-284
Overview
IBM SEQUEL (Structured English QUEry Language) language developed as part of System R project at the
IBM San Jose Research Laboratory
Renamed SQL (Structured Query Language)
ANSI and ISO standard SQL:
SQL-86 or SQL1
SQL-89
SQL-92 or SQL2
SQL:1999 (language name became Y2K compliant) or SQL3
SQL:2003
SQL is a comprehensive database language, it has statements for the data definition, update and query.
It has facilities for defining views on the database, for specifying security and authorization, for defining
integrity constraints and for specifying transaction controls.
Commercial systems offer most, if not all, SQL2 features, plus varying feature sets from later standards and
special proprietary features.
CREATE SCHEMA
Specifies a new database schema by giving it a name, e.g.
create schema company authorization KUMAR;
All users are not authorized to create schemas and its elements
The privileges to create the schema, tables and other construct can explicitly be granted to the relevant users
by DBA.
SQL2 uses the concept of catalog.
Catalog is a named collection of schemas in an SQL environment.
A catalog contains a special schema called INFORMATION_SCHEMA, which provides information on all
the schemas in the catalog and all the element descriptors of all the schema in the catalog to authorized users.
Integrity constraints can be defined for the relations and between the relations of same schema.
Schemas within the same catalog can also share certain elements such as domain definitions.
Domains in SQL
A domain can be declared and the domain can be used with several attributes.
CREATE DOMAIN ENO_TYPE AS CHAR(9)
ENO_TYPE can be used in place of CHAR(9) with SSN, ESSN, MGRSSN.
Data type of domain can be changed that will be reflected for the numerous attributes in the schema and
improves the schema readability.
Drop commands
DROP command is used to remove an element & its definition
DROP SCHEMA: To drop schema
DROP TABLE: To drop table
The relation (or schema) can no longer be used in queries, updates, or any other commands since its
description no longer exists
There are two DROP behaviour options CASCADE and RESTRICT
A CASCADE option is used, To remove schema and its all tables, views & all other elements
Examples:
DROP SCHEMA COMPANY CASCADE;
Schema and its all element are dropped.
There are two DROP behaviour options CASCADE and RESTRICT
A CASCADE option is used, To remove schema and its all tables, views & all other elements
Examples:
DROP SCHEMA COMPANY CASCADE;
Schema and its all element are dropped.
DROP TABLE DEPENDENT CACADE;
Table and its all element are dropped.
If RESTRICT option is used instead of CASCADE
A schema is dropped only if it has no elements, otherwise error will be shown.
A table is dropped only if it is not referenced in any constraint by any other table.
ALTER command
The definition of a base table can be changed by using ALTER TABLE command.
The various possible options include
adding a column
dropping a column
changing the definition of column
adding and dropping the table constraints
ALTER TABLE command is used to add an attribute to one of the base relations
The new attribute will have NULLs in all the tuples of the relation right after the command is executed;
hence, the NOT NULL constraint is not allowed for such an attribute
Example:
ALTER TABLE EMPLOYEE ADD JOB CHAR(12);
The database users must still enter a value for the new attribute JOB for each EMPLOYEE tuple.
This can be done using the UPDATE command or by DEFAULT clause.
ALTER TABLE command is also used to drop an attribute from one of the base relations.
To drop a column, an option CASCADE or RESTRICT should be chosen for drop behaviour .
Example:
ALTER TABLE EMPLOYEE DROP JOB;
If CASCADE, all relations and views that reference the column are dropped automatically from the schema
along with the column.
ALTER TABLE command is also used to modify an attribute of one of the base relations.
Example:
ALTER TABLE EMPLOYEE ALTER MGRSSN DROP DEFAULT;
ALTER TABLE EMPLOYEE ALTER MGRSSN SET DEFAULT;
Adding or dropping constraints
ALTER TABLE EMPLOYEE DROP CONSTRAINT FK_SUPERSSN CASCADE;
Query 1: Retrieve the name and address of all employees who work for the ‘Research’ department.
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME=’Research’ AND
DNUMBER=DNO
Query 2: For every project located in ‘Stafford’, list the project number, the controlling department
number, and the department manager’s last name, address, and birthdate.
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND PLOCATION=’ Stafford’;
Query 1A: Retrieve the name and address of all employees who work for the ‘Research’ department.
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME=’Research’ AND
EMPLOYEE.DNUMBER = DEPARTMENT.DNUMBER
Q1B
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE E, DEPARTMENT D
WHERE DNAME=’Research’ AND
E.DNUMBER = D.DNUMBER;
Query 8: For each employee, retrieve the employee’s name, and the name of his or her immediate
supervisor.
SELECT E.FNAME, E.LNAME, S.FNAME,
S. LNAME
FROM EMPLOYEE E S
WHERE E.SUPERSSN=S.SSN
Unspecified WHERE-clause
A missing WHERE-clause indicates no condition; hence, all tuples of the relations in the FROM-clause are
selected
If more than one relation is specified in the FROM-clause and there is no join condition, then the
CARTESIAN PRODUCT of tuples is selected
Use of * (Asterisk)
To retrieve all the attribute values of the selected tuples, a * is used, which stands for all the attributes
Q1C: SELECT *
FROM EMPLOYEE
WHERE DNO=5;
Q1D: SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME=’Research’ AND
DNO=DNUMBER;
SQL usually treats a table not as a set but rather as a multiset, duplicate tuples can appear more than once in a
table, and in the result of a query.
SQL does not automatically eliminate duplicate tuples in the result of queries because:
Duplicate elimination is expensive.
The user may want to see the duplicates in the result of query.
For an aggregate function, elimination of tuples is not desired.
Query 4: Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’ as a worker or as a manager of the department that controls the project.
(SELECT DISTINCT PNUMBER
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME=’Smith’)
UNION
(SELECT DISTINCT PNUMBER
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=’ Smith’)
Query 12A: Retrieve all employees who were born during the 195Os.
(Here, ‘5’ must be the 8th character of the string according to our format for date, so the BDATE value is 5’, with
each underscore as a place holder for a single arbitrary character.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE BDATE LIKE ‘_ _ _ _ _ 195_’;
(or ‘%195_’)
Arithmetic Operators
The standard arithmetic operators ‘+’, ‘-’, ‘*’, ‘/’ (for addition, subtraction, multiplication, and division,
respectively) can be applied to numeric values in an SQL query result.
Query 13: Show the resulting salaries if every employee working on the ‘ProductX’ project is given a
10% raise.
SELECT FNAME, LNAME, 1.1*SALARY AS INCREASED_SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER AND PNAME=’ProductX’
Comparison Operators
Query 14: Retrieve all the employees in department 5 whose salary is between Rs. 30,000 and Rs. 40,000.
SELECT *
FROM EMPLOYEE
WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO = 5;
SELECT *
FROM EMPLOYEE
WHERE (SALARY >= 30000) AND (SALARY <= 40000) AND DNO = 5;
Query 15: Retrieve a list of employees and the projects each works in, ordered by the employee’s
department, and within each department ordered alphabetically by employee last name, first name.
SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE,
WORKS_ON, PROJECT
WHERE DNUMBER=DNO
AND SSN=ESSN
AND PNO=PNUMBER
ORDER BY DNAME, LNAME, FNAME
Query 4A: Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’ as a worker or as a manager of the department that controls the project.
SELECT DISTINCT PNUMBER FROM PROJECT WHERE PNUMBER IN
(SELECT PNUMBER
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME=’Smith’)
OR
PNUMBER IN
(SELECT PNUMBER
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=’ Smith’)
Q16A
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E, DEPENDENT D
WHERE E.SSN=D.ESSN AND
E.FNAME=D.DEPENDENT_NAME
Query 16B: Retrieve the name of each employee who has a dependent with the same first name as the
employee.
SELECT FNAME, LNAME FROM EMPLOYEE
WHERE EXISTS
(SELECT *
FROM DEPENDENT
WHERE SSN=ESSN AND
FNAME=DEPENDENT_NAME)
Q7. List the names of managers who have at least one dependent.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN)
AND
EXISTS (SELECT *
FROM DEPARTMENT
WHERE SSN=MGRSSN);
Query 3: Retrieve the name of each employee who works on all the projects controlled by department
number 5.
SELECT FNAME, LNAME FROM EMPLOYEE
WHERE ((SELECT PNO, ESSN
FROM WORKS_ON
WHERE SSN=ESSN)
CONTAINS
(SELECT PNUMBER
FROM PROJECT
WHERE DNUM=5))
The second nested query, which is not correlated with the outer query, retrieves the project numbers of all
projects controlled by department 5
The first nested query, which is correlated, retrieves the project numbers on which the employee works,
which is different for each employee tuple because of the correlation
NULLS in SQL
SQL allows queries that check if a value is NULL (missing or undefined or not applicable)
SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL
values, so equality comparison is not appropriate.
Query 14: Retrieve the names of all employees who do not have supervisors.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL
Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the
result
Renaming Attributes
Any attribute which appears in the result can be renamed by adding the qualifier AS followed by the desired
new name.
AS construct can be used for both attribute names and relation names and can be used in both SELECT and
FROM clauses.
Query 8A: For each employee, retrieve the employee’s name, and the name of his or her immediate
supervisor.
SELECT E.NAME AS Supervisee_name,
S.NAME AS Superviser_name,
FROM EMPLOYEE AS E,
EMPLOYEE AS S
WHERE E.SUPERSSN=S.SSN
Joining Tables
We can specify a “joined relation” in the FROM-clause and it looks like any other relation but is the result of
a join
It allows the user to specify different types of joins (regular THETA JOIN, NATURAL JOIN, LEFT OUTER
JOIN, RIGHT OUTER JOIN, CROSS JOIN, etc)
Query 1: Retrieve the name and address of all employees who work for the ‘Research’ department.
Q1: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNUMBER=DNO AND DNAME=’Research’;
Q2: For every project located in ‘Stafford’, list the project number, the controlling department number,
and the department manager’s last name, address, and birthdate.
could be written as follows; this illustrates multiple joins in the joined tables
Q2: SELECT PNUMBER, DNUM,
LNAME, BDATE, ADDRESS
FROM ((PROJECT JOIN DEPARTMENT ON
DNUM=DNUMBER)
JOIN EMPLOYEE ON MGRSSN=SSN)
WHERE PLOCATION=’ Stafford’;
Aggregate Functions
Include COUNT, SUM, MAX, MIN, and AVG
Some SQL implementations may not allow more than one function in the SELECT-clause
Query 19: Find the sum of the salary of all employees, the maximum salary, the minimum salary, and the
average salary among employees.
SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)
FROM EMPLOYEE;
Query 20: Find the sum of the salary of all employees, the maximum salary, the minimum salary, and the
average salary among employees who work for the ‘Research’ department.
SELECT SUM(SALARY), MAX(SALARY), MIN(SALARY), AVG(SALARY)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND
DNAME= ‘Research’
Query 20: Find the sum of the salary of all employees, the maximum salary, the minimum salary, and the
average salary among employees who work for the ‘Research’ department.
SELECT SUM(SALARY), MAX(SALARY), MIN(SALARY), AVG(SALARY)
FROM (EMPLOYEE JOIN DEPARTMENT ON DNO=DNUMBER)
WHERE DNAME= ‘Research’;
Query 22: Retrieve the total number of employees in the Research Department.
SELECT COUNT(*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND DNAME=‘Research’;
Query 5: List the names of all employees with two or more dependents.
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE (SELECT COUNT(*)
FROM DEPENDENT
WHERE SSN = ESSN) >=2;
Grouping
SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-
clause
In many cases, we want to apply the aggregate functions to subgroups of tuples in a relation
Each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s)
Then aggregate function is applied to each subgroup independently
Query 20: For each department, retrieve the department number, the number of employees in the
department, and their average salary.
SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO
The EMPLOYEE tuples are divided into groups--each group having the same value for the grouping attribute
DNO
The COUNT and AVG functions are applied to each such group of tuples separately
The SELECT-clause includes only the grouping attribute and the functions to be applied on each group of
tuples
A join condition can be used in conjunction with grouping
Query 25: For each project, retrieve the project number, project name, and the number of employees who
work on that project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME;
In this case, the grouping and functions are applied after the joining of the two relations
The Having-clause
Sometimes we want to retrieve the values of these functions for only those groups that satisfy certain
conditions
The HAVING-clause is used for specifying a selection condition on groups (rather than on individual tuples)
Query 26: For each project on which more than two employees work, retrieve the project number, project
name, and the number of employees who work on that project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER , PNAME
HAVING COUNT (*) >2;
Query 27: For each project, retrieve the project number, project name, and the number of employees from
department 5 who work on that project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER = PNO
AND SSN = ESSN AND DNO = 5
GROUP BY PNUMBER, PNAME;
Query 28: For each department that has more than five employees, retrieve the department number, and
the number of its employees who are earning more than 40000.
SELECT DNUMBER, COUNT (*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNUMBER = DNO AND SALARY>40000 AND DNO IN (SELECT DNO
FROM EMPLOYEE
GROUP BY DNO
HAVING COUNT (*)>5)
GROUP BY DNUMBER;
An alternate form of INSERT specifies explicitly the attribute names that correspond to the values in the new
tuple
Attributes with NULL values can be left out
Example: U1A: Insert a tuple for a new EMPLOYEE for whom we only know the FNAME, LNAME, and
SSN attributes.
INSERT INTO EMPLOYEE (FNAME, LNAME, SSN) VALUES (‘Richard’, ‘Marini’, ‘653298653’)
Only the constraints specified in the DDL commands are automatically enforced by the DBMS when updates
are applied to the database
Another variation of INSERT allows insertion of multiple tuples resulting from a query into a relation
Example: Create a temporary table that has the Department name, number of employees, and total salaries
for each department.
U3A:
CREATE TABLE DEPTS_INFO (DEPT_NAME VARCHAR(1O), NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);
U3B:
INSERT INTO DEPTSINFO (DEPT_NAME,
NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME;
Note: The DEPTS_INFO table may not be up-to-date if we change the tuples in either the DEPARTMENT or
the EMPLOYEE relations after issuing U3B.
(We have to create a view (see later) to keep such a table up to date.)
Example: Change the location and controlling department number of project number 10 to ‘Bellaire’ and
5, respectively.
U5:
UPDATE PROJECT
SET PLOCATION = ‘Bellaire’, DNUM = 5 WHERE PNUMBER=1O;
Example: Give all employees in the ‘Research’ department a 10% raise in salary.
U6:
UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME=’Research’);
In this request, the modified SALARY value depends on the original SALARY value in each tuple
The reference to the SALARY attribute on the right of = refers to the old SALARY value before modification
The reference to the SALARY attribute on the left of= refers to the new SALARY value after modification
Views in SQL
A view is a “virtual” table that is derived from other tables (base tables or other views)
A view does not exist physically.
View allows for limited update operations.
View allows full query operations
It is a convenient way for expressing certain operations e.g. instead of writing a complicated query again and
again, a view can be created for the query, and simple SELECT command can be used to view the query
result.
SQL command:
CREATE VIEW <view> <a possible list of attribute names > < a query to specify the contents of the view>
If attribute names are not specified, these will be same as in the base relations)
Select first name, last name of employee along with the project name and number of hours on which they are
working on.
We can specify SQL queries on a newly created view which simplify the query
SELECT FNAME, LNAME FROM WORKS_ON_NEW WHERE PNAME=‘titan’;
When no longer needed, a view can be dropped
DROP VIEW WORKS_ON_NEW;
A view is always supposed to be up to date; if we modify base tables.
Hence view is not realized at the time of view definition but rather at the time we specify a query on the view.
View Update
Update on a single view without aggregate operations
Update may map directly to an update on the underlying base table
Views involving joins:
an update may map to an update on the underlying base relations but not always possible
Views defined using groups and aggregate functions are not updateable
UPDATE EMP_DEPT
SET DNO = 4
WHERE ENAME = ‘SMITH’;
UVI: update the PNAME attribute of ‘John Smith’ from ‘ProductX’ to ‘ProductY’
UPDATE WORKS_ON_NEW
SET PNAME = ‘ProductY’
WHERE LNAME = ‘Smith’
AND FNAME = ‘John’
AND PNAME = ‘ProductX’;
b) UPDATE PROJECT
SET PNAME = ‘ProductY’
WHERE PNAME = ‘ProductX’);
-------------not feasible
a) UPDATE WORKS_ON
SET PNO = (SELECT PNUMBER FROM PROJECT
WHERE PNAME = ‘ProductY’)
WHERE ESSN IN (SELECT SSN
FROM EMPLOYEE
WHERE LNAME = ‘Smith’ AND FNAME = ‘John’)
AND PNO IN (SELECT PNUMBER FROM PROJECT WHERE PNAME = ‘ProductX’);
V2:
CREATE VIEW DEPT_INFO(DEPT_NAME, NO_OF_EMPS, TOTAL_SAL)
AS
SELECT DNAME, COUNT(*), SUM(SALARY)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO = DNUMBER
GROUP BY DNAME;
Embedded SQL
Embedding database commands in a general-purpose programming language:
Database statements are embedded into the host programming language, they are identified by a special
prefix.
For example, the prefix for embedded SQL in the string EXEC SQL, which precedes all SQL commands in a
host language program.
A precompiler or preprocessor scans the source program code to identify database statements and extract
them for processing by the DBMS
They are replaced in the program by function call to the DBMS-generated code.
Impedance Mismatch
A problem occurs because of difference between the database model and the programming language model.
For example the practical relational model has three main constructs: attributes and their datatypes, tuples and
tables.
The first problem that may occur is that the datatype of the programming language differ from attribute data
types in the data model.
Hence it is necessary to have a binding for each host programming language that specifies for each attribute
type and the compatible programming language types.
Data types in C++ and java differ from the SQL datatypes
Another problem occurs because the results of most queries are sets or multisets of tuples, and each tuple is
formed of sequence of attribute values.
In the program it is often necessary to access individual data values within individual tuples for printing or
processing.
Hence a binding is needed to map the query result data structure, which is a table, to an appropriate data
structure in the programming language.
A mechanism is needed to loop over the tuples in a query result in order to access a single tuple at a time and
extract individual values from the tuple.
The extracted attribute values are typically copied to appropriate program variables for further processing by
the program.
A cursor or iterator variable is used to loop over the tuples in a query result.
Individual values within each tuple are typically extracted into distinct program variables of the appropriate
type.
Impedance mismatch is less of problem when a special database programming language is designed that uses
the same data model and datatypes as the database model.
One example of such a language is Oracle’s PL/SQL.
For object databases, the object data model is quite similar to the data model of the Java programming
language,
Thus impedance mismatch is greatly reduced when Java is used as host language for accessing a Java-
compatible object database.
The variables SQLCODE and SQLSTATE are used to communicate errors and exception conditions between
the database system and the program.
Communicating between the program and the DBMS using SQLCODE and SQLSTATE:
The two special communication variables that are used by the DBMS to communicate exception or error
conditions to the program are SQLCODE and SQLSTATE.
The SQLCODE variable shown in the example is an integer variable.
After each database command is executed, the DBMS returns a value in SQLCODE.
A value of 0 indicates that the statement was executed successfully by the DBMS.
If SQLCODE>0 (or, more specifically, if SQLCODE=100), this indicates that no more data are available in a
query result.
If SQL<0,this indicates some error has occurred.
In some systems-for example, in the Oracle RDBMS-SQLCODE is field in record structure called SQLCA
(SQL communication area), so it is referenced as SQLCA.SQLCODE.
In this case, the definition of SQLCA must be included in the C program by including the following line:
EXEC SQL include SQLCA;
In the later versions of the SQL standard, a communication variable called SQLSTATE was added, which is
a string of five characters.
A value of ‘00000’ in SQLSTATE indicates no error or exception; other variables indicates various errors or
exceptions.
For example, ‘02000’ indicates ‘no more data’ when using SQLSTATE.
Currently, both SQLSTATE and SQLCODE are available in the SQL standard.
Many of the error and exception codes returned in SQLSTATE are supposed to be standardized for all SQL
vendors and platforms, where the codes returned in SQLCODE are not standardized but are defined by the
DBMS vendor.
Hence is better to use SQLSTATE because this makes error handling in the application programs independent
of a particular DBMS.
Dynamic SQL
Specifying Queries at Runtime Using Dynamic SQL:
The embedded SQL queries are written as part of the host program source code.
Hence, any time we want to write to a different query, we must write a new program, and go through all the
steps involved (compiling, debugging, testing, and so on).
Sometimes, it is convenient to write a program that can execute different SQL queries or updates dynamically
or runtime.
For example, we may want to write a program that accepts an SQL query typed from the monitor, executes it,
and displays its result, such as the interactive interfaces available for most relational DBMSs.
Another example is when a user friendly interface generates SQL queries dynamically for the user based on
point and click operation on a graphical schema.
A string is input by the user into the string variable sqlupdatestring.
String should a SQL Command.
String is then prepared as an SQL command by associating it with the SQL variable sqlcommand.
SQL command executes.
In this case no syntax check or other types of checks on the command are possible, whereas in embedded
SQL the query could be checked at compile time because its text was in the program source code.
A dynamic query is much more complicated.
Because we do not know the type or the number of attributes to be retrieved by the SQL query when we are
writing the program.
A complex data structure is sometimes needed to allow for different number and the dynamic query.
The reason for separating PREPARE and EXECUTES is that if the command is to be executed multiple
times in a program, it can be prepared only once.
Preparing the command generally invokes syntax and other types of checks by the systems as well as
generating the code for executing it.
It is possible to combine the PREPARE and EXECUTE commands into a single statement.
EXEC SQL EXECUTES IMMEDIATE : sql updatesting ;
This is useful if the command is to be executed only once.
Alternatively, one can separate the two to catch any errors after the PREPARE statement, if any.
Loops can have names, and there is a LEAVE<loop name> statement to break a loop where a condition is
satisfied.
CREATE FUNCTION Dept_size (IN deptno INTEGER)
RETURNS VARCHAR [17]
DECLARE No_of_emps INTEGER;
SELECT COUNT(*) INTO No_of_emps
FROM EMPLOYEE WHERE Dno = deptno;
If No_of_emps > 100 THEN RETURN “HUGE”
ELSEIF No_of_emps >25 THEN RETURN “LARGE”
ELSEIF No_of_emps >10 THEN RETURN “MEDIUM”
ELSE RETURN “SMALL”
ENDIF
End of Chapter 4
SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between
certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be
unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
Look at the "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can have the same P_Id.
The P_Id distinguishes two persons even if they have the same name.
Next, we have the "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column refers to the persons in the
"Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id" column.
Different SQL JOINs
Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
SQL INNER JOIN Keyword
The INNER JOIN keyword return rows when there is at least one match in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: INNER JOIN is the same as JOIN.