0% found this document useful (0 votes)
70 views125 pages

DBMS Module 2

The document provides an overview of the relational data model and SQL. It defines key concepts like relations, tables, keys, and foreign keys. It explains the relational model and how it organizes data into tables with relationships maintained through common fields. It also describes SQL commands for data definition, manipulation, and querying including CREATE, INSERT, SELECT, UPDATE, DELETE. Data types supported in SQL are also summarized.

Uploaded by

Vaidehi Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
70 views125 pages

DBMS Module 2

The document provides an overview of the relational data model and SQL. It defines key concepts like relations, tables, keys, and foreign keys. It explains the relational model and how it organizes data into tables with relationships maintained through common fields. It also describes SQL commands for data definition, manipulation, and querying including CREATE, INSERT, SELECT, UPDATE, DELETE. Data types supported in SQL are also summarized.

Uploaded by

Vaidehi Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 125

Module 2

By:-Prof. Vaidehi Verma


 Relational Model: Relational Data Model - Concept of relations, schema-
instance distinction, keys, referential integrity and foreign keys, relational
algebra operators, SQL -Introduction, data definition in SQL, table, key and
foreign key definitions, update behaviours. Querying in SQL, notion of
aggregation, aggregation functions group by and having clauses.
Representational Data Model

 Describing the logical representation of data without giving details of physical


representation.
 ONLY LOGIC PART
Relational Model

 In this model, data is organized 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
Table1 student-studentid
table 2 subject-sub_id
table 3 marks-marks
 Representational Level Data Model
 Relational Model: Provides the concept of a relation.
 In the context of university database:
Key

 An attribute that can uniquely identify a tuple in a relation is called the key
of the table.
 The value of the attribute for different tuples in the relation has to be
unique.
Types of key

 Candidate key
 Super key
 Primary key
 Alternate key
 Foreign key
CandidateKey
Stu_id name phone city email
100 jhj 7897 ban vj

 Candidate key is a set of attributes (or attribute) which uniquely identify the tuples in a
relation or table.
 Ex. Student{ID, F_name, M_name, L_name, Age}
two candidate keys ID and {First_name, Last_name, DOB}. So here, there are present more
than one candidate keys, which can uniquely identifies a tuple in a relation.

5
Stuid,phone,email
(stuid,name)(stuid phone)(stuid email)
Nc(n/2)=5c2=10
4c2
6c3
Super key

 A super key is an attribute or a set of attributes used to identify the records


uniquely in a relation.
 Empssn,empnum,{empssn,empname},{empssn,empnum},
{empssn,empnum,empname},{empnum,empname}
PrimaryKey

 Primary Key is a set of attributes (or attribute) which uniquely identify the
tuples in a relation or table.
 Primary key is a minimal super key, so there is one and only one primary key
in any relation.
 Ex. Student{ID, F_name, M_name, L_name, Age} Here only ID can be primary
key because name, age and address can be same but ID can’t be same.
Alternate key

 A table can have multiple candidate keys. Among these candidate keys, only
one key gets selected as primary key, the remaining keys are known as
alternative or secondary keys.{stuid,emp_number}
Foreign key

 Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
 Fk is used to link 2 tables together
SQL
 SQL is a database computer language designed for the retrieval
and management of data in a relational database. SQL stands
for Structured Query Language.
Why to Learn SQL?

 SQL is Structured Query Language, which


is a computer language for storing,
manipulating and retrieving data stored in
a relational database.
 SQL is the standard language for
Relational Database System. All the
Relational Database Management Systems
(RDMS) like MySQL, MS Access, Oracle,
Sybase, Informix, Postgres and SQL Server
use SQL as their standard database
language.
Why to Learn SQL?

 Allows users to access data in the relational


database management systems.
 Allows users to describe the data.
 Allows users to define the data in a database
and manipulate that data.
 Allows to embed within other languages using
SQL modules, libraries & pre-compilers.
 Allows users to create and drop databases and
tables.
 Allows users to create view, stored procedure,
functions in a database.
 Allows users to set permissions on tables,
procedures and views.
What is 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.
What is a table?

 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.
Fields and Records
 Fields:

