You are on page 1of 46

Database Management

Data vs. information


What is data?
Data are streams of raw facts. The numbers, words, images and symbols which do not
bear a meaning, when standing alone are called data.
What is Information?
Information is the organized or classified data, which has some meaningful values for the
receiver. Creation of information can be defined as processing of data. Information is used to
make decisions and take actions.

Structured Vs. Unstructured data


Structured data
Structured data refers to information with a high degree of organization which is
comprised of clearly defined data types. Structured data is easily searchable by basic algorithms.
Ex. Data resides in relational databases
Unstructured data
Unstructured data refers to information that either does not have a recognizable structure
or is not organized in a pre-defined manner. Unstructured data is not easily searchable.
Ex. Text files, Social Media postings, Email, Videos, Photos, Audio files
What is a Database?
A database is an integrated, organized collection of logically related records and files.
Database models
Database model is a collection of concepts that can be used to show the logical structure
of a database, including the relationships and constraints that determine how data can be stored
and accessed.
Flat file model
The flat model is the earliest, simplest data model. It simply lists all the data in a single
table, consisting of columns and rows.

Manager

Advantages:
 Easy to setup
 Easy to understand (a simple design)
Disadvantages:
 Data Redundancy (Same data is often repeated many times)
 Data Inconsistency (As same data is stored more than once, it is possible that
two different values for the same item could be stored)
 No structured interrelationship between data records
 Program/data dependency (Changes in the format of the data also required
changes in the programs)
Hierarchical model
The hierarchical model organizes data into a tree-like structure, where each record has
one parent record and many children records.

Student

Paul

Advantages:
 Can express relationships between information
 Structures data in an upside-down tree (Simplifies data overview)
 Sufficient data independence (Reduced programming and maintenance
effort)
Disadvantages:
 Can only model one-to-one and one to many relationships, many to many
relationships are not supported.
 Data redundancy
 Can be slow on retrieving information
Network model
The network model builds on the hierarchical model by allowing many-to-many
relationships between linked records. It allows a record to have more than one parent.

Advantages:
 Can model many to many relationships
 Can better model complex real-world relationships
 Minimal data redundancy
 Sufficient data independence
Disadvantages:
 More difficult to navigate and visualize compared to the hierarchical
model
 Difficult to implement and maintain
 Lack of structural independence
Relational model
The relational model sorts data into tables, also known as relations, each of which
consists of columns and rows.

Manager

Manager

Advantages:
 More easily visualize data organization & relationships
 Ease of Data manipulation (accessing data in a database does not require
navigating a rigid pathway through a tree or hierarchy).
 Flexibility (scalable and extensible, provide a flexible structure to meet
changing requirements and increasing amounts of data)
 Supports one-to-one, one-to-many & many-to-many relationships.
 Minimal data redundancy and Inconsistency
 Data independence

Disadvantages:
 Can affect application performance (if the number of tables is large on
which relationships to be established it can affect the performance in
responding to the SQL queries).
 Hardware overheads (needs more powerful machines to run smoothly).
Object Relational model
A hybrid database model combines the simplicity of the relational model with the
advanced functionality of the object-oriented database model (defines a database as a collection
of objects). It allows designers to incorporate objects into the familiar table structure.

Advantages:
 Using OOP (object-oriented programming), ability to model, store & manipulate
complex data (i.e. multimedia)
 Reuse and Sharing (the ability to extend the DBMS server to perform standard
functionality centrally, rather than having it coded in each application)
 Increased Productivity (allows users and programmers to start using object-
oriented systems in parallel).
Disadvantages:
 Complexity
 High implementation costs
Relational database model
Relational data model is the primary data model, which is used widely around the world
for data storage and processing. This model is simple and it has all the properties and capabilities
required to process data with storage efficiency. The model was first proposed by E.F.Codd of
IBM research in 1970.

Main components of the relational database model


Relation
 All data and relationships are represented in a two dimensional table called a
relation.
 A table has rows and columns, where rows represent records/tuples and columns
represent the attributes.

Ex. Attributes and tuples of relation Student


Relationship
 The fundamental feature that differentiates relational databases from other
database types (e.g., flat-files) is the ability to define relationships.
 A relationship, in the context of databases, is a situation (connection) that exists
