2. What is Data?
Data is nothing but facts and statistics stored or free flowing
over a network, generally it's raw and unprocessed. For
example: When you visit any website, they might store you IP
address, that is data, in return they might add a cookie in your
browser, marking you that you visited the website, that is data,
your name, it's data, your age, it's data.
Data becomes information when it is processed, turning it
into something meaningful. Like, based on the cookie data
saved on user's browser, if a website can analyse that generally
men of age 20-25 visit us more, that is information, derived
from the data collected.
2
Author: Shaveta Khepra
3. What is DBMS?
A DBMS is a software that allows creation, definition and manipulation of
database, allowing users to store, process and analyse data easily. DBMS
provides us with an interface or a tool, to perform various operations like
creating database, storing data in it, updating data, creating tables in the
database and a lot more.
DBMS also provides protection and security to the databases. It also
maintains data consistency in case of multiple users.
Here are some examples of popular DBMS used these days:
MySql
Oracle
SQL Server
IBM DB2
PostgreSQL
Amazon SimpleDB (cloud based) etc.
3
Author: Shaveta Khepra
4. Characteristics of Database Management System
A database management system has following characteristics:
Data stored into Tables: Data is never directly stored into the
database. Data is stored into tables, created inside the database.
DBMS also allows to have relationships between tables which
makes the data more meaningful and connected. You can easily
understand what type of data is stored where by looking at all the
tables created in a database.
Reduced Redundancy: In the modern world hard drives are very
cheap, but earlier when hard drives were too expensive, unnecessary
repetition of data in database was a big problem. But DBMS follows
Normalisation which divides the data in such a way that repetition
is minimum.
Data Consistency: On Live data, i.e. data that is being continuously
updated and added, maintaining the consistency of data can become
a challenge. But DBMS handles it all by itself.
4
Author: Shaveta Khepra
5. Support Multiple user and Concurrent Access: DBMS allows
multiple users to work on it(update, insert, delete data) at the same
time and still manages to maintain the data consistency.
Query Language: DBMS provides users with a simple Query
language, using which data can be easily fetched, inserted, deleted
and updated in a database.
Security: The DBMS also takes care of the security of data,
protecting the data from un-authorised access. In a typical DBMS,
we can create user accounts with different access permissions, using
which we can easily secure our data by restricting user access.
DBMS supports transactions, which allows us to better handle and
manage data integrity in real world applications where multi-
threading is extensively used.
5
Author: Shaveta Khepra
6. Advantages of DBMS
Segregation of application program.
Minimal data duplicacy or data redundancy.
Easy retrieval of data using the Query Language.
Reduced development time and maintenance need.
With Cloud Datacenters, we now have Database Management
Systems capable of storing almost infinite data.
Seamless integration into the application programming languages
which makes it very easier to add a database to almost any
application or website.
6
Author: Shaveta Khepra
7. Disadvantages of DBMS
It's Complexity.
Except MySQL, which is open source, licensed DBMSs
are generally costly.
They are large in size.
7
Author: Shaveta Khepra
8. Users
A typical DBMS has users with different rights and permissions
who use it for different purposes. Some users retrieve data and
some back it up. The users of a DBMS can be broadly
categorized as follows −
8
Author: Shaveta Khepra
9. Administrators − Administrators maintain the DBMS and are
responsible for administrating the database. They are responsible to
look after its usage and by whom it should be used. They create
access profiles for users and apply limitations to maintain isolation
and force security. Administrators also look after DBMS resources
like system license, required tools, and other software and hardware
related maintenance.
Designers − Designers are the group of people who actually work
on the designing part of the database. They keep a close watch on
what data should be kept and in what format. They identify and
design the whole set of entities, relations, constraints, and views.
End Users − End users are those who actually reap the benefits of
having a DBMS. End users can range from simple viewers who pay
attention to the logs or market rates to sophisticated users such as
business analysts.
9
Author: Shaveta Khepra
10. Components of DBMS
The database management system can be divided into five
major components, they are:
Hardware
Software
Data
Procedures
Database Access Language
Let's have a simple diagram to see how they all fit together
to form a database management system.
10
Author: Shaveta Khepra
12. DBMS Components: Hardware
When we say Hardware, we mean computer, hard disks, I/O channels for
data, and any other physical component involved before any data is
successfully stored into the memory.
When we run Oracle or MySQL on our personal computer, then our
computer's Hard Disk, our Keyboard using which we type in all the
commands, our computer's RAM, ROM all become a part of the DBMS
hardware.
DBMS Components: Software
This is the main component, as this is the program which controls everything.
The DBMS software is more like a wrapper around the physical database,
which provides us with an easy-to-use interface to store, access and update
data.
The DBMS software is capable of understanding the Database Access
Language and interpret it into actual database commands to execute them
on the DB.
12
Author: Shaveta Khepra
13. DBMS Components: Data
Data is that resource, for which DBMS was designed. The motive behind the
creation of DBMS was to store and utilise data.
In a typical Database, the user saved Data is present and meta data is stored.
Metadata is data about the data. This is information stored by the DBMS to
better understand the data stored in it.
For example: When I store my Name in a database, the DBMS will store
when the name was stored in the database, what is the size of the name, is
it stored as related data to some other data, or is it independent, all this
information is metadata.
DBMS Components: Procedures
Procedures refer to general instructions to use a database management
system. This includes procedures to setup and install a DBMS, To login
and logout of DBMS software, to manage databases, to take backups,
generating reports etc.
13
Author: Shaveta Khepra
14. DBMS Components: Database Access Language
Database Access Language is a simple language designed to write
commands to access, insert, update and delete data stored in any
database.
A user can write commands in the Database Access Language and submit
it to the DBMS for execution, which is then translated and executed by
the DBMS.
User can create new databases, tables, insert data, fetch stored data,
update data and delete the data using the access language.
14
Author: Shaveta Khepra
15. DBMS Architecture
A Database Management system is not always directly available for
users and applications to access and store data in it. A Database
Management system can be centralised(all the data stored at one
location), decentralised (multiple copies of database at different
locations) or hierarchical, depending upon its architecture.
1-tier DBMS architecture also exist, this is when the database is
directly available to the user for using it to store data. Generally
such a setup is used for local application development, where
programmers communicate directly with the database for quick
response.
Database Architecture is logically of two types:
2-tier DBMS architecture
3-tier DBMS architecture
15
Author: Shaveta Khepra
16. 2-tier DBMS Architecture
2-tier DBMS architecture includes an
Application layer between the user and the
DBMS, which is responsible to communicate
the user's request to the database management
system and then send the response from the
DBMS to the user.
An application interface known as
ODBC(Open Database Connectivity) provides
an API that allow client side program to call
the DBMS. Most DBMS vendors provide
ODBC drivers for their DBMS.
Such an architecture provides the DBMS extra
security as it is not exposed to the End User
directly. Also, security can be improved by
adding security and authentication checks in
the Application layer too.
16
Author: Shaveta Khepra
17. 3-tier DBMS Architecture
3-tier DBMS architecture is the most commonly
used architecture for web applications.
It is an extension of the 2-tier architecture. In the
2-tier architecture, we have an application layer
which can be accessed programatically to perform
various operations on the DBMS. The application
generally understands the Database Access
Language and processes end users requests to the
DBMS.
In 3-tier architecture, an additional Presentation or
GUI Layer is added, which provides a graphical
user interface for the End user to interact with the
DBMS.
For the end user, the GUI layer is the Database
System, and the end user has no idea about the
application layer and the DBMS system
17
Author: Shaveta Khepra
19. Schemas versus Instances
• Database Schema: The description of a database. Includes
descriptions of the database structure and the constraints that
should hold on the database.
• Schema Diagram: A diagrammatic display of (some
aspects of) a database schema.
• Schema Construct: A component of the schema or an
object within the schema, e.g., STUDENT, COURSE.
• Database Instance: The actual data stored in a database at
a particular moment in time. Also called database state (or
occurrence).
19
Author: Shaveta Khepra
20. Database Schema Vs. Database State
• Database State: Refers to the content of a database at a
moment in time.
• Initial Database State: Refers to the database when it
is loaded
• Valid State: A state that satisfies the structure and
constraints of the database.
• Distinction
• The database schema changes very infrequently. The
database state changes every time the database is updated.
• Schema is also called intension, whereas state is called
extension.
20
Author: Shaveta Khepra
21. Example of a Database Schema
21
Author: Shaveta Khepra
22. Example of a database state
22
Author: Shaveta Khepra
24. Objectives of Three-Schema Architecture
1. DBA should be able to change database storage structures without
affecting the user’s views.
2. Logical structure of database should be unaffected by changes to
physical aspects of storage.
3. DBA should be able to change conceptual structure of database
without affecting all users.
4. Proposed to support DBMS characteristics of:
Program-data independence.
Support of multiple views of the data.
5. All users should be able to access same data.
6. Users should not need to know physical storage details.
24
Author: Shaveta Khepra
26. Three-Schema Architecture(Cont..)
Defines DBMS schemas at three levels:
Internal schema at the internal level to describe physical
storage structures and access paths (e.g. indexes).
Typically uses a physical data model.
Conceptual schema at the conceptual level to describe the
structure and constraints for the whole database for a community
of users.
Uses a conceptual or an implementation data model.
External schemas at the external level to describe the various
user views.
Usually uses the same data model as the conceptual schema.
26
Author: Shaveta Khepra
27. Data Independence
The ability to modify a scheme definition in one level without affecting a
scheme definition in a higher level is called data independence.
Logical Data Independence:
The capacity to change the conceptual schema without having to
change the external schemas and their associated application
programs.
Physical Data Independence:
The capacity to change the internal (physical) schema without having
to change the conceptual schema.
For example, the internal schema may be changed when certain file
structures are reorganized or new indexes are created to improve
database performance
27
Author: Shaveta Khepra
28. Data Independence (Cont..)
When a schema at a lower level is changed, only the mappings
between this schema and higher-level schemas need to be
changed in a DBMS that fully supports data independence.
The higher-level schemas themselves are unchanged.
Hence, the application programs need not be changed since
they refer to the external schemas.
28
Author: Shaveta Khepra
29. Mappings
Mappings translate information from one level to the next
External/Conceptual
Conceptual/Internal
These mappings provide data independence
29
Author: Shaveta Khepra
30. DBMS Database Models
A Database model defines the logical design and structure
of a database and defines how data will be stored,
accessed and updated in a database management system.
While the Relational Model is the most widely used
database model, there are other models too:
Hierarchical Model
Network Model
Entity-relationship Model
Relational Model
30
Author: Shaveta Khepra
31. Hierarchical Model
This database model organises data into a tree-like-structure,
with a single root, to which all the other data is linked. The
hierarchy starts from the Root data, and expands like a tree,
adding child nodes to the parent nodes.
In this model, a child node will only have a single parent node.
This model efficiently describes many real-world relationships
like index of a book, recipes etc.
In hierarchical model, data is organised into tree-like structure
with one one-to-many relationship between two different types
of data, for example, one department can have many courses,
many professors and of-course many students.
31
Author: Shaveta Khepra
33. Hierarchical Model (Cont..)
Advantages:
Simple to construct and operate
Corresponds to a number of natural hierarchically organized
domains, e.g., organization (“org”) chart
Language is simple:
Uses constructs like GET, GET UNIQUE, GET NEXT, GET NEXT
WITHIN PARENT, etc.
Disadvantages:
Navigational and procedural nature of processing
Database is visualized as a linear arrangement of records
Little scope for "query optimization"
Author: Shaveta Khepra
33
34. Network Model
This is an extension of the Hierarchical model. In this model
data is organised more like a graph, and are allowed to
have more than one parent node.
In this database model data is more related as more
relationships are established in this database model. Also,
as the data is more related, hence accessing the data is
also easier and fast. This database model was used to map
many-to-many data relationships.
This was the most widely used database model, before
Relational Model was introduced.
34
Author: Shaveta Khepra
36. Network Model (Advantages)
Network Model is able to model complex relationships.
Can handle most situations for modeling using record types and
relationship types.
Language is navigational; uses constructs like FIND, FIND member,
FIND owner, FIND NEXT within set, etc.
Programmers can do optimal navigation through the database.
Author: Shaveta Khepra
36
37. Network Model (Disadvantages)
Navigational and procedural nature of processing
Database contains a complex array of pointers that thread through a
set of records.
Little scope for automated “query optimization
Author: Shaveta Khepra
37
38. Relational Model
In this model, data is organised in two-dimensional tables and
the relationship is maintained by storing a common field.
This model was introduced by E.F Codd in 1970, and since
then it has been the most widely used database model, infact,
we can say the only database model used around the world.
The basic structure of data in the relational model is tables. All
the information related to a particular type is stored in rows of
that table.
Hence, tables are also known as relations in relational model.
38
Author: Shaveta Khepra
40. Object-oriented Data Models
In OO model, relationships are represented by reference via
object identifier(OID). OID here is a internal system identifier
rather than user defined attribute.
Object-Relational Models:
The object-relational data model combines features of the
object-oriented data model and relational data model.
Introduced in the latest versions of Oracle-10i, DB2, and SQL
Server and other DBMSs.
Author: Shaveta Khepra
40
42. E-R Model
It was developed to facilitate database design.
The E-R data model employs three basic notions:
Entity Sets
Relationships
Attributes
Author: Shaveta Khepra
42
43. Entity and Entity Sets
An entity is an object that exists and is distinguishable from other
objects.
Example: specific person, company, event, plant
Entities have attributes
Example: people have names and addresses
An entity set is a set of entities of the same type that share the
same properties.
Example: set of all persons, companies, trees, holidays
Author: Shaveta Khepra
43
45. ATTRIBUTES
An entity is represented by a set of attributes, that is descriptive
properties possessed by all members of an entity set.
Domain – the set of permitted values for each attribute
Author: Shaveta Khepra
Example:
customer = (customer_id, customer_name,
customer_street, customer_city )
loan = (loan_number, amount )
45
46. Types of Attributes
Simple and composite attributes.
Single-valued and multi-valued attributes
Example: multivalued attribute: phone_numbers
Derived attributes
Can be computed from other attributes
Example: age, given date_of_birth
Author: Shaveta Khepra
46
48. Relationship Sets
A relationship is an association among several entities
Example:
Hayes depositor A-102
customer entity relationship set account entity
A relationship set is a mathematical relation among n 2 entities,
each taken from entity sets
{(e1, e2, … en) | e1 E1, e2 E2, …, en En}
where (e1, e2, …, en) is a relationship
Example:
(Hayes, A-102) depositor
Author: Shaveta Khepra
48
50. An attribute can also be property of a relationship set.
For instance, the depositor relationship set between entity
sets customer and account may have the attribute access-
date
Author: Shaveta Khepra
50
51. Degree of a Relationship Set
Refers to number of entity sets that participate in a relationship
set.
Relationship sets that involve two entity sets are binary (or
degree two). Generally, most relationship sets in a database
system are binary.
Relationship sets may involve more than two entity sets.
Example: Suppose employees of a bank may have jobs (responsibilities) at
multiple branches, with different jobs at different branches. Then there is a
ternary relationship set between entity sets employee, job, and branch
Author: Shaveta Khepra
51
52. Mapping Cardinality Constraints
Express the number of entities to which another entity can
be associated via a relationship set.
Most useful in describing binary relationship sets.
For a binary relationship set the mapping cardinality must
be one of the following types:
One to one
One to many
Many to one
Many to many
Author: Shaveta Khepra
52
55. Keys
They ensure each record within a table can be uniquely
identified by one or a combination of fields within the table.
They help in identifying the relationship between tables.
A super key of an entity set is a set of one or more attributes whose values
uniquely determine each entity.
A candidate key is a subset of a super key. A candidate key is a single
field or the least combination of fields that uniquely identifies each record in
the table. The least combination of fields distinguishes a candidate key from
a super key. Every table must have at least one candidate key but at the same
time can have several.
Author: Shaveta Khepra
55
56. Author: Shaveta Khepra
56
In order to be eligible for a candidate key it must pass certain
criteria.
•It must contain unique values
•It must not contain null values
•It contains the minimum number of fields to ensure uniqueness
•It must uniquely identify each record in the table
57. Primary Key
A primary key is a candidate key that is most appropriate to be
the main reference key for the table. It is the primary key of
reference for the table and is used throughout the database to help
establish relationships with other tables.
Author: Shaveta Khepra
57
58. Foreign Key
A foreign key is generally a primary key from one table that appears as a
field in another where the first table has a relationship to the second.
Author: Shaveta Khepra
58
63. Cardinality Constraints
We express cardinality constraints by drawing either a
directed line (), signifying “one,” or an undirected
line (—), signifying “many,” between the relationship
set and the entity set.
Author: Shaveta Khepra
63
65. One-to-One Relationship
one-to-one relationship between an instructor and a student
an instructor is associated with at most one student via advisor
and a student is associated with at most one instructor via
advisor
Author: Shaveta Khepra
65
66. One-to-Many Relationship
one-to-many relationship between an instructor and a student
an instructor is associated with several (including 0) students
via advisor
a student is associated with at most one instructor via advisor,
Author: Shaveta Khepra
66
67. Many-to-One Relationships
In a many-to-one relationship between an instructor and a
student,
an instructor is associated with at most one student via
advisor,
and a student is associated with several (including 0)
instructors via advisor
Author: Shaveta Khepra
67
68. Many-to-Many Relationship
An instructor is associated with several (possibly 0) students via
advisor
A student is associated with several (possibly 0) instructors via advisor
Author: Shaveta Khepra
68
69. E-R Diagram for a Banking Enterprise
Author: Shaveta Khepra
69
70. Extended E-R Features: Specialization
Top-down design process; we designate subgroupings within an
entity set that are distinctive from other entities in the set.
These subgroupings become lower-level entity sets that have
attributes or participate in relationships that do not apply to the
higher-level entity set.
Depicted by a triangle component labeled ISA (E.g., instructor
“is a” person).
Attribute inheritance – a lower-level entity set inherits all the
attributes and relationship participation of the higher-level entity
set to which it is linked.
Author: Shaveta Khepra
70
72. Extended ER Features: Generalization
A bottom-up design process – combine a
number of entity sets that share the same features
into a higher-level entity set.
Specialization and generalization are simple
inversions of each other; they are represented in
an E-R diagram in the same way.
The terms specialization and generalization are
used interchangeably.
Author: Shaveta Khepra
72
74. AGGREGATION
Consider the ternary relationship works_on, which we saw
earlier
Suppose we want to record managers for tasks performed by an
employee at a branch
Author: Shaveta Khepra
74
75. Relationship sets works_on and manages represent overlapping
information
Every manages relationship corresponds to a works_on relationship
However, some works_on relationships may not correspond to any
manages relationships
So we can’t discard the works_on relationship
Eliminate this redundancy via aggregation
Treat relationship as an abstract entity
Allows relationships between relationships
Abstraction of relationship into new entity
Without introducing redundancy, the following diagram represents:
An employee works on a particular job at a particular branch
An employee, branch, job combination may have an associated manager
Author: Shaveta Khepra
75
80. 80
DDL
Creating a Database
To initialize a new database:
Syntax:
CREATE DATABASE database_name
There are numerous arguments that go along with this
command but are database specific
Only some databases require database to be created and space
to be allocated prior to creation of tables.
Some databases provide graphical user interfaces to create
databases and allocate space.
Access only allows database to be created using User Interface
81. 81
DDL
Creating a Table
Syntax
CREATE TABLE table_name
(Column_name datatype[(size)],
Column_name datatype[(size)],
)
Example
CREATE TABLE books
(ISBN char(20),
Title char(50),
AuthorID Integer,
Price float)
Creates a table with four columns
82. 82
DDL
Data Types
Following broad categories of data types exist in most
databases:
String Data
Numeric Data
Temporal Data
Large Objects
83. 83
DDL
String Data
Fixed Length:
Occupies the same length of space in memory no matter
how much data is stored in them.
Syntax:
char(n) where n is the length of the String
e.g. name char(50)
If the variable stored for name is ‘Sanjay’ the extra 43 fields
are padded with blanks
84. 84
DDL
String Data
Variable Length string is specified with maximum length of
characters possible in the string, however, the allocation is sized
to the size of the data stored in memory.
Syntax:
Varchar(n) – n is the maximum length of data possible for the type
There may be a restriction in the maximum length of the data
that you can specify in the declaration which will vary
according to the database.
All character data has to be enclosed in single quotes during
specification.
85. 85
DDL
Numeric Data Types
Store all the data related to purely numeric data.
Some numeric data may also be stored as a character field e.g.
zip codes
Common Numeric Types:
Decimal Floating point number
Float Floating point number
Integer(size) Integer of specified length
Money A number which contains exactly two digits
after the decimal point
Number A standard number field that can hold a
floating point data
Note: Different databases name their numeric fields differently and
may not support all numeric types. They may also support
additional numeric types.
86. 86
DDL
Temporal Data Types
These represent the dates and time:
Three basic types are supported:
Dates
Times
Date-Time Combinations
87. 87
DDL
Large Data Objects
These are used for storing data objects like files and
images:
There are two types:
Character Large Objects (clobs)
Binary Large Objects (blobs)
88. 88
DDL
Specifying Keys- Introduction
Unique keyword is used to specify keys.
This ensures that duplicate rows are not created in the database.
Both Primary keys and Candidate Keys can be specified in the database.
Once a set of columns has been declared unique any data entered that
duplicates the data in these columns is rejected.
Specifying a single column as unique:
Example
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
UNIQUE (name))
Here the name column has been declared as a candidate key
89. 89
DDL
Specifying Keys- Multiple Columns
Specifying multiple columns as unique:
Example:
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
UNIQUE (name),
UNIQUE(city, state))
Here both name & city/state combination are declared as
candidate keys
90. 90
DDL
Specifying Keys- Primary Key
Specifying multiple columns as unique:
To specify the Primary Key the Primary Key clause is used
Example:
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
PRIMARY KEY (studio_id),
UNIQUE (name),
UNIQUE(city, state)
)
91. 91
DDL
Specifying Keys- Single and MultiColumn Keys
Single column keys can be defined at the column level
instead of at the table level at the end of the field
descriptions.
MultiColumn keys still need to be defined separately at the
table level
CREATE TABLE Studios
(studio_id Number PRIMARY KEY,
name char(20) UNIQUE,
city varchar(50),
state char(2),
Unique(city, state))
Note: Some databases require the use of Unique Index for
specification of keys.
92. 92
DDL
Specifying Keys- Foreign Keys
References clause is used to create a relationship between a
set of columns in one table and a candidate key in the table
that is being referenced.
Example:
CREATE TABLE Movies
(movie_title varchar(40),
studio_id Number REFERENCES Studios(studio_id))
Creates a relationship from the Movies table to the Studios
table
93. 93
DDL
Constraints- Disallowing Null Values
Disallowing Null Values:
Null values entered into a column means that the data in not
known.
These can cause problems in Querying the database.
Specifying Primary Key automatically prevents null being entered
in columns which specify the primary key
Not Null clause is used in preventing null values from
being entered in a column.
Example:
CREATE TABLE Studios
( studio_id number PRIMARY KEY,
name char(20) NOT NULL,
city varchar(50) NOT NULL,
state char(2) NOT NULL)
Null clause can be used to explicitly allow null values in a
column also
94. 94
DDL
Constraints- Value Constraints
Value Constraints:
Allows value inserted in the column to be checked condition in the column
constraint.
Check clause is used to create a constraint in SQL
Example:
CREATE TABLE Movies
(movie_title varchar(40) PRIMARY KEY,
studio_id Number,
budget Number check (budget > 50000)
)
Table level constraints can also be defined using the Constraint keyword
Example:
CREATE TABLE Movies
(movie_title varchar(40) PRIMARY KEY,
studio_id Number,
budget Number check (budget > 50000),
release_date Date,
CONSTRAINT release_date_constraint Check (release_date between ’01-Jan-1980’ and ’31-
dec-1989))
Such constraints can be activated and deactivated as required.
95. 95
DDL
Constraints- Default Value
Default Value:
A default value can be inserted in any column by using the Default
keyword.
Example:
CREATE TABLE Movies (
movie_title varchar(40) NOT NULL,
release_date date DEFAULT sysdate NULL,
genre varchar(20) DEFAULT ‘Comedy’ Check genere
In (‘Comedy’, ‘Horror’, ‘Drama’)
)
Table level constraints can also be defined using the
Constraint keyword
release_date defaults to the current date, however Null value is enabled
in the column which will need to be added explicitly when data is
added.
Note: Any valid expression can be used while specifying constraints
97. 97
Modifying Records
Insert Statement
Insert:
Allows you to add new records to the Table
Syntax:
Insert into table_name[(column_list)] values (value_list)
Example:
INSERT INTO studios
VALUES (1, ‘Giant’, ‘Los Angeles’, ‘CA’)
INSERT INTO studios
(studio_city, studio_state, studio_name, studio_id)
VALUES (‘Burbank’, ‘CA’, ‘MPM’, 2)
Notes1: If the columns are not specified as in the first example the data goes
in the order specified in the table
Notes2: There are two ways of inserting Null values
1. If the field has a default value of Null, you can use an Insert statement that
ignores the column where the value is to be Null.
2. You can specify the column in the column list specification and assign a
value of Null to the corresponding value field.
98. 98
Modifying Records
Select & Insert
Select & Insert:
A select query can be used in the insert statement to get the values for the insert
statement
Example:
INSERT INTO city_state
SELECT studio_city, studio_state FROM studios
This selects the corresponding fields from the studios table and inserts them
into the city_state table.
Example:
INSERT INTO city_state
SELECT Distinct studio_city, studio_state FROM studios
This selects the corresponding fields from the studios table, deletes the
duplicate fields and inserts them into the city_state table. Thus the final table
has distinct rows
99. 99
Modifying Records
Delete Statement
Delete Statement:
is used to remove records from a table of the database. The where clause in
the syntax is used to restrict the rows deleted from the table otherwise all
the rows from the table are deleted.
Syntax: DELETE FROM table_name [WHERE Condition]
Example:
DELETE FROM City_State
WHERE state = ‘TX’
Deletes all the rows where the state is Texas keeps all the other
rows.
100. 100
Modifying Records
Update Statement
Update Statement:
used to make changes to existing rows of the table. It has three parts. First, you
,must specify which table is going to be updated. The second part of the statement
is the set clause, in which you should specify the columns that will be updated as
well as the values that will be inserted. Finally, the where clause is used to specify
which rows will be updated.
Syntax:
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, …..
[WHERE Condition]
Example:
UPDATE studios
SET studio_city = ‘New York’, studio_state = ‘NY’
WHERE studio_id = 1
Notes1: If the condition is dropped then all the rows are updated.
101. 101
Modifying Records
Truncate Statement
Truncate Statement:
used to delete all the rows of a table. Delete can also be used to delete all the rows
from the table. The difference is that delete performs a delete operation on each
row in the table and the database performs all attendant tasks on the way. On the
other had the Truncate statement simply throws away all the rows at once and is
much quicker. The note of caution is that truncate does not do integrity checks on
the way which can lead to inconsistencies on the way. If there are dependencies
requiring integrity checks we should use delete.
Syntax: TRUNCATE TABLE table_name
Example:
TRUNCATE TABLE studios
This deletes all the rows of the table studios
102. 102
Modifying Records
Drop Statement
Drop Statement:
used to remove elements from a database, such as tables, indexes or
even users and databases. Drop command is used with a variety of
keywords based on the need.
Drop Table Syntax: DROP TABLE table_name
Drop Table Example: DROP TABLE studios
Drop Index Syntax: DROP INDEX table_name
Drop Index Example: DROP INDEX movie_index
103. 103
Modifying Records
Alter Statement
Alter Statement:
used to make changes to the schema of the table. Columns can be
added and the data type of the columns changed as long as the data in
those columns conforms to the data type specified.
Syntax:
ALTER TABLE table_name
ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
Example:
ALTER TABLE studios
ADD (revenue Number DEFAULT 0)
104. 104
Modifying Records
Alter Statement
Add table level constraints:
Syntax:
ALTER TABLE table_name
ADD ([CONSTRAINT constraint_name CHECK comparison]
[columns REFERENCES table_name (columns)]
Example:
ALTER TABLE studios
ADD (CONSTRAINT check_state CHECK (studio_state in (‘TX’, ‘CA’, ‘WA’))
Modify Columns:
Syntax:
ALTER TABLE table_name
MODIFY column [data type]
[Default Expression]
[REFERENCES table_name (column_name)’
[CHECK condition]
Example:
ALTER TABLE People
MODIFY person_union varchar(10)
Notes1: Columns can not be removed from the table using alter. If you want to
remove columns you have to drop the table and then recreate it without the column
that you want to discard
105. 105
Modifying Records
Alter Statement
Alter Statement:
used to make changes to the schema of the table. Columns can be
added and the data type of the columns changed as long as the data in
those columns conforms to the data type specified.
Syntax:
ALTER TABLE table_name
ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
Example:
ALTER TABLE studios
ADD (revenue Number DEFAULT 0)
107. 107
Select Clause
Syntax
Select <List of Columns and expressions (usually involving columns)>
From <List of Tables & Join Operators>
Where <List of Row conditions joined together by And, Or, Not>
Group By <list of grouping columns>
Having <list of group conditions connected by And, Or, Not >
Order By <list of sorting specifications>
108. 108
Select Clause
Conceptual Evaluation
From Tables: Cross
product and join
operations
1
Restriction on where
conditions 2
Group
By?
Order By?
Project columns in
SELECT
finish
Sort on Group
BY columns
Yes
Compute
aggregates
and reduce
each group
to 1 row
3
4
Restriction
on HAVING
conditions
5
Sort
columns in
ORDER BY
No
No
Yes
6
7
109. 109
Select Clause
Example
Query:
Select movie_title, studio_id
From Movies
Where movie_type = ‘Comedy’
Notes:
Database looks in the movie_type column until it locates a comedy.
When it finds comedy it retrieves the value of movie_title & studio_id
The where clause is optional. When not specified the columns from all
the records are extracted.
Changing the order in the select_list changes the order in which the
columns are displayed
Using a * for the select_list selects all the columns from the table. They
are listed in the same order as in the original table.
Output:
Movie_Title Studio_ID
----------------------------------------
Vegetable house 1
Broccoli Wars 2
Carrot Affairs 4
Chocolat 1
Cranberry House 2
110. 110
Select Clause
Expressions in Select List
Example:
Select movie_title, gross, gross*1.5
From Movies
Output:
Movie_Title gross gross*1.5
----------------------------------------
Vegetable house 30 45
Broccoli Wars 20 30
Carrot Affairs 11 16.5
Chocolat 10 15
Cranberry House 50 75
111. 111
Select Clause
Operators
Arithmetic operators supported by SQL
() Parentheses
/ Division
* Multiplication
- Subtraction
+ Addition
Associativity and Precedence:
Precedence is the order in which operators are evaluated
Associativity is the order in which operators of same precedence
are evaluated
Multiplication and Division have the same precedence and
Subtraction and Division have the same precedence.
Equal precedence operators are evaluated from right to left
Parentheses can be used to control the sequence of evaluation of
various operators
112. 112
Select Clause
Alias (as)
Used to assign names to the columns when they are retrieved from
the database table.
Syntax:
Select expr1 [as alias1], expr2 [as alias2] [, … ]
From table1 [, table2, …]
[Where condition]
Example:
Select city, ((1.8 + avg_temp) + 32) AS temperature
From Temperature
Output
City Temperature
----------------------------------------
London 61.7
Albany 78.4
Paris 66.2
113. 113
Select Clause
Alias (as)
A multiword heading needs to be enclosed in double
quotes
Example:
Select city, ((1.8 + avg_temp) + 32) AS “Average Temperature”
From Temperature
Output:
City Average Temperature
-----------------------------------------------------
London 61.7
Albany 78.4
Paris 66.2
114. 114
Where Clause
Basics
Conditional statements in the select clause restrict the selection
of rows in the database.
It can be used in a variety of SQL Statements
Syntax:
Update table Set (column = value, column = …) [Where condition]
Delete From table [Where condition]
Select list from table [Where condition]
Condition is a Boolean expression which evaluates to true or
false
Complex expressions can be generated by using logical operators
115. 115
Where Clause
Operators
Arithmetic Operators used in the where clause
= equal
<>, != not equal
> Greater Than
< Less Than
>= Greater than or equal to
<= Less than or equal to
Logical operators
AND
OR
NOT
For numeric operator comparison you should not use quotes
around the number
You should put single quotes around characters and strings
116. 116
Where Clause
Null Values
Null values are unknown so the regular operators can not be
used for comparison
IS NULL is used to check if the field contains a null value or not.
IS NOT NULL is used to see if a field is not null
Example
Select movie_title
From movies
Where gross is null
Select movie_title
From movies
Where gross is not null
117. 117
Where Clause
Examples
Example:
Select movie_title, studio_id , gross
From Movies
Where studio_id = 3 and gross Is Null
Output:
Movie_Title Studio_ID GROSS
----------------------------------------------------------------
Bill Durham 3
Example:
Select movie_title, studio_id , gross
From Movies
Where studio_id = 3 OR gross Is Null
Output
Movie_Title Studio_ID GROSS
----------------------------------------------------------------
Bill Durham 3
Prince Kong 2
SQL Strikes Back 3 10
The Programmer 25.5
118. 118
Where Clause
Examples
Example:
Select movie_title, studio_id , gross
From Movies
Where studio_id = 3 and NOT gross Is Null
Output
Movie_Title Studio_ID GROSS
----------------------------------------------------------------
SQL Strikes Back 3 10
The Programmer 3 25.5
Example:
Select movie_title, studio_id, gross
From Movies
Where studio_id = 3
or studio_id = 2
or studio_id = 1
Output
Movie_Title Studio_ID GROSS
----------------------------------------------------------------
SQL Strikes Back 3 10
The Programmer 3 25.5
119. 119
Where Clause
IN condition
IN condition checks if the values in a column are present in list list when
selecting
Syntax:
Select select_list
From table
Where column [not] in (value_list)
Example (Using IN):
Select movie_title, studio_id
From Movies
Where studio_id in(2, 3)
Example (not Using IN)
Select movie_title, studio_id
From Movies
Where studio_id = 2
or studio_id = 3
NOT IN can similarly be used to select rows where values do not match
120. 120
Where Clause
Between condition
Between condition is used to see if the value of a column lies
between specified ranges
Syntax:
Select movie_title, budget
From table
Where column [not] between lower_value and upper_value
Example:
Select movie_title, budget
From Movies
Where budget between 10 and 50
Alternate Query:
Select movie_title, budget
From Movies
Where budget > 10 and budget < 50
121. 121
Where Clause
Like
Like allows a matching of patterns in the column data
Syntax:
Select select_list
From table
Where column [not] like ‘pattern’ [Escape char]
Wildcards:
- Any Single Character
% (or *) 0 or more characters
A combination of ‘-‘ and ‘%’ can be used to mean 1 or more
For test of fixed number of characters multiple dashes can be used
For example ‘----’ will select all 3 letter words from the column
Example:
Select movie_title
From movies
Where movie_title like ‘The %’
Output:
movie_title
------------
The Code Warrior
The Linux Programmer
The Rear Windows
122. 122
Where Clause
Escaping wild card characters
SQL allows you to define your own escape characters if
you want to include the % as a part of the search string.
Example:
Select movie_title
From movies
Where movie_title like ‘%50%%’ ESCAPE
This shows that the escape character is
123. 123
Where Clause
String Comparison
Example
Select movie_title, studio_id
From Movies
Where movie_title = ‘Independence Day’
Output
Movie_title Stuodio_ID
-----------------------------------------
Independence Day 1
Functions for where clauses
Upper()
Lower()
Trim()
Length()
Example:
Select studio_name
From Studios
Where lower(studio_state) = ‘ca’
Output:
Studio_name
----------------
Giant
Mpm
Metaversal Studios
124. 124
Where Clause
Expressions
Similar to the expressions in the select clause
Example:
Select movie_title, gross, budget
From movies
Where gross > (2 * budget)
Output
Movie_Title Gross budget
--------------------------------------------
Prince Kong 51.5 3.25
125. 125
Select Clause
Distinct
Eliminates all the duplicate entries in the table resulting from the
query.
Syntax:
Select [DISTINCT] select_list
From table[, table, …]
[Where expression]
[Order By expression]
Example:
Select DISTINCT studio_id, director_id
From Movies
studio_id director_id
1 1
2 2
2 10
3 1
3 9
126. 126
Select Clause
Distinct
Eliminates all the duplicate entries in the table resulting from the
query.
Syntax:
Select [DISTINCT] select_list
From table[, table, …]
[Where expression]
[Order By expression]
Example:
Select DISTINCT studio_id, director_id
From Movies
studio_id director_id
1 1
2 2
2 10
3 1
3 9
127. 127
Select Clause
Order By - Syntax
Used to sort the results based on contents of a column
Multiple levels of sort can be done by specifying multiple
columns
An expression can be used in Order By clause
Syntax:
Select function (column)
From table1 [, table2 …]
[Where condition]
[Order By {Column | alias | position} [ASC | DESC]]
128. 128
Select Clause
Order By - Example
Query: Sort Movies by profits in Ascending order
Select MovieTitle, Gross, Budget, (Gross – Budget) as profits
From movies
Order BY profits
Great Escape 67.5 70 -2.5
Upside Down 54 50 4
Green Warrior 96 80 16
Blue Oranges 28 7 21
Movie_title Gross Budget Profit
129. 129
Select
Aggregate Queries
Aggregate queries provides a more holistic view of the data
by further processing the retrieved data.
Categorizes the query results according to the contents of a
column in the database
Multiple levels of subgroups can be created by specifying
multiple columns
They can work on
On all the rows in a table
A subset of rows in a table selected using a where clause
Groups of selected data organized using Group By clause.
130. 130
Select - Aggregate Queries
Group By (Syntax)
Syntax:
Select function(column)
From <list of tables>
Where <condition>
Group By <list of columns>
Having <condition>
131. 131
Aggregate Queries
Functions
Functions:
Sum() Returns a sum of the column
Count() Returns a total number of rows returned by a query
Avg() Returns the average of a column
Min() Returns minimum value of the column returned by query
Max() Returns maximum value of the column returned by query
Count function
does not include columns containing null values in total
can be used with distinct to count the number of distinct rows
Output: Sum(budget)
---------------
65.1
Example:
Query: Select sum(budget)
From movies
Where studio_id = 3
132. 132
Select - Aggregate Queries
Group By (Examples)
Problem 1:
Get # of movies by each director for each studio
Select studio_id, director_id, count(*)
From Movies
Group By director_id, studio_id
Problem 2:
Get # of movies by each studio ordered by studio_id
Select studio_id, count(*)
From Movies
Group By studio_id
Order By studio_id
133. 133
Select - Aggregate Queries
Group By (Examples)
Problem 3: (Summation)
Select studio_id, Sum(budget)
From movies
Group by studio_id
Having Sum(budget) > 60
Problem 4: (Count)
Select studio_id, count(*)
From Movies
Group By studio_id
Order By studio_id