0% found this document useful (0 votes)
24 views34 pages

DBMS Lecture 8

This document provides an overview of SQL data definition and data types. It discusses the CREATE TABLE command for defining tables and specifying attributes, data types, and constraints. Key concepts covered include: - Using CREATE TABLE to define base tables with attributes, primary keys, foreign keys, and other constraints - Built-in SQL data types like numeric, character, date, boolean, and timestamp - Specifying default values, check constraints, and referential integrity constraints - Ambiguous names, relation aliases, and renaming attributes in queries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views34 pages

DBMS Lecture 8

This document provides an overview of SQL data definition and data types. It discusses the CREATE TABLE command for defining tables and specifying attributes, data types, and constraints. Key concepts covered include: - Using CREATE TABLE to define base tables with attributes, primary keys, foreign keys, and other constraints - Built-in SQL data types like numeric, character, date, boolean, and timestamp - Specifying default values, check constraints, and referential integrity constraints - Ambiguous names, relation aliases, and renaming attributes in queries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Database Management System

(TCS 503)
Lecture- 8
B. Tech- CSE
3rd Year (5th semester)

REHAN, Assistant Professor


+91-9548283808
SQL Data Definition and Data Types
• SQL is a comprehensive database language: It has
statements for data definitions, queries, and
updates. Hence, it is both a DDL and a DML.

• SQL uses the terms table, row, and column for


the formal relational model terms relation, tuple,
and attribute, respectively.
– 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).
The CREATE TABLE Command in SQL
• 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 EMPLOYEE (…….;
• The relations declared through CREATE TABLE
statements are called base tables (or base relations);
this means that the relation and its tuples are actually
created and stored as a file by the DBMS.

• Base relations are distinguished from virtual relations,


created through the CREATE VIEW statement, which
may or may not correspond to an actual physical file.

• 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. However, rows (tuples)
are not considered to be ordered within relation.
It is important to note that in table above, there are some foreign keys that may cause
errors because they are specified either via circular references or because they refer
to a table that has not yet been created.
For example, the foreign key Super_ssn in the EMPLOYEE table is a circular reference because it
refers to the table itself.
The foreign key Dno in the EMPLOYEE table refers to the DEPARTMENT table, which has not
been created yet.
To deal with this type of problem, these constraints can be left out of the initial CREATE TABLE
statement, and then added later using the ALTER TABLE statement.
Attribute Data Types and Domains in
SQL
• Numeric: 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.

• Character-string

• DATE: The DATE data type has ten positions, and its components are YEAR,
MONTH, and DAY in the form YYYY-MM-DD.

• Boolean: A Boolean data type has the traditional values of TRUE or FALSE.

• A timestamp
Specifying Constraints in SQL
• SQL allows NULLs as attribute values, a constraint NOT
NULL may be specified if NULL is not permitted for a
particular attribute.

• This is always implicitly specified for the attributes that


are part of the primary key of each relation.

• It is also possible to define a default value for an


attribute by appending the clause
– DEFAULT <value> to an attribute definition.
• The default value is included in any new tuple if an
explicit value is not provided for that attribute.
Set default value of college_country column to 'US'

CREATE TABLE College ( college_id INT PRIMARY


KEY, college_code VARCHAR(20), college_country
VARCHAR(20) DEFAULT 'US' );

• Here, the default value of


the college_country column is US. If we try to
store a NULL value in
the college_country column, its value will
be US by default.
• Another type of constraint can restrict attribute
or domain values using the CHECK clause
following an attribute or domain definition.
– WHERE (AGE >20)
Specifying Key and Referential
Integrity Constraints
• Keys and referential integrity constraints are very
important, there are special clauses within the
CREATE TABLE statement to specify them.
• The PRIMARY KEY clause specifies one or more
attributes that make up the primary key of a
relation. (NOT NULL and UNIQUE)

• Referential integrity is specified via the FOREIGN


KEY clause.
• A referential integrity constraint can be violated
when tuples are inserted or deleted, or when a
foreign key or primary key attribute value is
modified. (SET NULL, cascade)
Specifying Constraints on Tuples
Using CHECK
• In addition to key and referential integrity constraints, which are specified
by special keywords, other table constraints can be specified through
additional CHECK clauses at the end of a CREATE TABLE statement.

• These can be called tuple-based constraints because they apply to each


tuple individually and are checked whenever a tuple is inserted or
modified.

• For example, suppose that the DEPARTMENT table had an additional


attribute Dept_create_date, which stores the date when the department
was created. Then we could add the following CHECK clause at the end of
the CREATE TABLE statement for the DEPARTMENT table to make sure that
manager’s start date is later than the department creation date.

• CHECK (Dept_create_date <= Mgr_start_date);


Basic Retrieval Queries in SQL
• SQL has one basic statement for retrieving information from a database: the
SELECT statement.

• The SELECT statement is not the same as the SELECT operation of relational
algebra.

SELECT <attribute list>


FROM <table list>
WHERE <condition>;
where
• <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.
Example
• Retrieve the birth date and address of the
employee(s) whose name is ‘John B. Smith’.

SELECT Bdate, Address


FROM EMPLOYEE
WHERE Fname=‘John’ AND Minit=‘B’ AND Lname=‘Smith’;
Ambiguous Attribute Names, Aliasing,
Renaming, and Tuple Variables
• In SQL, the same name can be used for two (or more)
attributes as long as the attributes are in different
relations.

• If this is the case, and a multi-table query refers to two


or more attributes with the same name, we must
qualify the attribute name with the relation name to
prevent ambiguity. This is done by prefixing the
relation name to the attribute name and separating the
two by a period “.”.
– SELECT Fname, EMPLOYEE.Name, Address
FROM EMPLOYEE, DEPARTMENT
• An alias can follow the keyword AS.

• for example, by writing EMPLOYEE E in the FROM clause.

SELECT E.Fname, E.Lname


FROM EMPLOYEE AS E

• It is also possible to rename the relation attributes within


the query in SQL by giving them aliases.
• For example, if we write

EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd, Addr, Sex, Sal, Sssn, Dno)

