0% found this document useful (0 votes)
27 views9 pages

DBMS Module3

The document outlines the steps for creating relations in SQL for multi-valued attributes and n-ary relationships, detailing the use of primary and foreign keys. It covers SQL data definition, data types, and constraints, including how to specify key and referential integrity constraints. Additionally, it provides examples of basic SQL retrieval queries and the use of INSERT, DELETE, and UPDATE statements.

Uploaded by

honallivarun
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)
27 views9 pages

DBMS Module3

The document outlines the steps for creating relations in SQL for multi-valued attributes and n-ary relationships, detailing the use of primary and foreign keys. It covers SQL data definition, data types, and constraints, including how to specify key and referential integrity constraints. Additionally, it provides examples of basic SQL retrieval queries and the use of INSERT, DELETE, and UPDATE statements.

Uploaded by

honallivarun
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/ 9

Step 6: For each multi-valued attribute A, create a new relation R.

This relation will include an attribute


corresponding to A, plus the primary key K of the parent relation (entity type or relationship type) as a foreign
key in R. The primary key of R is the combination of A and K.

Step 7: For each n-ary relationship type R, where n>2, create a new relation S to represent R. Include the
primary keys of the relations participating in R as foreign keys in S. Simple attributes of R map to attributes of S.
The primary key of S is a combination of all the foreign keys that reference the participants that have cardinality
constraint > 1. For a recursive relationship, we will need a new relation.

SQL

2.8 SQL Data Definition and Data Types


 SQL uses the terms table, row, and column for the formal relational model terms relation, tuple, and
attribute, respectively.
 SQL command for data definition is the CREATE statement, which can be used to create schemas, tables
(relations), types, and domains, as well as other constructs such as views, assertions, and triggers.
 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.
 A schema is created via the CREATE SCHEMA statement, which can include all the schema elements’
definitions.
Eg: CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
 In SQLcatalog—a named collection of schemas.

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.
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 25
 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.
 Eg: CREATE TABLE EMPLOYEE

Attribute Data Types and Domains in SQL


 The basic data types available for attributes include numeric, character string, bit string, Boolean,
date, and time.
Numeric data types
 Integer numbers of various sizes like INTEGER or INT, and SMALLINT

Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 26


 Floating-point (real) numbers of various precision like FLOAT or REAL, and DOUBLE PRECISION
 Formatted numbers 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 datatypes
 Fixed length—CHAR(n) or CHARACTER(n), where n is the number of character
 Varying length— VARCHAR(n) or CHAR VARYING(n) or CHARACTER VARYING(n), where n is the
maximum number of characters.
Bit-string data types
 Fixed length n—BIT(n)—or varying length— BIT VARYING(n), where n is the maximum number of bits.
 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.
Boolean data types
 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.
DATE data types
 Date 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.
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.
Interval
 Another data type related to DATE, TIME, and TIMESTAMP is the 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.

2.9 Specifying Constraints in SQL


These include key and referential integrity constraints, restrictions on attribute domains and NULLs, and
constraints on individual tuples within a relation using the CHECK clause.
Specifying Attribute Constraints and Attribute Defaults
 Because SQL allows NULLs as attribute values, a constraint NOT NULL may be specified if NULL
is not permitted for a particular attribute which 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 to an
attribute definition.
 The default value is included in any new tuple if an explicit value is not provided for that attribute.
 Figure illustrates an example of specifying a default manager for a new department and a default
department for a new employee.
 If no default clause is specified, the default 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 table to the
following:
Dnumber INT NOT NULL CHECK (Dnumber> 0 AND Dnumber< 21);

Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 27


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.
 Ex: 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
 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,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.
 In general, the action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON
DELETE and ON UPDATE: The value of the affected referencing attributes is changed to NULL for
SET NULL and to the specified default value of the Key and referential integrity constraints were not
included in early versions of SQL.

Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 28


 The action for CASCADE ON DELETE is to delete all the referencing tuples, whereas the action for
CASCADE ON UPDATE is to change the value of the referencing foreign key attribute(s) to the updated
(new) primary key value for all the referencing tuples.

Giving Names to Constraints


 Figure illustrates how 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.

Specifying Constraints on Tuples Using CHECK


 other table constraints can be specified through additional CHECK clauses at the end of a CREATE
TABLE statement.
 These can be called row-based constraints because they apply to each row individually and are checked
whenever a row is inserted or modified.
 For example, suppose that the DEPARTMENT table in Figure had an additional attribute
Dept_create_date, which stores the date when the department was created.
 In CREATE TABLE statement for the DEPARTMENT table to make sure that a manager’s start date is
later than the department creation date.
CHECK (Dept_create_date<= Mgr_start_date);

2.10 Basic Retrieval Queries in SQL


 The basic form of the SELECT statement, sometimes called a mapping or a select-from-where block, is
formed of the three clauses SELECT, FROM, and WHERE and has the following form:
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, the basic logical comparison operators are =, <=, >, >=, and <>

Query 0

Retrieve the birthdate 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’;

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;
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 29
Query 1A
Ambiguous attribute names
SELECT FNAME, EMPLOYEE.NAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.NAME = ‘Research’ AND
DEPARTMENT.DNUMBER = EMPLOYEE.DNUMBER;

