SQL Day 1
SQL Day 1
CHAPTER 1
INTRODUCTION TO SQL
What is SQL?
SQL is an acronym of Structured Query Language. It is built to work around
relational databases which are sets of related information stored in tables. SQL
was introduced by IBM as the language to interface with its prototype relational
database management was introduced in 1979 by Oracle Corporation.
Benefits of SQL
• SQL has become the database language of choice because it is flexible, powerful,
and easy to learn.
• SQL is a non- procedural language. Process sets of records rather than just one at
a time and provides automatic navigation to the data.
• SQL provides commands for a variety of tasks including: querying data, creating,
updating and replacing objects and inserting, updating and deletion rows.
• All major relational database management systems support SQL thus one can
transfer all the skills gained with SQL from one RDBMS to another. Programs
written in SQL are portable; they can often be moved from one database system
to another with little modification.
Division of SQL
Every SQL statement begins with a command word key phrase. There are three major
categories.
Data definition language(DDL)
Create (new database objects)
Alter (Existing database objects)
Drop (existing database objects)
Data manipulation language (DML)
Select (to query)
Update (modifying tables)
Insert (adding new records)
Delete (remove existing records)
Data control language (DCL)
Grant (create user, provides database objects access)
Revoke (revokes the user, revokes database objects access)
Commit (commit the changes)
Rollback (nullify the changes)
Concepts of RDBMS
Database system
A database system is a computerized information
system for the management of data by means of a
general purpose software package called a database
management system. (DBMS). The major components
of a database management system are as follows:
• Computer hardware
• Database management system (software)
• Data to be manipulated
• User
Objectives of database system
1. Data independence: Different DBMS’s provide different of data independence to
protect application programs from modifications due to change in the database file
structure.
2. Minimizing data redundancy: Data from the different users are pooled and integrated
into the database in order to be shared by all users. Redundant data is thus minimized.
A properly designed database will reduce the redundant data storable to a minimum.
3. Data integrity: Some areas of concern in enforcing data integrity are input data
validation, concurrence interference and deadlock.
4. Data security: Because of extensive sharing in the database environment, the DBMS
protects an individual’s user’s data from unauthorized or destruction.
The DBA (Database Administrator) computer a programmer’s access right to a certain
portion of a database. There is control over access to the database. Access to the
database is controlled at a number of levels.
5. Efficiency: Providing fast response to user’s request and doing it economically is the
overall goal of the database system. Typically, a database query requires access to only
small portion of a large integrated database.
6. Data models: Data in the network is represented by a collection of records and
relationships among data are represented by links, which can be viewed as pointers.
Relational model
A relational model stores closely related data
in logical units called tables. A table can be
visualized as a series of columns and rows, in
which each column represents one type of
information and each row represents all the
types of information about a specific entity. In
relational system, there are no fixed
relationships between tables and no parent-
child requirements.
Tuple: Rows of a table are called tuples
Primary key: Primary key signifies a column whose values uniquely identify
the records in a file. E.g. employee number is primary key in employee
table, as it uniquely identifies any records in the employee table.
Foreign key: An ordinary column in a relation acting as a primary key, in
another relation is a foreign key. E.g. department number is a primary key
in department table, whereas it is foreign key in employee table.
Entity:
An entity is an object that exists and is distinguishable from other objects.
E.g. seema virnave with social security number 1001 is an entity, since it
uniquely identifies one particular person in the universe. Similarly 82001
in canara bank at Nehru place is an entity that uniquely identifies one
particular account.
Attribute:
Each column in a relation represents an attribute. It is equivalent to a
field. An entity is described by one or more attributes.
Examples
Consider the following relation
No name place score
1 SURESH THIRUNELVELI 100
2 RAJA TUTICORIN90
3 KUMAR PALAYAMKOTTAI 90
4.VENKATESH NAGERCOVIL 68
No, Name, Place, Score attributes
100,90,90,68 domain
1, SURESH, THIRUNELVELI, 100 tuple
Entity relationship
E-R diagram models data and depicts how the
business views its structure. Entity relationship
diagram defines the relationships and dependencies
between various entities. It helps in identifying
information and leads to identifying various attributes
of an entity.
Types of relations
one to one (1:1)
An entity in A associated with at most one entity in B,
and an entity in B is associated with a most one entity
in A.
Example 1: employee is allocated a company car.
Example 2:
Many to one (M: 1)
An entity in A associated with at most one
entity in B. An entity in B, however can be
associated with any number of entities.
Example 1:
Many to Many (M: M)
An entity A is associated with any number of
entities in B, and an entity in B is associated
with number of entities in A.
E.F. codds’s rule
1. Information rule (representation of information): All
information in a RDBMS including tables, column names is
represented by values in tables. Benefit- this simple view of data
speeds up design and learning. Information is stored in the form
of tables containing rows and column.
2. Guaranteed access rule: Every piece of data in a RDMS can be
accessed by using a combination of a table name, and a primary
key value that identifies a row. Benefits- retrieving each
individual piece of data stored in a database becomes easy.
3. Systematic treatment of NULL values: RDBMS handles records
that have unknown or inapplicable values in a redefined fashion.
RDBMS also distinguishes between ZEROS, BLANKS, NULLS in
records and handles such values in a consistent manner to
produce correct answers.
4. High-level insert, update and delete: The
capability of handling a base relation or a
derived as a single operand applies and only to
the retrieval of data, but also to the insertion,
updation, and deletion of data.
Normalization
Normalization is the process of simplifying the
relationship between data elements in a record.
Through normalization a collection of data in a
record structure is replaced by successive record
structures that are more simpler.
CHAPTER 2