⮚ Every table is broken up into smaller entities called fields. The fields in
the CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.
⮚ A field is a column in a table that is designed to maintain specific
information about every record in the table.
Fields and Records
 Records
⮚ A record is also called as a row of data is each
individual entry that exists in a table.
⮚ For example, there are 7 records in the above
CUSTOMERS table. Following is a single row of
data or record in the CUSTOMERS table −
SQL SYNTAX
 SQL is followed by a unique set of rules and guidelines called
Syntax.
 All the SQL statements start with any of the keywords like SELECT,
INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW and
all the statements end with a semicolon (;).
EMPno JOB Hiredate Sal Deptno
Number(4) Varchar(15 Date Number(5, Number(2)
) 2)
100 Account 21-08-2000 20000.50
1.ER diagram
Entity-Emp Attributes-EMPno,JOB,Hiredate,Sal,Deptno
2.
3.Primary key
4.sales-5char account-8char
5programms
DBMS Languages
DDL

• Create: It is used to create objects in the database.


• Alter: It is used to alter the structure of the database.
• Drop: It is used to delete objects from the database.
• Truncate: It is used to remove all records from a table.
• Rename: It is used to rename an object.
DML

• Select: It is used to retrieve data from a database.


• Insert: It is used to insert data into a table.
• Update: It is used to update existing data within a table.
• Delete: It is used to delete all records from a table.
DCL

• Grant: It is used to give user access privileges to a database.


• Revoke: It is used to take back permissions from the user.
TCL

• Commit: It is used to save the transaction on the database.


• Rollback: It is used to restore the database to original since the last Commit.
SQL DATA TYPE
SQL Data Type is an attribute that specifies the
type of data of any object. Each column, variable
and expression has a related data type in SQL.
You can use these data types while creating your
tables. You can choose a data type for a table
column based on your requirement.
DATA TYPES:
6 major Categories
DATA TYPES
DATA TYPES
DATA TYPES
DATA TYPES
SQL - CREATE Database

 The SQL CREATE DATABASE statement is


used to create a new SQL database.
 Syntax
 The basic syntax of this CREATE DATABASE
statement is as follows −
 CREATE DATABASE DatabaseName;
 Always the database name should be unique
within the RDBMS.
Example
 1) SQL> CREATE DATABASE testDB;
 2) SQL> CREATE DATABASE orig;
SQL DROP DATABASE 
 The SQL DROP DATABASE statement is used to drop an existing
database in SQL schema.
 Syntax:
 The basic syntax of DROP DATABASE statement is as follows −
 DROP DATABASE DatabaseName;
SQL DROP DATABASE 
 If you want to delete an existing database <testDB>, then the
DROP DATABASE statement would be as shown below −
 SQL> DROP DATABASE testDB;
SQL USE DATABASE
 When you have multiple databases in your SQL Schema, then
before starting your operation, you would need to select a
database where all the operations would be performed.

 The SQL USE statement is used to select any existing


database in the SQL schema.

 Syntax
 The basic syntax of the USE statement is as shown below −
 USE DatabaseName;

 Always the database name should be unique within the


RDBMS.
SQL CREATE TABLE
 The SQL CREATE TABLE statement is used
to create a new table.
 Syntax
 The basic syntax of the CREATE TABLE
statement is as follows −
 CREATE TABLE table_name( column1
datatype,
 column2 datatype,
 column3 datatype, .....
 columnN datatype,
 PRIMARY KEY( one or more columns ) );
SQL DROP TABLE
 The SQL DROP TABLE statement is used to remove a table
definition and all the data, indexes, triggers, constraints and
permission specifications for that table.
 The basic syntax of this DROP TABLE statement is as follows −
 DROP TABLE table_name;
SQL INSERT QUERY

 The SQL INSERT INTO Statement is used to


add new rows of data to a table in the
database.

 There are two basic syntaxes of the INSERT


INTO statement which are shown below.
1)INSERT INTO TABLE_NAME
(column1,column2,column3,................columnN)
VALUES (value1, value2, value3,...valueN);

Here, column1, column2, column3,...columnN


are the names of the columns in the table into
which you want to insert the data.
SQL INSERT QUERY
 You may not need to specify the column(s) name in the SQL query if
