0% found this document useful (0 votes)
13 views

Module 2 Part3

The document discusses the SQL database language including its structure, components like DDL, DML and DCL. It explains key SQL concepts such as schemas, tables, rows, columns and constraints. It also provides examples of SQL commands like CREATE TABLE, DROP TABLE, ALTER TABLE, INSERT, DELETE.

Uploaded by

gautham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module 2 Part3

The document discusses the SQL database language including its structure, components like DDL, DML and DCL. It explains key SQL concepts such as schemas, tables, rows, columns and constraints. It also provides examples of SQL commands like CREATE TABLE, DROP TABLE, ALTER TABLE, INSERT, DELETE.

Uploaded by

gautham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

MODULE 2: SQL

PART 3
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.
SQL
• SQL stands for Structured Query Language.

• Earlier it was called SEQUEL – Structured


English QUEry Language.

• It uses the terms tables, rows & columns for the


formal relational model terms relations, tuples &
attributes resp.

• It has statements for data definitions, queries &


updates.

• Each statement in SQL ends with a semicolon(;)


• SQL provides
▫ A data definition language (DDL)
▫ A data manipulation language (DML)
▫ A data control language (DCL)
• 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
Schema and Catalog Concepts in SQL
• An SQL schema is a collection of database objects.

• It is identified by a schema name, and includes an


authorization identifier to indicate the user or
account who owns the schema.
• Schema elements include tables, constraints, views,
domains, and other constructs that describe the
schema.
• A schema is created via the CREATE SCHEMA
statement.
• Creates a schema called COMPANY, owned by
the user with authorization identifier ‘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 – Collection of schemas in an SQL
environment.
• Catalog contains a special schema
called INFORMATION_SCHEMA,
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 most commonly used DDL commands are
CREATE TABLE
ALTER TABLE
DROP TABLE
TRUNCATE
COMMENT
RENAME
2. Data Manipulation Language (DML)
• It provides a set of operations to support the basic data
manipulation operations on the data held in the database.
• It is used to query, update or retrieve data stored in a
database.
• Some of the tasks that come under DML are
SELECT
Used to query and display data from a database.
INSERT
Adds new rows to a table.
UPDATE
Changes an existing value in a column or group of columns in a
table.
DELETE:
Removes a specified row or set of rows from a table.
MERGE.
CREATE TABLE

• 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
CREATE TABLE DEPARTMENT (
DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9) NOT NULL,
MGRSTARTDATE DATE,
PRIMARY KEY(DNUMBER),
FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE(SSN));
Data Types and Domains in SQL

• The basic data types available for attributes


include:
▫ numeric
▫ character string
▫ bit string
▫ Boolean
▫ Date & Time
▫ timestamp
NUMERIC
• INTEGER/INT, SMALLINT, FLOAT/REAL, etc.

CHARACTER STRING
• CHAR(n)/CHARACTER(n), VARCHAR(n)/
CHARACTER VARYING(n)/ CHAR VARYING(n),
CLOB, etc.

BIT STRING
• BIT(n), BIT VARYING(n), BLOBetc.
BOOLEAN
• 3 values – true, false & unknown.

DATE
• 10 positions: YYYY-MM-DD

TIME
• 8 positions: HH:MM:SS

TIMESTAMP
• Includes both date and time fields.
Specifying Constraints in SQL
• Entity Integrity constraint

• Referential Integrity constraint

• NULL constraint

• DEFAULT constraint

• UNIQUE constraint

• CHECK constraint
Entity Integrity Constraint
• PRIMARY KEY
▫ 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
Dnumber INT PRIMARY KEY;
PRIMARY KEY (DNo, DLocation)
Referential Integrity Constraint
• 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.

FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE(SSN)


NULL Constraint

• 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 can be specified for any other attributes
whose values are required not to be NULL

DNAME INTEGER NOT NULL


DAFAULT Constraint

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


attribute by appending the clause DEFAULT
<value> to an attribute definition.

City varchar(255) DEFAULT 'Sandnes'


UNIQUE Constraint

• Specifies alternate (secondary) keys


• Can also be specified directly for a secondary key if
the secondary key is a single attribute
CHECK Constraint

• Another type of constraint can restrict attribute or


domain values using the CHECK clause following
an attribute or domain definition.
• 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
• CHECK clauses can be specified at the end of a
CREATE TABLE statement.
• The following SQL creates a CHECK constraint
on the "Age" column when the "Persons" table is
created. The CHECK constraint ensures that the
age of a person must be 18, or older:
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
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.
DROP
• We can also drop schemas & constraints.

DROP SCHEMA COMPANY;

• Two drop command options:

• CASCADE – drops everything

• RESTRICT – drops only if it has no elements

DROP TABLE DEPENDENT CASCADE;


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.

ALTER TABLE table_name ADD COLUMN column_name DataType;

ALTER TABLE EMPLOYEE ADD COLUMN JOB VARCHAR(12);


• To DROP COLUMN in an existing table:

ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS


CASCADE;

• The basic syntax of an ALTER TABLE command


to change the DATA TYPE of a column in a table
is as follows.
• The basic syntax of ALTER TABLE to ADD
CONSTRAINT to a table is as follows
• The basic syntax of an ALTER TABLE command
to DROP CONSTRAINT from a table is as
follows.
EXAMPLES:
INSERT Command

• INSERT is used to add tuples 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.
• 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.
• This is useful if a relation has many attributes but only a few
of those attributes are assigned values in the new tuple.
• However, the values must include all attributes with NOT
NULL specification and no default value.
• Attributes with NULL allowed or DEFAULT values are the
ones that can be left out.
• For example, to enter a tuple for a new EMPLOYEE for whom
we know only the Fname, Lname, Dno, and Ssn attributes
• 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.
• It is also possible to insert into a relation
multiple tuples separated by commas in a single
INSERT command.
• The attribute values forming each tuple are
enclosed in parentheses.
INSERT INTO EMPLOYEE(Fname, Ssn, Dno) VALUES
(‘A’,’123’,2),(‘B’,’555’,4);
• 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.
• For example,
▫ to create a temporary table that has the employee last
name, project name, and hours per week for each
employee working on a project
The DELETE Command
• The DELETE command removes rows from a
table.
• It includes a WHERE clause to select the rows to
be deleted.
• Depending on the number of rows selected by
the condition in the WHERE clause, zero, one,
or several rows can be deleted by a single
DELETE command.
• A missing WHERE clause specifies that all rows
in the table 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
The UPDATE Command
• The UPDATE command is used to modify
attribute values of one or more selected rows.
• As in the DELETE command, a WHERE clause
in the UPDATE command selects the tuples to
be modified from a single relation.
PRE
SHARIKA T
• 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.
• It is also possible to specify NULL or DEFAULT
as the new attribute value
• each UPDATE command explicitly refers to a
single relation only.
MODULE 2 ENDS

You might also like