Samson Dbms (R23) UNIT-II
Samson Dbms (R23) UNIT-II
UNIT-II
Name Of The Programme B.Tech-CSE
Regulations R-23
CSE Department
UNIT II
Relational Model: Introduction to relational model, concepts of domain, attribute, tuple,
relation, importance of null values, constraints (Domain, Key constraints, integrity
constraints) and their importance, Relational Algebra, Relational Calculus.
BASIC SQL: Simple Database schema, data types, table definitions (create, alter), different
DML operations (insert, delete, update).
The table name and column names are helpful to interpret the meaning of values in each row.
The data are represented as a set of relations. In the relational model, data are stored as tables.
However, the physical storage of the data is independent of the way the data are logically
organized.
Domain
• Columns in table have a unique name, often referred as attributes in DBMS. A domain is
a unique set of values permitted for an attribute in a table. For example, a domain of
month-of-year can accept January, February….December as possible values, a domain of
integers can accept whole numbers that are negative, positive and zero.
• Domain: It contains a set of atomic values that an attribute can take.
1. Domain Constraints
2. Entity Integrity Constraints
3. Key Constraints
4. Referential Integrity Constraints
Domain Constraints
Domain constraints can be violated if an attribute value is not appearing in the corresponding
domain or it is not of the appropriate data type.
Domain constraints specify that within each tuple, and the value of each attribute must be
unique. This is specified as data types which include standard data types integers, real numbers,
characters, Booleans, variable length strings, etc.
Example:
Domain Constraints are Data type constraints, length constraints, Not null constraints…
Key Constraints
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.
Example:
In the given table, CustomerID is a primary key attribute of Customer Table. It is most likely to
have a single key for one customer, CustomerID =1 is only for the CustomerName =" Google".
A Unique Key ensures that the values in a column are unique, but unlike a primary key, it allows
one NULL value.
Employee_ID Email Name
1 [email protected] Aniket Kumar
2 NULL Shashwat Dubey
3 stalin@ gmail.com Manvendra Sharma
Unique Values: The email column must contain unique values.
[email protected] and stalin@ gmail.com are valid.
Adding another row with [email protected] would result in an error.
Allows One NULL: The email column can contain one NULL value.
They enforce that a foreign key in one table must either match a value in the referenced
primary key of another table or be NULL.
This guarantees the logical connection between related tables in a relational database.
Referential Integrity constraints in DBMS are based on the concept of Foreign Keys.
NULL values could be possible because at the time of data entry information is not available. So
SQL supports a special value known as NULL which is used to represent the values of attributes
that may be unknown or not apply to a tuple.
SQL places a NULL value in the field in the absence of a user-defined value.
For example, the Apartment_number attribute of an address applies only to addresses that are in
apartment buildings and not to other types of residences.
So, NULL values are those values in which there is no data value in the particular field in the
table.
Importance of NULL Value
It is important to understand that a NULL value differs from a zero value.
A NULL value is used to represent a missing value, but it usually has one of three
different interpretations:
The value unknown (value exists but is not known)
Value not available (exists but is purposely withheld)
Attribute not applicable (undefined for this tuple)
It is often not possible to determine which of the meanings is intended. Hence, SQL does
not distinguish between the different meanings of NULL.
RELATIONAL ALGEBRA
Relational algebra is a procedural query language, which takes instances of relations as input and
yields instances of relations as output. It uses operators to perform queries. An operator can be
either unary or binary. They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate results are also
considered relations.
The fundamental operations of relational algebra are as follows −
Select
Project
Union
Set-different
Cartesian product
Join
Rename
Selects and projects columns named as subject and author from the relation Books..
Notation:
rΧs
Example:
Consider two relations STUDENT(SNO, FNAME, LNAME) and DETAIL(ROLLNO, AGE)
below:
STUDENT table:
SNO FNAME LNAME
1 Albert Smith
2 Pearl Weber
DETAIL table:
ROLLNO AGE
5 18
9 21
On applying CROSS PRODUCT on STUDENT and DETAIL,
STUDENT ✕ DETAILS
we get:
SNO FNAME LNAME ROLLNO AGE
1 Albert Smith 5 18
1 Albert Smith 9 21
2 Pearl Weber 5 18
2 Pearl Weber 9 21
In general, we don’t use cartesian Product unnecessarily, which means without proper meaning
we don’t use Cartesian Product. Generally, we use Cartesian Product followed by a Selection
operation and comparison on the operators as shown below :
σ A=D (A ✕ B)
Join Operations:
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Example 2: Project EMP_NAME and SALARY from two table based on their EMP_CODE.
Π EMPNAME, SALARY (EMPLOYEE ⋈ SALARY)
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
RELATIONAL CALCULUS
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that is,
it tells what to do but never explains how to do it.
Relational calculus exists in two forms:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)
Existential Quantifier: ∃T ∈ R(Cond) will succeed if Cond succeeds for at least one tuple in T.
Universal Quantifier: ∀T ∈ R(Cond) will succeed if Cond succeeds for at all tuples in T.
Applications of SQL
As mentioned before, SQL is one of the most widely used query language over the databases.
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.
SQL databases:
Microsoft SQL
MySQL
Oracle
PostgreSQL
MSSQL
DATABASE SCHEMA
A schema is the blueprint or structure that defines how data is organized and stored in a database.
It outlines the tables, fields, relationships, views, indexes, and other elements within the
database. The schema defines the logical view of the entire database and specifies the rules that
govern the data, including its types, constraints, and relationships.
DATA TYPES
1. CHAR(Size): This data type is used to store character string values of fixed length. The
maximum number of character is 255 characters.
3. NUMBER(P, S): The NUMBER data type is used to store number (fixed or floating
point). P is precision and S is scale.
The precision is the number of digits in a number. It ranges from 1 to 38.
The scale is the number of digits to the right of the decimal point in a number. It ranges
from -84 to 127.
For example, the number 1234.56 has a precision of 6 and a scale of 2. So to store this number,
you need NUMBER(6,2).
P and S are optional.
4. INT(size) or INTEGER(size) , FLOAT, DOUBLE, DECIMAL are also used
5. DATE: This data type is used to represent date and time. The standard format is DD-
MMM- YYYY as in 17-SEP-2009.
6. RAW: The RAW data type is used to store binary data, such as digitized picture or
image.
7. BOOL or BOOLEAN : True or false
ALTER COMMAND:
ALTER TABLE is used to alter the structure of the database by add Columns, modify size and
delete an empty column
DROP COMMAND:
DROP– is used to delete objects from the database. It is DDL Command.
DROP TABLE:
A DROP TABLE statement is used to delete a table structure and all data from a table. We
cannot rollback.
TRUNCATE COMMAND:
TRUNCATE TABLE – is used to delete all data from a table. It is a DDL command.
.
Syntax: TRUNCATE TABLE tableName;
Example: SQL> TRUNCATE TABLE student;
INSERT COMMAND:
INSERT is used insert the data into the table. It is DML command.
Syntax:
INSERT into tablename(Column1, Column2..) values (value1,value2,…);
Column1, Column2.. : Specify the column names only when to insert data for specific column
values for the record.
Example:
Create table with table name “teacher” and insert records and display the records.
SQL> create table teacher ( id number(3), name varchar2(10), age number(3));
SQL> insert into teacher values(1, 'A.RAO', 30);
SQL> insert into teacher (id , name) values(2, 'B.RAO');
Inserted row with teacher id and name only.
UPDATE COMMAND:
UPDATE command is used to set the new data to the columns. It is DML command.
Syntax:
UPDATE tablename SET Column1=new value, Column2=new value .. where condition;
Example: Change the name of teacher to ‘ARJUN RAO’ whose name is ‘A.RAO’
SQL> UPDATE teacher SET name=’ARJUN RAO’ where name=’A.RAO’;
DELETE COMMAND:
DELETE command is used to delete a specific single record or group of records or all records
based on the given condition in WHERE clause. It is DML command.
It delete the records temporarily from the table. We can rollback the deleted records before
COMMIT command.
Syntax:
DELETE from tablename where condition;
Example: Delete a teacher record whose id=3;
SQL> DELETE from teacher where id=3;
Example: Delete a teacher record whose name is ‘B.RAO’
SQL>DELETE from teacher where name=’B.RAO’;
In this SELECT syntax, Column_Name1, Column_Name2, ….., Column_NameN are the name
of those columns in the table whose data we want to read.
If you want to access all rows from all fields of the table, use the following SELECT syntax with
* asterisk sign: