Dbms Module 3 Chapter 6

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 53

SQL: SQL data definition and data types,

specifying constraints in SQL, retrieval queries


in SQL, INSERT, DELETE, and UPDATE
statements in SQL, Additional features of SQL.
6.1 SQL Data Definition and Data Types
• 6.1.1 Schema and Catalog Concepts in SQL
• 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, types, constraints, views, domains, and other
constructs (such as authorization grants) that describe the schema.
• A schema is created via the CREATE SCHEMA statement
CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
• Not all users are authorized to create schemas and schema elements. The
privilege to create schemas, tables, and other constructs must be explicitly
granted to the relevant user accounts by the system administrator or DBA.
• Catalog—a named collection of schemas
• A catalog always contains a special schema called
INFORMATION_SCHEMA, which provides information on all the
schemas in the catalog and all the element descriptors in these
schemas.
• Integrity constraints such as referential integrity can be defined
between relations only if they exist in schemas within the same
catalog. Schemas within the same catalog can also share certain
elements, such as type and domain definitions.
6.1.2 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 possibly 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.
• We can explicitly attach the schema name to the relation name, separated by a period.
• For example, by writing
CREATE TABLE COMPANY.EMPLOYEE
rather than
CREATE TABLE EMPLOYEE
we can explicitly (rather than implicitly) make the EMPLOYEE table part of the COMPANY schema
• The relations declared through CREATE TABLE statements are called
base tables (or base relations); this means that the table and its rows
are actually created and stored as a file by the DBMS. Base relations
are distinguished from virtual relations, created through the CREATE
VIEW statement.
• 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 a table (relation).
6.1.3 Attribute Data Types and Domains in SQL
The basic data types
■ Numeric –
• INTEGER (INT, and SMALLINT )
• floating-point numbers (FLOAT or REAL, and DOUBLE PRECISION).
• Formatted numbers - DECIMAL(i, j) or DEC(i, j) or NUMERIC(i, j)
where i is the precision, the total number of decimal digits
j, the scale, is the number of digits after the decimal point.
■ Character
• string data types are either fixed length—CHAR(n) or CHARACTER(n), where n is the
number of characters—or varying length— VARCHAR(n) or CHAR VARYING(n) or
CHARACTER VARYING(n), where n is the maximum number of characters.
• Another variable-length string data type called CHARACTER LARGE OBJECT or CLOB
is also available to specify columns that have large text values, such as documents.
The CLOB maximum length can be specified in kilobytes (K), megabytes (M), or
gigabytes (G). For example, CLOB(20M) specifies a maximum length of 20
megabytes.
■ Bit-string
• either of fixed length n—BIT(n)—or 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’.5
• Another variable-length bitstring data type called BINARY LARGE OBJECT or
BLOB is also available to specify columns that have large binary values, such
as images. As for CLOB, the maximum length of a BLOB can be specified in
kilobits (K), megabits (M), or gigabits (G). For example, BLOB(30G) specifies
a maximum length of 30 gigabits.
■ A Boolean data type
• has the traditional values of TRUE or FALSE.
• 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.
■ 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
• months should be between 1 and 12 and days must be between 01 and 31
• A TIME WITH TIME ZONE data type includes an additional six positions for specifying the
displacement from the standard universal time zone, which is in the range +13:00 to –12:59
in units of HOURS:MINUTES.
■ A timestamp data type (TIMESTAMP)
• includes the DATE and TIME fields, plus a minimum of six positions for
decimal fractions of seconds and an optional WITH TIME ZONE qualifier.
for example, TIMESTAMP ‘2014-09-27 09:12:47.648302’.
• Literal values are represented by single-quoted.
■ INTERVAL data type.
• This specifies an interval—a relative value that can be used to increment or
decrement an absolute value of a date, time, or timestamp.
• Intervals are qualified to be either YEAR/MONTH intervals or DAY/TIME
intervals
• to change the data type for a domain that is used by numerous
attributes in a schema
CREATE DOMAIN SSN_TYPE AS CHAR(9);
We can use SSN_TYPE in place of CHAR(9) for the attributes Ssn and
Super_ssn of EMPLOYEE, Mgr_ssn of DEPARTMENT, Essn of
WORKS_ON, and Essn of DEPENDENT.
• It may not be available in some implementations of SQL.
6.2 Specifying Constraints in SQL
• 6.2.1 Specifying Attribute Constraints and Attribute Defaults
• 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, but it can be specified for any other attributes
whose values are required not to be NULL.
• It is also possible to define a default value for an attribute by appending the
clause DEFAULT to an attribute definition.
• The default value is included in any new tuple if an explicit value is not
provided for that attribute.
• If no default clause is specified, the default value is NULL for attributes that
do not have the NOT NULL constraint.
• Another type of constraint can restrict attribute or domain values using the CHECK
clause following an attribute or domain definition.
• For example, suppose that department numbers are restricted to integer numbers
between 1 and 20; then, we can change the attribute declaration of Dnumber in the
DEPARTMENT to the following:
Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);
• The CHECK clause can also be used in conjunction with the CREATE DOMAIN statement.
For example, we can write the following statement:
CREATE DOMAIN D_NUM AS INTEGER
CHECK (D_NUM > 0 AND D_NUM < 21);
We can then use the created domain D_NUM as the attribute type for all attributes that
refer to department numbers such as Dnumber of DEPARTMENT, Dnum of PROJECT, Dno
of EMPLOYEE, and so on.
6.2.2 Specifying Key and Referential Integrity
Constraints
• The PRIMARY KEY clause specifies one or more attributes that make up the
primary key of a relation.
• If a primary key has a single attribute, the clause can follow the attribute
directly.
• For example, the primary key of DEPARTMENT can be specified as follows
Dnumber INT PRIMARY KEY,
• The UNIQUE clause specifies alternate (unique) keys, also known as candidate
keys as illustrated in the DEPARTMENT and PROJECT table declarations.
• The UNIQUE clause can also be specified directly for a unique key if it is a
single attribute, as in the following example:
Dname VARCHAR(15) 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 updated.
• 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.
• The schema designer can specify an alternative action to be taken by attaching a
referential triggered action clause to any foreign key constraint.
• The options include SET NULL, CASCADE, and SET DEFAULT. An option must be qualified
with either ON DELETE or ON UPDATE.
• The database designer chooses ON DELETE SET NULL and ON UPDATE CASCADE for the
foreign key Super_ssn of EMPLOYEE.
• This means that if the tuple for a supervising employee is deleted, the value of Super_ssn
is automatically set to NULL for all employee tuples that were referencing the deleted
employee tuple. On the other hand, if the Ssn value for a supervising employee is
updated, the new value is cascaded to Super_ssn for all employee tuples referencing the
updated employee tuple.
6.2.3 Giving Names to Constraints

