Ls 7 - Relational Database and SQL 12 A C 2024 - 2025
Ls 7 - Relational Database and SQL 12 A C 2024 - 2025
Lesson – 7
Relational Database & SQL
ADVANTAGES OF DATABASE
• Eliminates data redundancy ( Duplication of data is avoided)
• Provides data consistency (By Propagating Updates – the changes made to data are reflected automatically)
• Sharing of Data (same data may be shared among users for different purposes)
• Improves data integrity (By range checks , value matching to(Emp ID) , formats(Date))
• Enables privacy and security (ensured by access authorization checks)
• Improved backup and recovery system
• Enforces standardised database (Company / Industry/ National / International Standards are to be followed;
Standardization enables Data Migration)
COMPONENTS OF A DATABASE
Any person who can Manages the database and
access the database is seeks to meet the needs of
called Database user the database users.
This model uses a tree like structure to A network database is a collection of Data is stored in the form of tables in
represent relationship among nodes and links, such that any node which rows are instances and columns
records. can be linked to any other node as fields.
multiple times.
7
RELATIONAL DATABASE
• RELATIONAL DATABASE .
• A relational model consists of a collection of tables, each of which is assigned a unique
name, i.e., it represents the database as a collection of relations or tables
• The most popular data model as it is scientific and stable model than hierarchical or network
model.
• In this model, data is organized in two-dimensional tables called relations. The tables or
relations are related to each other.
• A relational database is based on this relational model developed by E.F. Codd.
• In this type of database, the data and relations between them are organized into tables, having
logically related records containing the same fields. The contents of a table or relation can be
permanently saved for future use. Thus, this model is simple and has all the properties and
capabilities required to process data with storage efficiency.
Emp_id
• Portable
• High speed
• Easy to learn & understand
• Used for RDBMS
• Acts as both programming language & interactive language
• Provides Client / Server Architecture
• Supports Object based programming
WHAT IS SQL?
Structured Query Language (SQL) is a language that enables you to create, modify and
access information from Relational Database Management Systems and are also used to
create interface between a user and database
CONSTRIANTS
The restrictions that are imposed on the table at the creation level to control the
insertion, updation or deletion operation done on table.
• NOT NULL – The column must not assume a null value (cannot be empty).
• UNIQUE – No two rows can have the same value in the specified column.
• PRIMARY KEY – A combination of UNIQUE & NOT NULL constraint. Only
one Primary Key per table is allowed.
NB :
• DEFAULT – Default value can be specified for the column. Eg: School char(6) Default=‘SIS Br’
• CHECK – Limits the value that can be inserted. Eg:- age INT CHECK(age<18)
Eg: -
Eg: -
2. CREATING TABLES
Multiple conditions
35
The DISTINCT keyword eliminates duplicate rows from the results of a SELECT statement.
Eg: - All names ending in ‘A’ Eg: - All names ending in ‘N’
Eg: - All names with character ‘r’. Eg: - All names with 2 characters before ‘n’.
OR
TRUNCATE TABLE <tablename> ;
12. MODIFYING THE TABLE STRUCTURE : ALTER - This command is used to modify the definition of a
table by modifying the definition of its columns.
a) Adding a new column.
ALTER TABLE <tablename>
ADD(<column_name><datatype>);
b) Deleting a column.
ALTER TABLE <tablename>
DROP COLUMN <columnname> ;
• MIN() Returns the minimum value in the given column or set of values
• AVG() Returns the average value of data in the given column or set of values
• COUNT() Returns the total number of values / records as per the given column
NULL values are ignored by all the aggregate functions (Except count(*))
• Used to apply a SQL SELECT query on a group of records instead of whole table.
• A grouped column is generally that column of the table which has repeating values.
• Used to apply a SQL SELECT query on a group of records i.e after the GROUP BY
• A grouped column is generally that column of the table which has repeating values.
SQL JOIN
➢ An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.
➢ While querying for a join, more than one table is considered in FROM clause.
EXAMPLE
• SELECT NAME,GNAME FROM STUDENT,GAMES;
EQUI JOIN
➢ An Equi join is a simple SQL join condition that uses the equal to sign (=) as a comparison
operator for defining a relationship between two tables on the basis of a common field.
➢ Syntax for Equi Join: SELECT <COLUMN1>, <COLUMN2> ….
FROM <TABLE1> <TABLE2>
WHERE <TABLE1.COLUMN1> = <TABLE2.COLUMN1> ;
NATURAL JOIN
• A natural join is a type of join that combines tables based on common columns
(same name and type) in both tables.
• Features:
• There is no need to specify the column names to join.
• The resultant table does not contain repeated columns.
• It is possible to perform a natural join on more than two tables.
➢ Syntax : SELECT <COLUMN1>, <COLUMN2> ….
FROM <TABLE1> NATURAL JOIN <TABLE2>;