between two relational database tables.
 A relationship is established between two database tables when one table has a
foreign key that references the primary key of another table.
Constraints
Every relation has some conditions that must satisfy for it to be a valid relation. These
conditions are called Relational Integrity Constraints. Constraints are useful tools for database
designers for enforcing business logics.
Types of Constraints
 NOT NULL Constraint
 The NOT NULL constraint is a restriction placed on a column in a
relational database table.
 It enforces the condition that, in that column, every row of data must
contain a value - it cannot be left blank during insert or update operations.
i.e. NOT to accept NULL values
 Ex. In relation Student, StudentID column will NOT accept NULL values

 UNIQUE Constraint
 The UNIQUE constraint ensures that all values in a column are different.
i.e. No duplicate values are permitted.
 Both the UNIQUE and PRIMARY KEY constraints provide a guarantee
for uniqueness for a column or set of columns.
 A PRIMARY KEY constraint automatically has a UNIQUE constraint.
 However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
 Ex. In relation Student, values of StudentID column will be UNIQUE

 PRIMARY KEY Constraint


 The PRIMARY KEY constraint uniquely identifies each record in a
database table.
 Primary keys must contain UNIQUE values, and cannot contain NULL
values.
 A table can have only one primary key, which may consist of single or
multiple fields.
 Ex. In relation Student, StudentID column will be its PRIMARY KEY
 FOREIGN KEY Constraint
 A FOREIGN KEY is a key used to link two tables together.
 A FOREIGN KEY is a field (or collection of fields) in one table that refers
to the PRIMARY KEY in another table.
 Ex. Relationship between Student and Course

 CHECK constraint
 The CHECK constraint is used to limit the value range that can be placed
in a column.
 It can refer to a single column, or multiple columns of the table.
 Ex.in relation Product, Quantity of a product should be a non-negative
value
QUANTITY >= 0
Entity Relationship (ER) diagrams
The ER model defines the conceptual view of a database. It works around real world
entities and the relationships among them. American computer scientist Peter Chen is credited
with developing ER modeling for database design in the 1970s.
ER model uses 3 main constructs:
1. Entities
2. Attributes
3. Relationships
Entity
 An entity is a real-world object, either animate or inanimate, that can be easily
identifiable.
 Entities have attributes or properties that give them their identity.
 An entity set is a collection of similar types of entities.
 Entities are represented by rectangles.
 Rectangles are named with the entity set they represent.
 Ex.

Student Customer

 Weak entity is an entity that cannot be uniquely identified by its own attributes
alone. i.e.do not have a Key attribute on their own.
 Weak entity must defined by a foreign key relationship with another entity.
 Weak entity is represented by a double-lined rectangle.
 Ex. Dependents of an employee

Dependent

Attributes
 A property or characteristic of an entity.
 Attributes are represented by ellipses.
 Every ellipse represents one attribute and is directly connected to its entity
(rectangle).
 Ex. Attributes of Entity Student

Types of Attributes
 Simple attribute
Simple attributes are atomic values, which cannot be divided further. Also
known as Atomic attributes.
Ex. Customer’s phone number
 Composite attribute
Composite attributes are made of more than one simple attribute. i.e.
Composite attributes can be further divided into subparts with independent
meaning.
Ex. Student’s name

Composite attributes are represented by ellipses that are connected with an


ellipse.

 Derived attribute
Derived attributes are the attributes that do not exist in the physical
database, but their values are derived from other attributes present in the database.
Ex. Age of an Employee is an derived attribute from attribute Birth Date

Derived attributes are represented by dashed ellipses.

 Single-value attribute
Single-value attributes contain single value.
Ex. Student ID
 Multi-value attribute
Multi-value attributes may contain more than one values.
Ex. Contact Numbers of a Customer

Multivalued attributes are represented by double ellipse.


Entity identifiers /Keys
 A Key is an attribute or a collection of attributes that uniquely identifies an entity
among its entity set.
 Ex. Student ID of a Student
Types of Keys
 Super Key
A set of attributes (one or more) that can uniquely identify an entity in an entity
set. i.e. no two combinations have the same values for the attributes.
Ex. Attributes of Student – Student_ID, Name, DOB, NIC
Super Keys – { Student_ID }
{ Student_ID, Name }
{Student_ID, Name, DOB}
{Student_ID, Name, DOB, NIC }
 Candidate Key
