1 - Dbms Module 2
1 - Dbms Module 2
com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
SYLLABUS
• Structure of Relational Databases - Integrity Constraints,
Synthesizing ER diagram to relational schema
• Introduction to Relational Algebra - select, project, cartesian
product operations, join - Equi-join, natural join. query
examples,
• Introduction to Structured Query Language (SQL), Data
Definition Language (DDL), Table definitions and
operations – CREATE, DROP, ALTER, INSERT, DELETE,
UPDATE.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Attributes
Relation name
EMPLOYEE
EMP_NO Name Address Mobile number Age Salar
y
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Domain
• It has three parts
▫ Name
▫ Data type
▫ Format
• A Domain is allowed set of atomic values.
• Atomic means each value in the domain is indivisible to the
relational model.
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Tuples (rows)
• A tuple is an ordered set of values
• Tuple is a portion of a table containing data that described
only entity, relationship, or object
• Also known as record
• Each value is derived from an appropriate domain.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Columns
• Columns in a table are also called attributes or fields of
the relation.
• A single cell in a table called field value, attribute value or
data element.
• For example, for the entity person, attributes could include
eye colour and height.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Key of a Relation
• Each row has a value of a data item (or set of items) that
uniquely identifies that row in the table
Called the key
• Sometimes row-ids or sequential numbers are assigned as keys
to identify the rows in a table
Called artificial key or surrogate key
PREPARED BY SHARIKA T R,
SNGCE
Relations (Tables)
• A table of values
• A relation may be thought of as a set of rows.
• A relation may alternately be thought of as a set of
columns.
• That is a table is perceived as a two-dimensional structure
composed of rows and columns.
• Each row represents a fact that corresponds to a real-world
entity or relationship.
• Each row has a value of an item or set of items that uniquely
identifies that row in the table
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Schema of a Relation
• It is basically an outline of how data is organized
• It is denoted by R (A1, A2, .....An)
▫ Here R is relation name and
▫ it has some attributes A1 to An
• Each attribute have some domain and it is represented by
dom(Ai)
• Relation schema is used to describe a relation and R is name
of the relation
• Each attribute has a domain or a set of valid values.
▫ For example, the domain of Cust-id is 6 digit numbers.
PREPARED BY SHARIKA T R,
SNGCE
Degree of a relation
• Degree of a relation is number of attributes in a relation
• Eg
▫ STUDENT(Id, Name, Age, Departmentno)
Has degree 4
• Using datatype of each the definition can be written as
▫ STUDENT(Id:Integer, Name:String,Age:integer,
Departmentno:integer)
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Relation State
• The relation state is a subset of the Cartesian product of
the domains of its attributes
▫ each domain contains the set of all possible values the attribute
can take.
• Example: attribute Cust-name is defined over the domain of
character strings of maximum length 25
▫ dom(Cust-name) is varchar(25)
• The role these strings play in the CUSTOMER relation is
that of the name of a customer.
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Cardinality in domain D
• Cardinality in domain D by |D| then the total number of
tuples in cartesiean product is
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Alternative Definition of a
Relation
• ordering of values in a tuple is unnecessary
• a relation schema R = {A1, A2, ..., An} is a set of attributes, and
• a relation state r(R) is a finite set of mappings r = {t1, t2, ...,
tm}, where each tuple ti is a mapping from R to D, and
• D is the union (denoted by ∪) of the attribute domains; that is,
D = dom(A1) ∪ dom(A2) ∪ ... ∪ dom(An).
• Here t[Ai] must be in dom(Ai) for 1 ≤ i ≤ n for each mapping t in
r.
• Each mapping ti is called a tuple.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Definition Summary
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Characteristics of Relation
• Ordering of tuples in a relation r(R):
▫ The tuples are not considered to be ordered, even though
they appear to be in the tabular form.
• Ordering of attributes in a relation schema R (and of values
within each tuple):
▫ We will consider the attributes in R(A1, A2, ..., An) and the
values in t=<v1, v2, ..., vn> to be ordered .
(However, a more general alternative definition of relation does
not require this ordering).
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
• Values in a tuple:
▫ All values are considered atomic (indivisible).
▫ Each value in a tuple must be from the domain of the attribute
for that column
If tuple t = <v1, v2, …, vn> is a tuple (row) in the relation state r of
R(A1, A2, …, An)
Then each vi must be a value from dom(Ai)
PREPARED BY SHARIKA T R,
SNGCE
• Notation:
▫ We refer to component values of a tuple t by:
t[Ai] or t.Ai
This is the value vi of attribute Ai for tuple t
▫ Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t
containing the values of attributes Au, Av, ..., Aw, respectively
in t
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Key Constraints
• Superkey of R:
▫ Is a set of attributes SK of R with the following condition:
No two tuples in any valid relation state r(R) will have
the same value for SK
That is, for any distinct tuples t1 and t2 in r(R), t1[SK]
t2[SK]
This condition must hold in any valid state r(R)
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
• Key of R:
▫ A "minimal" superkey
▫ That is, a key is a superkey K such that removal of any
attribute from K results in a set of attributes that is not a
superkey (does not possess the superkey uniqueness
property)
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
• In general:
▫ Any key is a superkey (but not vice versa)
▫ Any set of attributes that includes a key is a superkey
▫ A minimal superkey is also a key
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Entity Integrity
• Entity Integrity:
▫ The primary key attributes PK of each relation schema R in S
cannot have null values in any tuple of r(R).
This is because primary key values are used to identify the
individual tuples.
t[PK] null for any tuple t in r(R)
If PK has several attributes, null is not allowed in any of these
attributes
▫ Note: Other attributes of R may be constrained to disallow null
values, even though they are not members of the primary key.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Referential Integrity
• A constraint involving two relations
▫ The previous constraints involve a single relation.
• Used to specify a relationship among tuples in two
relations:
▫ The referencing relation and the referenced relation.
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
constraints
• Each relation schema can be displayed as a row of attribute
names
• The name of the relation is written above the attribute
names
• The primary key attribute (or attributes) will be underlined
• A foreign key (referential integrity) constraints is displayed
as a directed arc (arrow) from the foreign key attributes to
the referenced table
▫ Can also point the the primary key of the referenced relation for
clarity
• Next slide shows the COMPANY relational schema
diagram
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
homework
• Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course:
• STUDENT(SSN, Name, Major, Bdate)
• COURSE(Course#, Cname, Dept)
• ENROLL(SSN, Course#, Quarter, Grade)
• BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
• TEXT(Book_ISBN, Book_Title, Publisher, Author)
• Draw a relational schema diagram specifying the foreign keys for this
schema.
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Multivalued Attributes
• If you have a multi-valued attribute, take the attribute and turn it into a new entity or
table of its own.
• Then make a 1: N relationship between the new entity and the existing one.
• Create a table for the attribute.
▫ Add the primary (id) column of the parent entity as a foreign key within the new table
▫ PERSONS( personid , name, dateOfBirth, gender)
▫ PERSONS_PHONE(personid ,phone)
▫ personid within the table Phones is a foreign key referring to the personid of Persons
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
• For cases when the Person is not married i.e. has no wifeID, the attribute can set to
NULL
PERSONS( personid , name, dateOfBirth, gender,wifeid)
PERSONS_PHONE(personid ,phone)
WIFE(wifeid , name )
PREPARED BY SHARIKA T R,
SNGCE
PERSONS_PHONE(personid ,phone)
HOUSE(houseid, personid,
number, address)
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
M:N Relationships
PREPARED BY SHARIKA T R,
SNGCE
Weak Entity
PREPARED BY SHARIKA T R,
SNGCE
Homework
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Relational algebra
• Relational algebra is a procedural query language, which
takes instances of relations as input and yields instances of
relations as output.
• It uses operators to perform queries. An operator can be
either unary or binary.
• They accept relations as their input and yield relations as
their output.
• Relational algebra is performed recursively on a relation
and intermediate results are also considered relations.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PROJECT Operation
• PROJECT operation selects certain columns from the
table and discards the other columns.
• If we are interested in only certain attributes of a relation,
we use the PROJECT operation to project the relation over
these attributes only
• vertical partition of the relation into two relations
▫ one has the needed columns (attributes) and contains the
result of the operation, and
▫ the other contains the discarded columns.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
• Example: list each employee’s first and last name and salary
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Example
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Rename
• The results of the operations that we are performing are saved
without any name.
• If we want to give some name to the result of any operation then we
can rename the result of the operations using the rename
operation.
• It can also be used to rename an old relation. It is denoted by
symbol rho (ρ).
• Notation 1: Naming the result of an operation ρh(E)
▫ where the result of expression E is stored as h.
• Notation 2: Renaming an already existing column
ρNew_Name/Old_Name(E)
PREPARED BY SHARIKA T R,
SNGCE
Rename
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
PREPARED BY SHARIKA T R,
MINUS Operations
• UNION:
▫ The result of this operation, denoted by R ∪ S, is a relation that
includes all tuples that are either in R or in S or in both R and S.
▫ Duplicate tuples are eliminated.
• INTERSECTION:
▫ The result of this operation, denoted by R ∩ S, is a relation that
includes all tuples that are in both R and S.
• SET DIFFERENCE (or MINUS):
▫ The result of this operation, denoted by R – S, is a relation that
includes all tuples that are in R but not in S.
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Example
• retrieve the Social Security numbers of all employees who
either work in department 5 or directly supervise an employee
who works in department 5, we can use the UNION operation
as follows
PREPARED BY SHARIKA T R,
SNGCE
DEP5_EMPS
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Example
• We want to retrieve a list of names of each female
employee’s dependents
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Example
• Retrieve the name of the manager of each department.
▫ To get the manager’s name, we need to combine each
department tuple with the employee tuple whose Ssn value
matches the Mgr_ssn value in the department tuple.
▫ We do this by using the JOIN operation and then projecting the
result over the necessary attributes, as follows
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
RESULT
Dname Lname Fname
Research Wong Franklin
Administration Wallance Jennifer
Headquaters Borg James
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
THETA JOIN
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
EQUIJOIN
• a JOIN, where the only comparison operator used is =, is
called an EQUIJOIN
• in the result of an EQUIJOIN we always have one or more
pairs of attributes that have identical values in every tuple
PREPARED BY SHARIKA T R,
SNGCE
NATURAL JOIN
• NATURAL JOIN requires that the two join attributes (or
each pair of join attributes) have the same name in both
relations.
• If this is not the case, a renaming operation is applied first.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Example
• combine each PROJECT tuple with the DEPARTMENT tuple
that controls the project
▫ first we rename the Dnumber attribute of DEPARTMENT to Dnum
▫ so that it has the same name as the Dnum attribute in PROJECT—
and then we apply NATURAL JOIN
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Square Cube
PREPARED BY SHARIKA T R,
SNGCE
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Homework
1. Retrieve the name and address of all employees who work
for the ‘Research’ department.
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
SYLLABUS
• Structure of Relational Databases - Integrity Constraints,
Synthesizing ER diagram to relational schema
• Introduction to Relational Algebra - select, project, cartesian
product operations, join - Equi-join, natural join. query
examples,
• Introduction to Structured Query Language (SQL), Data
Definition Language (DDL), Table definitions and
operations – CREATE, DROP, ALTER, INSERT, DELETE,
UPDATE.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
SQL HISTORY
• Structured Query Language(SQL) was developed at IBM by
Donald D. Chamberlin and Raymond F. Boyce in the early
1970s.
• This was initially called SEQUEL(Structured English QUEry
Language).
• The main objective of SQL is to update, store, manipulate
and retrieve data stored in a relational database.
PREPARED BY SHARIKA T R,
SNGCE
SQL
• SQL provides
▫ A data definition language (DDL)
▫ A data manipulation language (DML)
▫ A data control language (DCL)
• In addition SQL
▫ Can be used from other languages
▫ Is often extended to provide common programming
constructs (such as if-then tests, loops, variables, etc.)
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Cont..
• SQL is a declarative (non-procedural) language
▫ Procedural - say exactly what the computer has to do
▫ Non-procedural – describe the required result (not the
way to compute it)
• SQL is based on the relational model
▫ It has many of the same ideas
▫ Databases that support SQL are often described as
relational databases
▫ It is not always true to the model
PREPARED BY SHARIKA T R,
SNGCE
Cont..
• E/R designs can be implemented in SQL
▫ Entities, attributes, and relationships can all be expressed
in terms of SQL
▫ Many-to-many relationships are a problem, so should be
removed
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
SQL environment
• SQL environment is basically an installation of an SQL-
compliant RDBMS on a computer system
• SQL uses the concept of a catalog a named collection of
schemas in an SQL environment.
• 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
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
Cont..
• 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 domain definitions.
PREPARED BY SHARIKA T R,
SNGCE
DATABASE LANGUAGES
1. Data Definition Language (DDL):
▫ It is used to specify a database conceptual schema using set of definitions.
▫ It supports the definition or declaration of database objects.
▫ The more common DDL commands are
a.CREATE TABLE:
ALTER TABLE
DROP TABLE
TRUNCATE
COMMENT
RENAME
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
CREATE TABLE
• Specifies a new base relation by giving it a name, and specifying
each of its attributes and their data types (INTEGER, FLOAT,
DECIMAL(i,j), CHAR(n), VARCHAR(n))
• A constraint NOT NULL may be specified on an attribute
CREATE TABLE DEPARTMENT (
DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
• In SQL2, can use the CREATE TABLE command for specifying the
primary key attributes, secondary keys, and referential integrity
constraints (foreign keys).
• Key attributes can be specified via the PRIMARY KEY and UNIQUE
phrases
CREATE TABLE DEPT (
DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP );
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
• UNIQUE
▫ specifies alternate (secondary) keys
▫ can also be specified directly for a secondary key if the
secondary key is a single attribute
PREPARED BY SHARIKA T R,
SNGCE
• FOREIGN KEY
▫ 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
▫ 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
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
• ON DELETE SET NULL and ON UPDATE CASCADE for the foreign key
Super_ssn of EMPLOYEE means
• 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
• 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
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
DROP TABLE
• The SQL DROP TABLE statement is used to remove a table
definition and all the data, indexes, triggers, constraints and
permission specifications for that table.
• You should be very careful while using this command
because once a table is deleted then all the information
available in that table will also be lost forever.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
ALTER TABLE
• The SQL ALTER TABLE command is used to add, delete or modify
columns in an existing table.
• You should also use the ALTER TABLE command to add and drop
various constraints on an existing table.
• The basic syntax of an ALTER TABLE command to add a New Column
in an existing table is as follows.
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
INSERT Command
• INSERT is used to add a single tuple to a relation.
• 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.
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Homework
Visit my YouTube Page: https://www.youtube.com/c/sharikatr
PREPARED BY SHARIKA T R,
SNGCE
MODULE 2 ENDS