You are on page 1of 27

• Consider the foreign key constraint from the dept name attribute of

instructor to the department relation.


• Give examples of inserts and deletes to these relations, which can
cause a violation of the foreign key constraint.
Consider the relational database of Figure 2.14. What are the
appropriate primary keys?
Introduction to SQL
• IBM developed the original version of SQL,
originally called Sequel
• Many products now support the SQL
language. SQL has clearly established itself as
the standard relational database language.
• The SQL language has several parts:
– Data-definition language (DDL).
Provides commands to define
Relation schemas,
Deleting relations, and
Modifying relation schemas.
– Data-manipulation language (DML).
Provides the ability to
Query information from the database
To insert tuples into,
Delete tuples from, and
Modify tuples in the database.
• The SQL language has several parts:…
– Integrity: Commands for specifying integrity con- straints
that the data stored in the database must satisfy. Updates
that violate integrity constraints are disallowed.
– View definition: The SQL DDL includes commands for
defining views.
– Transaction control: SQL includes commands for
specifying the beginning and ending of transactions.
– Embedded SQL and dynamic SQL: Embedded and
dynamic SQL define how SQL statements can be
embedded within general-purpose programming lan-
guages, such as C, C++, and Java.
– Authorization: The SQL DDL includes commands for
specifying access rights to relations and views.
SQL Data Definition (DDL)
• To specify set of relations
• To specify information about each relation
– The schema for each relation.
– The types of values associated with each attribute.
– The integrity constraints.
– The set of indices to be maintained for each relation
– The security and authorization information for each
relation.
– The physical storage structure of each relation on
disk.
3.2.2 Basic Schema Definition
• To define an SQL relation use create table
command.
create table r
(A1 D1,
A2 D2, ...,
An Dn,
< integrity-constraint1>,
...,
< integrity-constraintk >);
Number of different integrity constraints.

• Primary key (Aj1 , Aj2 ,..., Ajm ):


This specification says that attributes Aj1 , Aj2 ,...,
Ajm form the primary key for the relation.
The primary- key attributes are required to be
nonnull and unique; that is, no tuple can have a
null value for a primary-key attribute, and no two
tuples in the relation can be equal on all the
primary-key attributes.
Optional, but good to specify
Number of different integrity constraints…

• Foreign key (Ak1 , Ak2 ,..., Akn )referencess:


It says that the values of attributes (Ak1 , Ak2 ,...,
Akn ) for any tuple in the relation must correspond
to values of the primary key attributes of some
tuple in relations.
• not null:
It specifies that the null value is not allowed for that
attribute; in other words, the constraint excludes
the null value from the domain of that attribute.
A newly created relation is empty initially.
Use the insert command to load data into the relation.
For example, to insert the fact that there is an instructor
named Smith in the Biology department with
instructor id 10211 and a salary of $66,000
insert into instructor values (10211, ’Smith’, ’Biology’,
66000);
To delete tuples from a relation use delete command .
The command
delete from student;
To remove a relation from an SQL database drop
table command.
The drop table command deletes all
information about the dropped relation from
the database.
The command drop table r; schema for r will be
deleted
Note: delete from r; retains relation r, but
deletes all tuples in r
• alter table command :
– to add attributes to an existing relation.
– All tuples in the relation are assigned null as the
value for the new attribute.
alter table r add A D;
– drop attributes from a relation by the command
alter table r drop A;
A partial SQL DDL definition of the university
database
Basic Structure of SQL Queries
• The basic structure of an SQL query consists of
three clauses: select, from, and where.
3.3.1 Queries on a Single Relation
• Ex
select name from instructor;
select dept name from instructor; (Check the
output)
select distinct dept name from instructor;
The keyword all to specify explicitly that duplicates
are not removed:
select all dept name from instructor;
The select clause may also contain arithmetic
expressions involving the op- erators +, −, ∗,
and / operating on constants or attributes of
tuples.
For example, the query:
select ID, name, dept name, salary * 1.1 from
instructor;
• The where clause allows us to select only
those rows in the result relation of the from
clause that satisfy a specified predicate.
• Consider the query “Find the names of all
instructors in the Computer Science
department who have salary greater than
$70,000.”

select name from instructor where dept name =


’Comp. Sci.’ and salary > 70000;
• Ex
select name from instructor;
select dept name from instructor; (Check the
output)
select distinct dept name from instructor;
The keyword all to specify explicitly that duplicates
are not removed:
select all dept name from instructor;
3.3.2 Queries on Multiple Relations
• Queries often need to access information from
multiple relations.
• Ex: Suppose to answer the query
“Retrieve the names of all instructors, along with
their department names and department building
name.”
To answer the query, each tuple in the instructor
relation must be matched with the tuple in the
department relation whose dept_name value matches
the dept_name value of the instructor tuple
3.3.2 Queries on Multiple Relations
list the relations that need to be accessed in the
from clause specify the matching condition in
the where clause.

select name, instructor.dept name, building


from instructor, department
where instructor.dept name= department.dept
name;

You might also like