Query 1B
Aliasing
SELECT E.FNAME, E.NAME, E.ADDRESS
FROM EMPLOYEE E, DEPARTMENT D
WHERE D.NAME = ‘Research’ AND D.DNUMBER = E.DNUMBER;

Query 1C
Retrieve all the attribute values of EMPLOYEE tuples who work in department number 5
SELECT *
FROM EMPLOYEE
WHERE DNO = 5;

Query 1D
Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works in for
every employee of the ‘Research’ department
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME = ‘Research’ AND DNO = DNUMBER;

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, ADDRESS, BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM = DNUMBER AND MGRSSN = SSN AND PLOCATION = ‘Stafford’;

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.SUPERSSN = S.SSN;

Query 8A
Reformulation of query 8 to retrieve the last name of each employee and his or her supervisor, while
renaming the resulting attribute names as EMPLOYEE_NAME and SUPERVISOR_NAME
SELECT E.LNAME AS EMPLOYEE_NAME, S.LNAME AS SUPERVISOR_NAME
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.SUPERSSN = S.SSN;
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 30
Query 9
Select all EMPLOYEE SSNs in the database
SELECT SSN
FROM EMPLOYEE;

Query 10
Select all combination of EMPLOYEE SSN and DEPARTMENT DNAME in the database
SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT;

Query 10A
Select the CROSS PRODUCT of the EMPLOYEE and DEPARTMENT relations
SELECT *
FROM EMPLOYEE, DEPARTMENT;
Query 11
Retrieve the salary of every employee
SELECT ALL SALARY
FROM EMPLOYEE;

Query 11A
Retrieve all distinct salary values
SELECT DISTINCT SALARY
FROM EMPLOYEE;

Query 12
Retrieve all employees whose address is in Houston, Texas
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE ‘%Houston,TX%’;

Query 12A
Find all employees who were born during the 1950s
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE BDATE LIKE ‘_ _ _ _ _ _ _ 70’;

Query 13
Show the resulting salaries if every employee working on the ‘ProductX’ project is given a 10 percent raise
SELECT FNAME, LNAME, 1.1*SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN = ESSN AND PNO = PNUMBER AND PNAME = ‘ProductX’;

Query 14
Retrieve all employees in department 5 whose salary is between £30,000 and £40,000
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 31
SELECT *
FROM EMPLOYEE
WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO = 5;

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, 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 DESC, LNAME ASC, FNAME ASC;

2.11 INSERT, DELETE, and UPDATE Statements in SQL


The INSERT Command
 INSERT is used to add a single tuple (row) to a relation (table).
 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.
 INSERT INTO EMPLOYEE VALUES ( ‘Richard’, ‘K’, ‘Marini’, ‘653298653’, ‘1962-12-30’, ‘98 Oak
Forest, Katy, TX’, ‘M’, 37000, ‘653298653’, 4 );
 For example, to enter a tuple for a new EMPLOYEE for whom we know only the Fname, Lname, Dno,
and Ssn attributes,
INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn) VALUES (‘Richard’, ‘Marini’, 4,
‘653298653’);
The DELETE Command
 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.
 Tuples are explicitly deleted from only one table at a time.
 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.
 DELETE FROM EMPLOYEE WHERE Ssn = ‘123456789’;
 DELETE FROM EMPLOYEE;
The UPDATE Command
 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.
 UPDATE PROJECT,
SET Plocation = ‘Bellaire’, Dnum = 5
WHERE Pnumber = 10;

Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 32


2.12 Additional Features of SQL
 various techniques for specifying complex retrieval queries, including nested queries, aggregate
functions, grouping, joined tables, outer joins, case statements, and recursive queries; SQL views,
triggers, and assertions; and commands for schema modification.
 SQL has various techniques for writing programs in various programming languages that include SQL
statements to access one or more databases. These include embedded (and dynamic) SQL, SQL/CLI
(Call Level Interface) and its predecessor ODBC (Open Data Base Connectivity), and SQL/PSM
(Persistent Stored Modules).
 Each commercial RDBMS will have, in addition to the SQL commands, a set of commands for
specifying physical database design parameters, file structures for relations, and access paths such as
indexes. We called these commands a storage definition language (SDL).
 SQL has transaction control commands. These are used to specify units of database processing for
concurrency control and recovery purposes.
 SQL has language constructs for specifying the granting and revoking of privileges to users. Privileges
typically correspond to the right to use certain SQL commands to access certain relations. Each relation is
assigned an owner, and either the owner or the DBA staff can grant to selected users the privilege to use
an SQL statement—such as SELECT INSERT, DELETE, or UPDATE— to access the relation. In
addition, the DBA staff can grant the privileges to create schemas, tables, or views to certain users. These
SQL commands—called GRANT and REVOKE.
 SQL has language constructs for creating triggers. These are generally referred to as active database
techniques, since they specify actions that are automatically triggered by events such as database updates.
 SQL has incorporated many features from object-oriented models to have more powerful capabilities,
leading to enhanced relational systems known as object-relational. Capabilities such as creating complex-
structured attributes, specifying abstract data types (called UDTs or user-defined types) for attributes and
tables, creating object identifiers for referencing tuples, and specifying operations on types.
 SQL and relational databases can interact with new technologies such as XML and OLAP/data
warehouses.

Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 33

You might also like