• A constraint may be given a constraint name, following the keyword


CONSTRAINT.
• The names of all constraints within a particular schema must be
unique.
• A constraint name is used to identify a particular constraint in case the
constraint must be dropped later and replaced with another
constraint.
6.2.4 Specifying Constraints on Tuples Using CHECK

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


row individually and are checked whenever a row is inserted or
modified.
• CHECK clauses at the end of a CREATE TABLE statement.
CHECK (Dept_create_date <= Mgr_start_date);
6.3 Basic Retrieval Queries in SQL
6.3.1 The SELECT-FROM-WHERE Structure of Basic SQL Queries
• The basic form of the SELECT statement
• In SQL, the basic logical comparison operators for comparing attribute
values with one another and with literal constants are =, <, <=, >, >=,
and <>. These correspond to the relational algebra operators
respectively
• Q0 Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’.

• The SELECT clause of SQL specifies the attributes whose values are to be retrieved, which
are called the projection attributes in relational algebra.
• The WHERE clause specifies the Boolean condition that must be true for any retrieved tuple,
which is known as the selection condition in relational algebra.
• Q1 Retrieve the name and address of all employees who work for the
‘Research’ department.

Dname = ‘Research’ is a selection condition


Dnumber = Dno is a join condition
• Q2 For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last
name, address, and birth date.
6.3.2 Ambiguous Attribute Names, Aliasing, Renaming,
and Tuple Variables
When a multitable query refers to two or more attributes with the same name, prefix the relation name to the
attribute name and separating the two by a period.
aliases or tuple variables
• 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.
• An alias can follow the keyword AS
• Whenever one or more aliases are given to a relation, we can use
these names to represent different references to that same relation.
This permits multiple references to the same relation within a query.
• It is also possible to rename the relation attributes within the query
in SQL by giving them aliases. For example
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.
6.3.3 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.
• 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.
• The * can also be prefixed by the relation name or alias; for example,
EMPLOYEE.* refers to all attributes of the EMPLOYEE table.
6.3.4 Tables as Sets in SQL
• SQL does not automatically eliminate duplicate tuples in the results of
queries
• If we 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.
• A query with SELECT ALL does not eliminate duplicate tuples from the
result of an SQL query.
set union (UNION), set difference
(EXCEPT), and set intersection
(INTERSECT) operations.
Query 4. Make a list of all project numbers for projects that involve an
employee whose last name is ‘Smith’, either as a worker or as a
manager of the department that controls the project.
• SQL also has corresponding multiset operations, which are followed
by the keyword ALL (UNION ALL, EXCEPT ALL, INTERSECT ALL).
• Their results are multisets (duplicates are not eliminated).
6.3.5 Substring Pattern Matching and Arithmetic Operators
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
- underscore (_) replaces a single character.
Query 12. Retrieve all employees whose address is in Houston, Texas.
• Query 12A. Find all employees who were born during the 1950s.
Use of arithmetic in queries
• The standard arithmetic operators for addition (+), subtraction (−),
multiplication (*), and division (/) can be applied to numeric values or
attributes with numeric domains.
• Query 13. Show the resulting salaries if every employee working on
the ‘ProductX’ project is given a 10% raise.