you are adding values for all the columns of the table. But make
sure the order of the values is in the same order as the columns in
the table.
 The SQL INSERT INTO syntax will be as follows −
2) INSERT INTO TABLE_NAME VALUES(value1,value2,……valueN)
SQL SELECT QUERY
 The SQL SELECT statement is used to fetch the
data from a database table which returns this
data in the form of a result table. These result
tables are called result-sets.

 The basic syntax of the SELECT statement is as


follows −
1)SELECT column1, column2, columnN FROM
table_name;
 Here, column1, column2... are the fields of a
table whose values you want to fetch. If you
want to fetch all the fields available in the
field, then you can use the following syntax.
2)SELECT * FROM table_name;
SQL WHERE CLAUSE
 The SQL WHERE clause is used to specify
a condition while fetching the data from
a single table or by joining with multiple
tables. If the given condition is satisfied,
then only it returns a specific value from
the table. You should use the WHERE
clause to filter the records and fetching
only the necessary records.
 The WHERE clause is not only used in the
SELECT statement, but it is also used in
the UPDATE, DELETE statement, etc.,
SQL WHERE CLAUSE
 Syntax
 The basic syntax of the SELECT statement with the WHERE clause
is as shown below.
 SELECT column1, column2, columnN FROM table_name WHERE
[condition] You can specify a condition using the 
comparison or logical operators like >, <, =, LIKE, NOT, etc
SQL WHERE CLAUSE
 The following code is an example which would fetch the ID, Name
and Salary fields from the CUSTOMERS table, where the salary is
greater than 20000 −
 SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY >
20000;
 SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE NAME =
‘Mohan';
SQL AND and OR operator
SQL AND OPERATOR
SQL AND OPERATOR
 SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY >
20000 AND age < 25;
SQL OR OPERATOR
SQL OR OPERATOR
 SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY >
20000 OR age < 25;
SQL UPDATE QUERY
 The SQL UPDATE Query is used to modify the existing records in a
table. You can use the WHERE clause with the UPDATE query to
update the selected rows, otherwise all the rows would be
affected.
SQL UPDATE QUERY
SQL UPDATE QUERY
 The following query will update the ADDRESS for a customer whose
ID number is 6 in the table.
 SQL> UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;

 SQL>UPDATE CUSTOMERS SET ADDRESS = 'Pune', SALARY =


10000.00;
SQL DELETE QUERY WITH WHERE
CLAUSE
 The SQL DELETE Query is used to delete the existing records from a
table.
 You can use the WHERE clause with a DELETE query to delete the
selected rows, otherwise all the records would be deleted.
SQL DELETE QUERY WITH WHERE
CLAUSE
SQL DELETE QUERY WITH WHERE
CLAUSE
 SQL> DELETE FROM CUSTOMERS WHERE ID = 6;
SQL LIKE CLAUSE
 The SQL LIKE clause is used to compare
a value to similar values using wildcard
operators. There are two wildcards used
in conjunction with the LIKE operator.
 The percent sign (%)
 The underscore (_)
 The percent sign represents zero, one or
multiple characters.
 The underscore represents a single
number or character. These symbols can
be used in combinations.
SQL LIKE CLAUSE
 Syntax:
SELECT FROM table_name
WHERE column LIKE 'XXXX%'
Or
SELECT FROM table_name WHERE column
LIKE '%XXXX%'
or
SELECT FROM table_name WHERE column
LIKE 'XXXX_'
or
SELECT FROM table_name WHERE column
LIKE '_XXXX'
SQL LIKE CLAUSE
SQL LIKE CLAUSE
SQL LIKE CLAUSE
SQL ORDER BY
SQL ORDER BY
SQL ORDER BY
SQL ORDER BY
SQL GROUP BY
SQL GROUP BY
SQL GROUP BY
SQL GROUP BY
SQL GROUP BY
SQL DISTINCT keyword
SQL NULL
SQL NULL

 You must use the IS NULL or IS NOT NULL operators to check for a


NULL value.
SQL TOP CLAUSE
SQL ALTER TABLE
SQL CONSTRAINTS
SQL CONSTRAINTS
SQL JOINS
 The SQL Joins clause is used to combine records from two or more