A super key with no redundant attribute is known as candidate key. A candidate
key is a super key but a super key is not necessary a candidate key. There can be more
than one candidate key in a relation.
Ex. Student_ID , NIC
 Primary Key
A primary key is one of the candidate keys chosen to uniquely identify the entity
set. Primary keys can be represented by underlined attributes.
Ex. Student_ID

Student_ID Name

Student

 Alternate Key
Alternate Key is a candidate key not used for Primary key
Ex. NIC of Student

 Foreign Key
If an attribute can only take the values which are present as values of some other
attribute, it will be foreign key to the attribute to which it refers.
Ex. Employee_No attribute Employee relation will be a foreign key of Dependent
relation

 Partial Key
A weak entity has a Partial Key which can be used to distinguished 2 weak
entities belong to the same owner entity.
Ex.
Employee

Wife Son
(Dependent 1) (Dependent 2)

Primary key(Weak) = Primary key(Owner) + Partial key(Weak)


Primary key(Dependent) = Employee_No + Dependent_name
Relationships
 A relationship is an association (Connection) between 2 or more entities.
 Relationships are represented by diamond-shaped box.
 Name of the relationship is written inside the diamond-box.
 All the entities (rectangles) participating in a relationship, are connected to it by a
line.
Degree of Relationship
The number of participating entities in a relationship defines the degree of the
relationship.
1. Unary type relationships
When the same entity participates more than once in a relationship. Also known
as Recursive relationship.

Ex.

2. Binary type relationships


A relationship where two entities are participating is called a binary relationship.
Ex.
3. Ternary type relationships
A relationship where three entities are participating is called a ternary
relationship.
Ex.

 Relationship can also have attributes (Descriptive Attribute).


 Ex. Qty attribute on Supply Relationship between Supplier, Project and Parts
 Weak Relationship connects Owner entity with a weak entity.
 Weak relationship is represented by double lined diamond.
 Ex.

Employee_No

 Partial key of a weak entity can be represented by dotted line attributes.


Cardinality
Cardinality specifies number of instances (occurrences) of an entity which are associated
(or linked) to the number of instances of another entity.

 One to One (1:1)


When only one instance of an entity is associated with at most one instance of
another entity. It is marked as '1:1'.
Ex.

 One to Many (1:M)


When one instance of an entity is associated with more than one instance
of another entity. It is marked as '1:M'.
Ex.

 Many to Many (M:N)


When more than one instance of an entity is associated with more than one
instance of another entity. It is marked by (M:N)
Ex.
Participation Constraints
Participation constraint specifies whether the existence of an entity depends on its
being related to another entity via the relationship type.

 Total Participation
Each entity is involved in the relationship. Total participation is represented by
double lines.

 Partial participation
Not all entities are involved in the relationship. Partial participation is represented by
single lines.
Ex.

Employee_No

1 M

Partial Total
Introduction to EER (Extended ER) diagrams
Extended entity-relationship diagrams also known as Enhanced entity-relationship
diagrams are specialized ER Diagrams that can be used for advanced modeling of databases.
EERDs use several concepts that are closely related to object-oriented design and programming.
In addition to the same concepts that ordinary ER diagrams encompass, EERDs include:

● Subclasses, Superclasses and Inheritance


● Specialization
● Generalization

Subclasses, Superclasses and Inheritance

Subclass - A subgroup of entities with unique attributes.

Superclass - An entity type that relates to one or more Subclasses.

Inheritance - The concept that attributes of higher-level entity-sets are inherited by lower-level
entity-sets.

Specialization

Specialization is the process of defining a set of subclasses of an entity type based


on some distinguish characteristic. It is a top-down approach.

Ex.
Generalization

Generalization is the process of defining a general entity type from a collection of


specialized entity types. Generalization is the reverse process of Specialization. It is a bottom-up
approach.

Ex.
Overlapping and Disjoint design constraints
Overlapping – Means that the same (real-world) entity may be a member of more than one
subclass of the specialization.
Ex.
Disjoint - Means that an entity can be a member of at most one of the subclasses of the
specialization.
Ex.