in the FROM clause, Fn becomes an alias for Fname, Mi for


Minit, Ln for Lname, and so on.
Recursive query
• Query 8. For each employee, retrieve the employee’s first and last name
and the first and last name of his or her immediate supervisor.

SELECT E.Fname, E.Lname, S.Fname, S.Lname


FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;

• we can think of E and S as two different copies of the EMPLOYEE relation;


the first, E, represents employees in the role of supervisees or
subordinates; the second, S, represents employees in the role of
supervisors.

• We can now join the two copies. Of course, in reality there is only one
EMPLOYEE relation, and the join condition is meant to join the relation
with itself by matching the tuples that satisfy the join condition
E.Super_ssn = S.Ssn.
Unspecified WHERE Clause
and Use of the Asterisk
• A missing WHERE clause indicates no
condition on tuple selection; hence, all tuples
of the relation specified in the FROM clause
qualify and are selected for the query result. 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.
• Q1: SELECT Ssn
FROM EMPLOYEE;
• Q2: SELECT Ssn, Dname
FROM EMPLOYEE, DEPARTMENT;
• For example, Query q selects all EMPLOYEE
Ssns, and Query 10 selects all combinations of
an EMPLOYEE Ssn and a DEPARTMENT Dname,
regardless of whether the employee works for
the department or not.

– Notice that Q2 is similar to a CROSS PRODUCT


operation followed by a PROJECT
• To retrieve all the attribute values of the
selected tuples, we do not have to list the
attribute names explicitly in SQL; we just
specify an asterisk (*), which stands for all the
attributes.
Tables as Sets in SQL
• As we mentioned earlier, 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.

• If we do want to eliminate duplicate tuples from


the result of an SQL query, we use the keyword
DISTINCT in the SELECT clause, meaning that only
distinct tuples should remain in the result.

• In general, a query with SELECT DISTINCT


eliminates duplicates, whereas a query with
SELECT ALL does not.
• There are set union (UNION), set difference
(EXCEPT), and set intersection (INTERSECT)
operations.
Substring Pattern Matching and
Arithmetic Operators
• The first feature allows comparison conditions
on only parts of a character string, using the
LIKE comparison operator. This can be used
for string pattern matching.

• Partial strings are specified using two reserved


characters: % replaces an arbitrary number of
zero or more characters, and the underscore
(_) replaces a single character.
• The standard arithmetic operators for addition
(+), subtraction (–), multiplication (*), and
division (/) can be applied to numeric values
or attributes with numeric domains.