(0.1*E.Salary)+E.Salary
• Comparison operator BETWEEN
Query 14. Retrieve all employees in department 5 whose salary is
between $30,000 and $40,000.

Equivalent to the condition ((Salary >= 30000) AND (Salary <= 40000))
6.3.6 Ordering of Query Results
ORDER BY clause 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
• Query 15. Retrieve a list of employees and the projects they are working on,
ordered by department and, within each department, ordered alphabetically by
last name, then first name.

• 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.
ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC
Summary of Basic SQL Retrieval Queries
• A simple retrieval query in SQL can consist of up to four clauses, but
only the first two—SELECT and FROM—are mandatory.
• The clauses are specified in the following order, with the clauses
between square brackets [ … ] being optional:

• Two additional clauses are GROUP BY and HAVING


6.4 INSERT, DELETE, and UPDATE
Statements in SQL
• 6.4.1 The INSERT Command
• INSERT is used to add a single tuple (row) to a relation (table).
• 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.
• A second form of the INSERT statement allows the user to specify
explicit attribute names that correspond to the values provided in the
INSERT command.

• Attributes not specified in U1A are set to their DEFAULT or to NULL,


and the values are listed in the same order as the attributes are listed
in the INSERT command itself.
• A DBMS that fully implements SQL should support and enforce all the
integrity constraints that can be specified in the DDL.
• A variation of the INSERT command inserts multiple tuples into a
relation in conjunction with creating the relation and loading it with
the result of a query.
• Another variation for loading data is to create a new table TNEW that
has the same attributes as an existing table T, and load some of the
data currently in T into TNEW.

• The clause WITH DATA specifies that the table will be created and
loaded with the data specified in the query.
• 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.
• The deletion may propagate to tuples in other relations if referential triggered
actions are specified in the referential integrity constraints of the DDL.
• Depending on the number of tuples selected by the condition in the WHERE
clause, zero, one, or several tuples can be deleted by a single DELETE command.
• A missing WHERE clause specifies that all tuples in the relation are to be
deleted; however, the table remains in the database as an empty table.
• We must use the DROP TABLE command to remove the table definition
6.4.3 The UPDATE Command
• The UPDATE command is used to modify attribute values of one or
more selected tuples.
• WHERE clause in the UPDATE command selects the tuples to be
modified from a single relation.
• 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.
• An additional SET clause in the UPDATE command specifies the
attributes to be modified and their new values.
• Several tuples can be modified with a single UPDATE command.

You might also like