Structured Query
Language
RDBMS
RDBMS stands
for Relational Database Management System.
RDBMS is the basis for SQL, and for all
modern database systems like MS SQL Server,
IBM DB2, Oracle, MySQL, and Microsoft
Access.
A Relational database management system
(RDBMS) is a database management system
(DBMS) that is based on the relational model
as introduced by E. F. Codd.
Tables
The data in an RDBMS is stored in database
objects which are called as tables. This table is
basically a collection of related data entries and
it consists of numerous columns and rows.
Table structure Emp Attendence
ID Name Age Address Salary
101 John 21 Delhi 26000
102 John 21 Delhi 26000
103
Some Concept
What is a NULL value?
A NULL value in a table is a value in a field
that appears to be blank, which means a field
with a NULL value is a field with no value.
It is very important to understand that a NULL
value is different than a zero value or a field
that contains spaces. A field with a NULL
value is the one that has been left blank
during a record creation.
Some Concept
SQL Constraints
Constraints are the rules enforced on data columns on a 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.
Constraints can either be column level or table level. Column level constraints are
applied only to one column whereas, table level constraints are applied to the entire
table.
Following are some of the most commonly used constraints available in SQL −
NOT NULL Constraint − Ensures that a column cannot have a NULL value.
DEFAULT Constraint − Provides a default value for a column when none is specified.
UNIQUE Constraint − Ensures that all the values in a column are different.
PRIMARY Key − Uniquely identifies each row/record in a database table.
FOREIGN Key − Uniquely identifies a row/record in any another database table.
CHECK Constraint − The CHECK constraint ensures that all values in a column satisfy
certain conditions.
INDEX − Used to create and retrieve data from the database very quickly.
Some Concept
Data Integrity
The following categories of data integrity exist with each
RDBMS −
Entity Integrity − There are no duplicate rows in a table.
Domain Integrity − Enforces valid entries for a given
column by restricting the type, the format, or the range
of values.
Referential integrity − Rows cannot be deleted, which are
used by other records.
User-Defined Integrity − Enforces some specific business
rules that do not fall into entity, domain or referential
integrity.
Structured Query Languge
SQL is divided into four categories as
mentioned below:
DDL – Data Definition Language
DML – Data Manipulation Language
DCL – Data Control Language
TCL – Transaction Control Language
DDL
DDL(Data Definition Language) : DDL or Data Definition
Language actually consists of the SQL commands that can be
used to define the database schema.
Examples of DDL commands:
CREATE – is used to create the database or its objects (like
table, index, function, views, store procedure and triggers).
DROP – is used to delete objects from the database.
ALTER-is used to alter the structure of the database.
TRUNCATE–is used to remove all records from a table,
including all spaces allocated for the records are removed.
COMMENT –is used to add comments to the data dictionary.
RENAME –is used to rename an object existing in the database.
DML
DML is used for performing queries on the data
within schema objects.
E.g. SELECT
DML(Data Manipulation Language) : The SQL
commands that deals with the manipulation of data
present in the database belong to DML or Data
Manipulation Language and this includes most of the
SQL statements.Examples of DML:
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a
table.
DELETE – is used to delete records from a database
table.
SQL Joins
A relational database consists of multiple related tables
linking together using common columns which are known
as foreign key columns.
A join is a method of linking data between one (self-join)
or more tables based on values of the common column
between the tables.
MySQL supports the following types of joins:
Inner join
Left join
Right join
Cross join
Inner Join
The inner join clause compares each row from the
first table with every row from the second table. If
values in both rows cause the join condition
evaluates to true, the inner join clause creates a new
row whose column contains all columns of the two
rows from both tables and include this new row in
the final result set. In other words, the inner join
clause includes only rows whose values match.
The following shows the basic syntax of the inner
join clause that joins two tables table_1 and table_2:
E.g. SELECT column_list FROM table_1 INNER JOIN
table_2 ON join_condition;
Left Join
In other words, the left join selects all data
from the left table whether there are
matching rows exist in the right table or not.
In case there is no matching rows from the
right table found, NULLs are used for
columns of the row from the right table in the
final result set.
SELECT column_list FROM table_1 LEFT JOIN
table_2 ON join_condition;
Views
Views are database objects which donot store
data physically. When we execute the SELECT
statement against the view, MySQL executes the
underlying query specified in the view’s
definition and returns the result set. For this
reason, sometimes, a view is referred to as a
virtual table.
Views can be created with CREATE View
command, can be updated with CREATE OR
REPLACE View command and can be deleted with
Drop View <View name> statement.
Stored Procedure
A procedure (often called a stored procedure) is a
subroutine like a subprogram in a regular computing
language, stored in database. A procedure has a name,
a parameter list, and SQL statement(s). Almost all
relational database system supports stored procedure.
Advantages:
Stored procedures are fast as they are compiled once.
Stored procedures are portable. When you write your
stored procedure in SQL, you know that it will run on
every platform that MySQL
Stored procedures are always available as 'source code'
in the database itself.
Stored Procedure
Parameters used in Stored Procedure
IN parameters
OUT parameter
INOUT parameter
DISTINCT and IN
The SELECT DISTINCT command returns only
distinct (different) values in the result set.
E.g.
SELECT DISTINCT Prod_id FROM TransMaster;
IN: The IN operator allows us to specify multiple
values in a WHERE clause. The IN operator is a
shorthand for multiple OR conditions.
E.g. SELECT * FROM TransMaster
WHERE Prod_id IN (1,2,3);
Database Normalization
Normalization is a database design technique
that reduces data redundancy and eliminates
undesirable characteristics like Insertion,
Update and Deletion Anomalies. Normalization
rules divides larger tables into smaller tables
and links them using relationships. The
purpose of Normalization in SQL is to eliminate
redundant (repetitive) data and ensure data is
stored logically.
Database Normalization
Here is a list of Normal Forms
1NF (First Normal Form)
2NF (Second Normal Form)
3NF (Third Normal Form)
BCNF (Boyce-Codd Normal Form)
1st Normal Form
1NF (First Normal Form) Rules
Each table cell should contain a single value.
Each record needs to be unique.
Primary key
It has following attributes
A primary key cannot be NULL
A primary key value must be unique
The primary key values should rarely be
changed
The primary key must be given a value when a
new record is inserted.
2NF (Second Normal Form) Rules
Rule 1- Be in 1NF
Rule 2- Single Column Primary Key
It is clear that we can't move forward to make
our simple database in 2nd Normalization
form unless we partition the table above.
2NF (Second Normal Form) Rules Continued
Another table
Third normal Form
Rule 1- Be in 2NF
Rule 2- Has no transitive functional
dependencies
Third Normal Form
Third Normal Form
Types of DBMS
Database Management System (DBMS) is a software
for storing and retrieving users' data while
considering appropriate security measures.
Four Types of DBMS systems are:
Hierarchical database.
Network database.
Relational database.
Object-Oriented database.
DBMS
Hierarchical DBMS
In a Hierarchical database, model data is organized in a tree-like
structure. Data is Stored Hierarchically (top down or bottom up) format.
Data is represented using a parent-child relationship. In Hierarchical
DBMS parent may have many children, but children have only one
parent.
Network Model
The network database model allows each child to have multiple
parents. It helps you to address the need to model more complex
relationships like as the orders/parts many-to-many relationship. In
this model, entities are organized in a graph which can be accessed
through several paths.
Relational model
Relational DBMS is the most widely used DBMS model because it is one
of the easiest. This model is based on normalizing data in the rows and
columns of the tables. Relational model stored in fixed structures and
manipulated using SQL.
DBMS
Object-Oriented Model
In Object-oriented Model data stored in the
form of objects. The structure which is called
classes which display data within it. It defines a
database as a collection of objects which stores
both data members values and operations.
Thank You
- Amlan Dey