tables in a database. A JOIN is a means for combining fields from
two tables by using values common to each.
SQL JOINS
SQL JOINS
SQL INNER JOIN
  The most important and frequently used of the
joins is the INNER JOIN. They are also referred to
as an EQUIJOIN.

 returns rows when there is a match in both tables.

 The INNER JOIN creates a new result table by


combining column values of two tables (table1 and
table2) based upon the join-predicate. The query
compares each row of table1 with each row of
table2 to find all pairs of rows which satisfy the
join-predicate. When the join-predicate is
satisfied, column values for each matched pair of
rows of A and B are combined into a result row.
SQL INNER JOIN
SQL LEFT JOIN
 The SQL LEFT JOIN returns all rows from
the left table, even if there are no
matches in the right table. This means
that if the ON clause matches 0 (zero)
records in the right table; the join will
still return a row in the result, but with
NULL in each column from the right table.
 This means that a left join returns all the
values from the left table, plus matched
values from the right table or NULL in
case of no matching join predicate.
SQL LEFT JOIN
SQL LEFT JOIN
SQL RIGHT JOIN
 The SQL RIGHT JOIN returns all rows from
the right table, even if there are no
matches in the left table. This means that
if the ON clause matches 0 (zero) records
in the left table; the join will still return a
row in the result, but with NULL in each
column from the left table.
 This means that a right join returns all the
values from the right table, plus matched
values from the left table or NULL in case
of no matching join predicate.
SQL RIGHT JOIN
SQL RIGHT JOIN
SQL FULL JOIN
 The SQL FULL JOIN combines the results of both left and right
outer joins.
 The joined table will contain all records from both the tables and
fill in NULLs for missing matches on either side.
SQL FULL JOIN
SQL FULL JOIN
SQL FULL JOIN –UNION ALL
SQL SELF JOIN
 The SQL SELF JOIN is used to join a table to itself as if the table
were two tables; temporarily renaming at least one table in the
SQL statement.
SQL SELF JOIN
SQL SELF JOIN
SQL SELF JOIN
SQL SELF JOIN
SQL CARTESIAN JOIN
 The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product
of the sets of records from two or more joined tables. Thus, it
equates to an inner join where the join-condition always evaluates
to either True or where the join-condition is absent from the
statement.
SQL CARTESIAN JOIN
SQL CARTESIAN JOIN
SQL CARTESIAN JOIN
 SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS, ORDERS;
SQL CARTESIAN JOIN
SQL USING VIEWS
 A view is nothing more than a SQL statement that is
stored in the database with an associated name. A view is
actually a composition of a table in the form of a
predefined SQL query.
 A view can contain all rows of a table or select rows from
a table. A view can be created from one or many tables
which depends on the written SQL query to create a view.
 Views, which are a type of virtual tables allow users to do
the following −
 Structure data in a way that users or classes of users find
natural or intuitive.
 Restrict access to the data in such a way that a user can
see and (sometimes) modify exactly what they need and
no more.
 Summarize data from various tables which can be used to
generate reports.
SQL USING VIEWS
SQL using views
SQL AGGREGATION FUNCTION
COUNT FUNCTION
1)SELECT COUNT(*)  
FROM PRODUCTS;  

2) SELECT COUNT(*)  
FROM PRODUCTS;  
WHERE RATE>=20; 

3) SELECT COUNT(DISTINCT COMPANY)  FROM PRODUCTS;   
4)SELECT COMPANY, COUNT(*)  
FROM PRODUCTS  
GROUP BY COMPANY;  

5) SELECT COMPANY, COUNT(*)  
FROM PRODUCTS  
GROUP BY COMPANY  
HAVING COUNT(*)>2;  
2. SUM Function

 Sum function is used to calculate the sum of all selected columns.


It works on numeric fields only.
3. AVG function

 The AVG function is used to calculate the average value of the


numeric type. AVG function returns the average of all non-Null
values.
4. MAX Function

 MAX function is used to find the maximum value of a certain


column. This function determines the largest value of all selected
values of a column.
5. MIN Function

 MIN function is used to find the minimum value of a certain


column. This function determines the smallest value of all selected
values of a column

You might also like