Ex. EER diagram


Conversion of ER diagrams to Relational schema
Relational schema
 Relational or Database schema describes the structure and constraints of data
representing in a particular domain.
 Domain is the allowed values (types, values, length) of an attribute.
Ex: in the calendar year, 12 months are allowed.
 It is a tabular structure representation of an ER diagram in order to visualize
entities, relationships and relationship keys in a more design oriented way.
 A Relational schema can be described as a blueprint of a database that outlines the
way of how data is organized into tables.
Relation instances
Relation instances are the values of the attributes at a given instances.

Steps in the ER-to-Relational mapping


Step 1: Mapping of Strong entity types
 For each strong entity type create a relation that include all the simple attributes of
the entity.
 Choose one of the key attributes as the primary key.
 Any composite attributes must be represented as simple attributes.
 Omit multi valued and derived attributes.

Step 2: Mapping of Weak entity types


 For each weak entity type create a relation and include all simple attributes (or
simple components of composite attributes)
 Include the primary key of the owner entity as a foreign key.
 Owner’s primary key + partial key of the weak entity becomes the primary key.
Ex.

Fname Lname
Relationship
EmpNo

EmpName

Telephone
Dname

Age
DOB

Step 1
Employee (EmpNo, Fname, Lname, DOB)
Step 2
Dependent (EmpNo, Dname, Relationship)

Step 3: Mapping of Binary 1:1 Relationship types


 Select an entity (it is better to select the entity with the total participation) and
include the primary key of other entity as a foreign key.
 Include attributes of the relationship (descriptive attributes) into the selected
entity relation as well.
Ex.

DepNo

StartDate DepName

Employee (EmpNo, Fname, Lname)


Department (DepNo, Depname, EmpNo, StartDate)

Step 4: Mapping of Binary 1: M Relationship types


 Select the entity in the many side of the relationship and include the primary key
of the one side as a foreign key of the many side entity.
 Include attributes of the relationship (descriptive attributes) into the many side as
well.
Ex.

DepNo

DepName
StartDate
Employee (EmpNo, Fname, Lname, DepNo, StartDate)
Department (DepNo, Depname)

Step 5: Mapping of Binary M: N Relationship types


 For each binary M: N relationship, create a new relation for the relationship.
 Include primary keys of both participating entities and any attributes of the
relationship.
 Primary key will be the concatenation of primary keys of participating entities.

Step 6: Mapping of Multivalued Attributes


 For each multivalued attribute create a new relation
 Include the primary key attributes of the entity type
 Primary key will be the primary key of the entity and the multivalued attribute
itself.
Ex.

Pname

Telephone

Hours Location
Step 5
WorksOn (EmpNo, Pname, Hours)
Step 6
Telephone (EmpNo, Telephone)
Step 7: Mapping of N-ary Relationship Types
 For each n-ary relationship, create a new relation.
 Include primary keys of all participating entities and any attributes of the
relationship.
 Primary key will be the concatenation of primary keys of participating entities.
Ex.

Supply ( Sname, Partno, Pname, Qty )

Database Normalization
 Normalization is the process of restructuring the logical data model of a database
to,
 Eliminate data redundancy
 Organize data efficiently
 Reduce data duplication
 Reduce the potential for anomalies during data operations (Insert, Update
and Delete).
 Based on functional dependencies among the attributes of a relation.
 Functional dependency describes the relationship between attributes in a relation.
 3 Types of Functional Dependencies:
1. Full dependency
2. Partial dependency
3. Transitive dependency
 Normalization is a multi-step process beginning with an “unnormalized” relation.
Levels of Normalization (Normal Forms)
 Zero Normal Form (0NF)
 First Normal Form (1NF)
 Second Normal Form (2NF)
 Third Normal Form (3NF)

Zero Normal Form (0NF)


Zero Normal form also known as the unnormalized form is any relation in its raw state,
They commonly contain repeating values, and other characteristics that are not found in
normalized relations.
Ex.
First Normal Form (1NF)
 First normal form (1NF) lays the groundwork for an organized database design.
 1NF states that domains of all attributes in the relation must include only atomic
