DBMS Module - 2
DBMS Module - 2
Relational Model
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?
▪ 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?
Fields:
Every table is broken up into smaller entities
called fields. The fields in the CUSTOMERS
table consist of ID, NAME, AGE, ADDRESS.
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.
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 (;).
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
Syntax
▪ The basic syntax of the USE statement is as shown below −
▪ USE DatabaseName;
(column1,column2,column3,................columnN)
VALUES (value1, value2, value3,...valueN);
Syntax:
3. AVG function
⚫ The AVG function is used to calculate the
5. MIN Function
⚫ MIN function is used to find the minimum
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
⚫ Point-02:
•Selection operator always selects the entire tuple. It can not select a section
or part of a tuple.
Point-04:
•Degree of the relation from a selection operation is same as degree of the input
relation.
Point-06:
•The number of rows returned by a selection operation is obviously less than or equal
to the number of rows in the original table.
Thus,
•Minimum Cardinality = 0
•Maximum Cardinality = |R|
Ex:Selects the records from EMPLOYEE table with
department ID = 20 and employees whose salary is more
than 10000.
σdept_id = 20 AND salary>=10000 (EMPLOYEE)
•The degree of output relation (number of columns present) is equal to the number of
attributes mentioned in the attribute list.
Point-02:
•Projection operator automatically removes all the duplicates while projecting the
output relation.
•So, cardinality of the original relation and output relation may or may not be same.
•If there are no duplicates in the original relation, then the cardinality will remain
same otherwise it will surely reduce.
Point-03:
•If attribute list is a super key on relation R, then we will always get the same number
of tuples in the output relation.
•This is because then there will be no duplicates to filter.
Point-04:
Point-06:
Point-07:
Ex:
To rename the columns of the table. If the
STUDENT table has ID, NAME and ADDRESS
columns and if they have to be renamed to
STD_ID, STD_NAME, STD_ADDRESS, then we
have to write as follows.
attributes.
• Attribute domains must be compatible.
eliminated.
We have two courses table and we want to perform the
union operation on table Course_1(C_id, C_name) and
Course_2(C_id, C_name). Remember, we can perform
union operation only because both the tables have same
attribute.
Intersection Operator (∩)
⚫ Intersection operator is denoted by ∩ symbol and
it is used to select common rows (tuples) from two
tables (relations).
⚫ Lets say we have two relations R1 and R2 both
have same columns and we want to select all those
tuples(rows) that are present in both the relations,
then in that case we can apply intersection
operation on these two relations R1 ∩ R2.
Set Difference (-)