• For string data types, the concatenate


operator || can be used in a query to append
two string values.

• Another comparison operator, which can be


used for convenience, is BETWEEN,
• SELECT *
FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000) AND Dno = 5;

• The condition (Salary BETWEEN 30000 AND 40000) in Q14 is


equivalent to the condition ((Salary >= 30000) AND (Salary <=
40000)).
Ordering of Query Results
• SQL allows the user to order the tuples in the
result of a query by the values of one or more of
the attributes that appear in the query result, by
using the ORDER BY clause.

• The default order is in ascending order of values.


We can specify the keyword DESC if we want to
see the result in a descending order of values.
The keyword ASC can be used to specify
ascending order explicitly.
• A simple retrieval query in SQL can consist of up to four
clauses, but only the first two—SELECT and FROM—are
mandatory.

– SELECT <attribute list>


FROM <table list>
[ WHERE <condition> ]
[ ORDER BY <attribute list> ];

• Two additional clauses GROUP BY and HAVING. These can


be used to provide additional power to aggregate
functions; and various types of joins that can combine
records from various tables in different ways.
INSERT, DELETE, and UPDATE
Statements in SQL
• In SQL, three commands can be used to modify the
database: INSERT, DELETE, and UPDATE.

– In its simplest form, INSERT is used to add a single tuple to


a relation.

– The values should be listed in the same order in which the


corresponding attributes were specified in the CREATE
TABLE command.
• INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn)
VALUES (‘Richard’, ‘Marini’, 4, ‘653298653’);
• The DELETE command removes tuples from a
relation. It includes a WHERE clause, similar to
that used in an SQL query, to select the tuples to
be deleted.
• DELETE FROM EMPLOYEE
WHERE Lname=‘Brown’;

• DELETE FROM EMPLOYEE


WHERE Ssn=‘123456789’;
• The UPDATE command is used to modify attribute
values of one or more selected tuples.

• As in the DELETE command, a WHERE clause in the


UPDATE command selects the tuples to be modified
from a single relation. However, updating a primary
key value may propagate to the foreign key values
of tuples in other relations if such a referential
triggered action is specified in the referential
integrity constraints of the DDL.
Comparisons Involving NULL
and Three-Valued Logic
• SQL has various rules for dealing with NULL
values.
• NULL is used to represent a missing value, but
that it usually has one of three different
interpretations
– value unknown (exists but is not known),
– value not available (exists but is purposely withheld),
or
– value not applicable (the attribute is undefined for
this tuple).
• Unknown value. A person’s date of birth is not known,
so it is represented by NULL in the database.

• Unavailable or withheld value. A person has a home


phone but does not want it to be listed, so it is
withheld and represented as NULL in the database.

• Not applicable attribute. An attribute


Last_College_Degree would be NULL for a person who
has no college degrees because it does not apply to
that person.
– When a NULL is involved in a comparison operation, the
result is considered to be UNKNOWN (it may be TRUE or it
may be FALSE).
• SQL uses a three-valued logic with values TRUE, FALSE, and UNKNOWN instead of the
standard two-valued (Boolean) logic with values TRUE or FALSE.
• It is therefore necessary to define the results (or truth values) of three-valued logical
expressions when the logical connectives AND, OR, and NOT are used.

• For example, the result of (FALSE AND UNKNOWN) is FALSE, whereas the result of
(FALSE OR UNKNOWN) is UNKNOWN. The result of the NOT logical operation. Notice
that in standard Boolean logic, only TRUE or FALSE values are permitted; there is no
UNKNOWN value.
• SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;

SQL allows queries that check whether an


attribute value is NULL.
NESTED QUERIES
• SELECT DISTINCT Pnumber
FROM PROJECT
WHERE Pnumber IN
( SELECT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND
Mgr_ssn=Ssn AND Lname=‘Smith’ )
OR
Pnumber IN
( SELECT Pno
FROM WORKS_ON, EMPLOYEE
WHERE Essn=Ssn AND Lname=‘Smith’ );

• The first nested query selects the project


numbers of projects that have an employee with
last name ‘Smith’ involved as manager, while the
second nested query selects the project numbers
of projects that have an employee with last name
‘Smith’ involved as worker.

You might also like