(simple & indivisible) values and the value of each attribute must be a single
value.
First normal form enforces these criteria:
 Eliminate repeating groups in individual tables.
 Identify each set of related data uniquely.
Ex.
1NF Storage Anomalies
Insertion: Cannot insert a new patient who has not yet undergone surgery (since surgeon
# is part of the key).
Cannot insert a newly hired surgeon who hasn’t operated yet.
Update: If a patient comes in for a new procedure, and has since moved, need to change
multiple address entries.
Deletion: Deleting a patient record may also delete all info about a surgeon.
When there are functional dependencies (like side effects and drug) changing
one item eliminates other information.

Second Normal Form (2NF)


 A relation is said to be in Second Normal Form if it is in 1NF and every non-key
attribute is fully functionally dependent on the primary key.
 That is, every non-key attribute needs the full primary key for unique
identification.
Partial Dependency
When a non-key attribute is determined by a part, but not the whole, of a COMPOSITE
primary key.
Ex. Patient Name and Patient address can be determined by Part of the Primary key:
Patient #
Ex. Relation 1: Patient
Relation 2: Surgeon

Relation 3: Surgery
1NF Storage Anomalies: Removed
Insertion: Can now enter new patients without surgery.
Can now enter Surgeons who haven’t yet operated.
Deletion: If Charles Brown dies, the corresponding tuples from Patient and Surgery
tables can be deleted without losing information on David Rosen.
Update: If John White comes in for third time, and has moved, only need to change the
Patient table.

2NF Storage Anomalies


Insertion: Cannot enter the fact that a particular drug has a particular side effect unless it
is given to a patient.
Deletion: Can lose the information that penicillin can cause a rash if John White receives
some other drug because of the penicillin rash, and the new drug and its side
effect are entered.
Update: If drug side effects change (a new formula) need to update multiple occurrences
of side effects.

Third Normal Form (3NF)


 A relation is said to be in Third Normal Form if it is in 2NF and there is no transitive
functional dependency between non-key attributes
Transitive Dependency
When a non-key attribute determines another non-key attribute.
Ex. The side effect column in the Surgery table is determined by the drug administered
Ex. Relation 1: Surgery

Relation 2: Drug
2NF Storage Anomalies: Removed
Insertion: Can now enter the fact that a particular drug has a particular side effect in
the Drug relation.
Deletion: If John White receives some other drug as a result of the rash from
penicillin, but the information on penicillin and rash is maintained.
Update: The side effects for each drug appear only once.

Data Base Management System (DBMS)


 A DBMS is a software that allows creation, definition and manipulation of databases.
 A database management system stores data in such a way that it becomes easier to
retrieve, manipulate, and produce information.
 The DBMS serves as an interface between the database and end users or application
programs.
 The DBMS manages three important things:
1. The data
2. The database engine (that allows data to be accessed, locked and modified)
3. The database schema (that defines the database’s logical structure)
Ex. Microsoft Access, MySQL, SQL Server, Oracle
Components of DBMS
 Hardware
Consists of a set of physical electronic devices such as storage devices, on which
data is stored and I/O devices, processors, main memory etc. which are used for
storing and retrieving the data in a fast and efficient manner.

 Software
Consists of set of programs to control and manage the database. This includes the
DBMS software itself, the Operating System and the network software for sharing
data among users, and application programs for accessing data in the DBMS.

 Data
DBMS exists to collect, store, process and access data. The database contains
both the actual or operational data and the metadata.
Metadata: Data about the data. It provides information or description of the
structure of the database. Ex. Number of Tables and Table Names, Number of
fields and field Names, Primary Key Fields etc.
 Users
A typical DBMS has users with different rights and permissions who use it for
different purposes.
Database Administrators: Responsible for administrating the database. They
create access profiles for users and enforce security. DBAs manage system license,
required tools, and other software and hardware related maintenance.

Designers: Involve in developing and designing the parts of DBMS.

End Users: Access the database for the information to carry out their primary
business responsibilities.

 Procedures
Procedures refer to the instructions and rules that govern the design and use of the
database.
Ex. Instructions and rules on how to:
 Log on to the DBMS.
 Use a particular DBMS facility or application program.
 Start and stop the DBMS.
 Change the structure of a table.
 Handle hardware or software failures.

The Benefits of Using DBMS


 Controlling Data Redundancy and Inconsistency
In non-database systems each application program has its own private files. In this
case, the duplicated copies of the same data is created in many places. DBMS creates
a centralized database to avoid this.

 Improved Data Integrity


DBMS can enforce constraints on data to ensure the accuracy of data. An integrity
constraint is a condition that is enforced automatically by the DBMS and whose
violation prevents the data from being stored in the database.
Ex. Key constraint, Check constraint

 Improved Data sharing


DBMS helps in gaining better and faster access to data as well as better
management of the data. DBMS supports multiple user applications to operate on the
same stored data. It supports concurrent access, recovery from crashes.
 Increased Data security
A DBMS provides a framework for better enforcement of data privacy and
security policies.
Ex. User authorization

 Improved decision making and end-user productivity


Better-managed data and improved data access make it possible to generate
better-quality information.

Structured Query Language (SQL)


 SQL is a computer language for storing, manipulating and retrieving data stored in
databases.
 SQL is the standard language for Relation Database Management Systems.
 All RDBMSs like MySQL, MS Access, Oracle and SQL Server use SQL as the
standard database language.
 SQL can:
 Create new databases
 Create new tables in a database
 Insert records into a table
 Update records in a table
 Delete records from a table
 Retrieve data from a database
 Create views and stored procedures in a database
 Set permissions on tables, procedures and views
SQL Commands
 The standard SQL commands are used to interact with relational databases.
Ex. CREATE, SELECT, INSERT, UPDATE, DELETE and DROP.
 These commands can be mainly classified into three groups based on their
nature.
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Control Language (DCL)
Data Definition Language (DDL)
 The language used to create the database structure.
DDL Commands
 CREATE - Creates a new database, a table, a view of a table, or other objects
 ALTER - Modifies an existing database object, such as a table
 DROP - Deletes an entire database, a table, a view or other objects
Data Manipulation Language (DML)
 The language used to manipulate data in a database.
DML Commands
 INSERT - Creates a record
 UPDATE - Modifies records
 DELETE - Deletes records
 SELECT - Retrieves certain records from one or more tables.

Data Control Language (DCL)


 The language used to control privileges in a database.
DCL Commands
 GRANT - Gives a privilege to user
 REVOKE - Takes back granted privileges from user
SQL Syntax
 SQL follows unique set of rules and guidelines called Syntax.
 SQL uses English keywords & user-defined names.
 All the SQL statements start with any of the keywords like SELECT,
INSERT, UPDATE and DELETE, etc.
 SQL keywords are not case sensitive (By convention, keywords are upper-
case).
 Some database systems require a semicolon at the end of each SQL statement.
 Semicolon is the standard way to terminate and separate SQL statements.
 Text data is enclosed using single quotes.

 SQL CREATE DATABASE Statement


 Used to create a new database.
Syntax:
CREATE DATABASE Database name;
Ex.
CREATE DATABASE School;
 SQL DROP DATABASE Statement
 Used to drop or delete an existing database.
Syntax:
DROP DATABASE Database name;
Ex.
DROP DATABASE School;
 SQL USE Statement
 When there are multiple databases, we need to select a database where all the
operations would be performed.
 The SQL USE statement is used to select any existing database.
Syntax:
USE Database name;
Ex.
USE School;
 SQL CREATE TABLE Statement
 Used to create a new table in a selected database.
Syntax:
CREATE TABLE Table name
(
Column 1 data type,
Column 2 data type,
Column 3 data type,
.....
);
Ex.
CREATE TABLE Student
(
StudentID int,
Name varchar(100),
Address varchar(100),
DOB date
);

 SQL DROP TABLE statement


 Used to remove a table definition and all data, indexes, triggers, constraints,
and permission specifications for that table.
Syntax:
DROP TABLE Table name;
Ex.
DROP TABLE Student;
 SQL ALTER TABLE Statement
 Used to add, delete, or modify columns in an existing table.
To add a column in a table
Syntax:
ALTER TABLE Table name
ADD Column name data type;
Ex.
ALTER TABLE Student
ADD email varchar(50);

To delete a column in a table


Syntax:
ALTER TABLE Table name
DROP COLUMN Column name;
Ex.
ALTER TABLE Student
DROP COLUMN email;

SQL Data Types


 Each column in a database table is required to have a name and a data type.
 The data type is a guideline to understand what type of data is expected inside
of each column.
General Data Types
Data type Description
CHAR Fixed-length Character string

VARCHAR Variable length Character string.

BOOLEAN TRUE or FALSE

INTEGER or INT Integer number (whole numbers)

FLOAT Number with a floating decimal point

DATE Year, month, and day


Format: YYYY-MM-DD
DATETIME A date and time combination.
Format: YYYY-MM-DD HH:MI:SS
SQL Constraints
 Constraints are the rules enforced on data columns on table.
 These are used to limit the type of data that can go into a table.
 This ensures the accuracy and reliability of the data in the database.

 SQL NOT NULL


 The NOT NULL constraint enforces a column to NOT to accept NULL
values.
 By default, a column can hold NULL values.
Ex:
CREATE TABLE Student
(
StudentID int NOT NULL,
Name varchar(100) NOT NULL,
Address varchar(100),
DOB date
);
 SQL PRIMARY KEY
 To create a PRIMARY KEY constraint
Ex.
CREATE TABLE Student
(
StudentID int PRIMARY KEY,
Name varchar(100) NOT NULL,
Address varchar(100),
DOB date
);
Or
CREATE TABLE Student
(
StudentID int,
Name varchar(100) NOT NULL,
Address varchar(100),
DOB date,
PRIMARY KEY (StudentID)
);
 To create a PRIMARY KEY constraint on multiple columns
Ex.
CREATE TABLE Student
(
StudentID int,
Name varchar(100) NOT NULL,
Address varchar(100),
DOB date,
PRIMARY KEY (StudentID,Name)
);

 SQL FOREIGN KEY


 To create a FOREIGN KEY constraint
Ex.
CREATE TABLE Marks
(
StudentID int,
SubjectID int,
Marks int,
PRIMARY KEY (StudentID, SubjectID),
FOREIGN KEY (StudentID) REFERENCES Student (StudentID),
FOREIGN KEY (SubjectID) REFERENCES Subject (SubjectID)
);

 SQL CHECK
 Used to limit the value range that can be entered into a column.
Ex.
CREATE TABLE Marks
(
StudentID int,
SubjectID int,
Marks int CHECK (Marks>=0 AND Marks<=100)
);
 SQL INSERT INTO Statement
 Used to add new records of data to a table in the database.
Syntax:
INSERT INTO Table name (column1, column2, column3,...)
VALUES (value1, value2, value3,...);
Or
INSERT INTO Table name
VALUES (value1, value2, value3,...);
Ex.
INSERT INTO Student (StudentID, Name, Address, DOB)
VALUES (001,’Harry Potter’, ‘4 Privet Drive’, ‘1980-07-31’);

INSERT INTO Student


VALUES (002,’ Draco Malfoy’, ‘Malfoy Manor’, ‘1980-06-05’);

 To insert Data in Specified Columns


Ex.
INSERT INTO Student (StudentID, Name)
VALUES (003, ‘Hermione Granger’);

 SQL UPDATE Statement


 Used to modify the existing records in a table.
Syntax:
UPDATE Table name
SET Column name = new value
WHERE Condition;
Ex.
UPDATE Student
SET Address = ‘Hogwarts’
WHERE StudentID = 001;

 To update several Columns in a Row


Ex.
UPDATE Student
SET Address = ‘Hogwarts’, DOB = ‘1980-07-30’
WHERE StudentID = 001;

 SQL DELETE Statement


 Used to delete existing records in a table.
Syntax:
DELETE FROM Table name
WHERE Condition;
Ex.
DELETE FROM Student
WHERE StudentID = 002;
 To delete all rows in a table without deleting the table.
Ex.
DELETE *
FROM Student;

 The Result Set


 The result from a SQL query is stored in a result-set.
 Most database software systems allow navigation of the result set with
programming functions, like: Move-To-First-Record, Get-Record-Content,
Move-To-Next-Record, etc.

 SQL SELECT Statement


 Used to select data from a table.
Syntax:
SELECT Column name(s)
FROM Table name;
Ex.
SELECT Name, DOB
FROM Student;

 To Select All Columns use a * symbol instead of column names.


Ex.
SELECT *
FROM Student;

 SQL SELECT DISTINCT Statement


 Used to return only distinct (different) values.
Syntax:
SELECT DISTINCT Column name(s)
FROM Table name;

Ex.
SELECT DISTINCT City
FROM Student;
 SQL WHERE Clause
 Used to specify a selection criterion.
Ex.
SELECT Address
FROM Student
WHERE StudentID = 001;

 The WHERE clause can be combined with operators.

 SQL Operators
Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to

SQL LIKE Operator


 Used to specify a search for a pattern in a column by using wildcard operators
within SQL WHERE clause.
 Wildcard operators can be used to define missing letters in the pattern both
before and after the pattern.
 % can represent zero, one or multiple characters.
 _ can represent any single character.
Syntax:
SELECT Column name(s)
FROM Table name
WHERE Column name LIKE Search pattern;

Ex.
Searching Students with first name that starts with an ‘S’
SELECT *
FROM Student
WHERE FirstName LIKE ‘S%’;
Searching Students with first name that ends with an ‘S’
SELECT *
FROM Student
WHERE FirstName LIKE ‘%S’;

Searching Students with first name that contain the pattern 'ba'
SELECT *
FROM Student
WHERE FirstName LIKE '%ba%';

Searching Students with first name that contain five letters


SELECT *
FROM Student
WHERE FirstName LIKE '_ _ _ _ _';

Searching Students with first name that starts with any letter, has an ‘E’
for second letter and ends with a ‘T’
SELECT *
FROM Student
WHERE FirstName LIKE '_ n%s';

SQL AND Operator


 Used to specify more than one condition in SQL WHERE clause.
 Returns record(s) if all the conditions separated by AND are TRUE.
Ex.
SELECT *
FROM Student
WHERE Address = ‘Borella’ AND Class = ‘10C’;
SQL OR Operator
 Used to specify more than one condition in SQL WHERE clause.
 Returns record(s) if any of the conditions separated by OR is TRUE.
Ex.
SELECT *
FROM Student
WHERE Address = ‘Borella’ OR Address = ‘Kottawa;
SQL IN Operator
 Used to specify discrete values in WHERE clause.
 IN operator is a shorthand for multiple OR conditions.
Ex.
SELECT *
FROM Student
WHERE Address IN (‘Borella’ ,‘Kottawa);
SQL BETWEEN Operator
 Used to search for values within a set of values, given the minimum value and
the maximum value.
Ex.
SELECT *
FROM Student
WHERE DOB BETWEEN '2000-01-01' AND '2000-02-01';

SQL ORDER BY
 Used to sort the data in ascending or descending order based on one or more
columns.
Syntax:
SELECT Column name(s)
FROM Table name
WHERE [Condition]
ORDER BY Column name(s) [ASC] or [DESC];

Ex. Sorting student list in ascending order by Student name


SELECT *
FROM Student
ORDER BY StudentName ASC;

Sorting student list in descending order Student name


SELECT *
FROM Student
ORDER BY StudentName DESC;

Sorting student list in ascending order by Class and then by Student name
(2-level ordering)
SELECT *
FROM Student
ORDER BY Class, StudentName;
Joining Multiple Tables
 Used to retrieve information from two or more different tables.
 Combines rows from two or more tables, based on a related column.
Syntax:
SELECT Column name(s)
FROM Table name1, Table name2…
WHERE Table name1. Column name = Table name2. Column name;

Ex.
Student (Student_ID, Student_Name, Class)
Class (Class_ID, Class_Teacher)

Joining Student and Class tables to retrieve Class teacher of Students

SELECT Student.StudentName, Class.ClassTeacher


FROM Student, Class
WHERE Student.Class = Class.ClassID;

SQL JOIN Statement


Syntax:
SELECT Column name(s)
FROM Table 1
JOIN Table 2 ON Table 1.Column name = Table 2.Column name;

Ex. Joining Student and Class tables to retrieve Class teacher of Student with
ID 001.
SELECT Student.StudentName, Class.ClassTeacher
FROM Student JOIN Class
ON Student.Class = Class.ClassID
WHERE Student.Student_ID = 001;

You might also like