0% found this document useful (0 votes)
43 views30 pages

DBMS Unit 2

The document provides an overview of the relational model, including key concepts such as domain, attribute, tuple, and relation, along with the significance of null values and various constraints in database management. It also introduces basic SQL operations for creating and modifying database schemas, as well as performing data manipulation. Additionally, it covers relational algebra, detailing fundamental operations like selection, projection, union, and set difference.

Uploaded by

maneeshgopisetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views30 pages

DBMS Unit 2

The document provides an overview of the relational model, including key concepts such as domain, attribute, tuple, and relation, along with the significance of null values and various constraints in database management. It also introduces basic SQL operations for creating and modifying database schemas, as well as performing data manipulation. Additionally, it covers relational algebra, detailing fundamental operations like selection, projection, union, and set difference.

Uploaded by

maneeshgopisetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

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)

Introduction
● Relational Model was proposed by E.F Codd to model data in the form of relations or tables.
● After designing the conceptual model of database using ER diagram, we need to convert the
conceptual model in the relational model which can be implemented using any RDBMS
(Relational Data Base Management System) like SQL, MY SQL etc.
● The relational model is very simple and elegant; a database is a collection of one or more
relations, where each relation is a table with rows and columns.
● This simple tabular representation enables even new users to understand the contents of a
database, and it permits the use of simple, high-level languages to query the data.
************
Relational Model
● Relational Model represents how date is stored in relational databases. A Relational database
stores data in the form of relations (tables).
● Consider a relation STUDENT with attributes ROLL_NO, NAME, ADDRESS,
PHONE and AGE as shown in table.
ROLL_N NAME ADDRESS PHONE AGE
O
1 A Hyderabad 9455123451 28
2 B Guntur 9652431843 27
3 C Nellore 9156253131 26
4 D Ongole 9215635311 25

1
● Attribute: Attributes are the properties that define
a relation. Ex: ROLL_NO, NAME
● Tuple: Each row in a relation is known as tuple.
Ex:
1 A Hyderaba 94551234 28
d 51
● Degree: The number of attributes in the relation is known as degree.
Ex: The degree of the given STUDENT table is 5.
● Column: Column represents the set of values for a particular
attribute. The column ROLL_NO is extracted from the
relation STUDENT.
Ex:
ROLL_NO

1
● Null values: The value which is not known or unavailable is called NULL VALUE.
It is represented by blank space.
● Cardinality: The number of tuples is present in the relation is called as its cardinality.
Ex: The Cardinality of the STUDENT table is 4.
************
Concept of Domain
● The domain of a database is the set of all allowable values (or) attributes of the database.
Ex: Gender (Male, Female, Others).
Relation
● A relation is defined as a set of tuples and attributes.
● A relation consists of Relation schema and relation instance.
● Relation schema: A relation schema represents the name of the relation with its attributes.
● Ex: STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is
Relation schema for STUDENT.
Emp_co Emp_Na Dept_Na
de me me
01234 John HR
12567 Smith Sales
21678 Sai Production
12456 Jay Design

2
Importance of Null values:
⮚ 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.
⮚ For example, the apartment_number attribute of an address applies only to the address that is in
apartment buildings and not to other types of residences.
⮚ It is important to understand that a NULL value is different from Zero value.
⮚ A Null value is used to represent a missing value, but that is usually has one of the following
interpretations:
● Value unknown (Value exists but it is unknown)
● Value not available (exists but it is purposely withheld)
● Attribute not applicable (undefined for this tuple)
************
Constraints
⮚ On modeling the design of the relational data base, we can put some rules(conditions) like what
values are allowed to be inserted in the relation
⮚ Constraints are the rules enforced on the data columns of a table. These are used to limit the type
of data that can go in to a table
⮚ This Ensure the accuracy and reliability of the data in the database. Constraints could be either
on a column level on a table level.
Domain Constraints In DBMS
⮚ In DBMS table is viewed as a combination of rows and columns
⮚ For example, if you are having a column called month and you want only (jan, feb,
march……) as values allowed to be entered for that particular column which is
referred to as domain for that particular column
Definition:
Domain constraint ensures two things it makes sure that the data value entered for that
particular column matches with the data type defined by that column. It shows that the
constraints (NOT NULL/UNIQUE/PRIMARY KEY/FOREIGNKEY/CHECK/DEFAULT)

3
Domain constraint= data type check for the column +constraints.
Example:, we want to create a table “STUDENT” with “stu_id” field having a value
greater than 100, can create a domain and table like this.
▪ Create domain id_value int constraint id_test check (value>=100);
▪ CREATE table STUDENT (stu_id id value primary key, stu_name varchar (30),
stu_age int);
************
Key constraints in DBMS:
⮚ Constraints are nothing but the rules that are to be followed while entering data into
columns of the database table.
⮚ Constraints ensure that the data entered by the user into columns must be within the criteria
specified by the condition.
⮚ We have 6 types of key constraints in DBMS
1. Not Null
2. Unique
3. Default
4. Check
5. Primary key
6. Foreign key
1. Not Null:
 Null represents a record where data may be missing data or data for that record may be
optional.
 Once not null is applied to a particular column, you cannot enter null values to that column.
 A not null constraint cannot be applied at table level.

4
 Example :
Create table EMPLOYEE (roll_no number(5) Not null, name varchar2(20) Not null, Age
number(5) not null, phone number(15));

2. Unique:
 Sometimes we need to maintain only. Unique data in the column of a database table, this is
possible by using a Unique constraint.
 Unique constraint ensures that all values in a column are Unique.
Example:
Create table EMPLOYEE (roll_no number(5) UNIQUE, name varchar2(20) Not null, Age
number(5) not null, phone number(15));

Default:
● Default in SQL is used to add default data to the columns.
●When a column is specified as default with same value then all the rows will use the same
value i.e., each and every time while entering the data we need not enter that value.
●But default column value can be customized i.e., it can be over ridden when inserting a data for
that row based on the requirement.
(Row with default values “abc”)

5
Example:
Create table EMP(id_no number(5) primary key, name varchar2(20), company
varchar2(20) default abc);
 As a result, whenever you insert a new row each time you need not enter a value for thisdefault
column that is entering a column value for a default column is optional.
3. Check:
 Check constraint ensures that the data entered by the user for that column is within the range of
values or possible values specified.
Example:
Create table STUDENT (roll_no number(5) primary key, name varchar2(25), age number(3)
check(age>=18));

4. Primary Key:
 A primary key is a constraint in a table which uniquely identifies each row record in a
database table by enabling one or more column in the table as primary key.
Example:
Create table EMP (roll_no number(5) primary key, name varchar(20), age number(5), gpa
number(5));

6
5. Foreign Key:
 The foreign key constraint is a column or list of columns which points to the
primary keycolumn of another table.
 The main purpose of the foreign key is only those values are allowed in the present
table thatwill match to the primary key column of another table.

Example:
(Reference Table)
Create table CUSTOMER1 (roll_no number(5) primary key, name varchar2(20), course_id
number(5));
(Child table)
Create table CUSTOMER2 (course_id number(5), marks number(5) references customer1(ID));
***********
Integrity Constraints in DBMS:
There are two types of integrity constraints
1. Entity Integrity Constraints
2. Referential Integrity Constraints
1. Entity Integrity constraints:
●These constraints are used to ensure the uniqueness of each record or row in the data
table.
●Entity Integrity constraints says that no primary key can take NULL VALUE, since
using primary key we identify each tuple uniquely in a relation.
Example:

7
2. Referential Integrity constraints:
● The referential integrity constraint is specified between two relations or tables and usedto
maintain the consistency among the tuples in two relations.
● This constraint is enforced through foreign key, when an attribute in the foreign key of
relation R1 have the same domain as primary key of relation R2, then the foreign key
ofR1 is said to reference or refer to the primary key of relation R2.
● The values of the foreign key in a tuple of relation R1 can either take the values of the
primary key for some tuple in Relation R2, or can take NULL values, but can‟t be empty.

Explanation:

● In the above, DNO of the first relation is the foreign key and DNO in the second
relation is the primary key
● DNO=22 in the foreign key of the first relation is not available in the second
relation so, since DNO=22 is not defined in the primary key of the second relation
therefore Referential integrity constraints is violated here.
*************

RELATIONAL ALGEBRA

⮚ Relational algebra is one of the two formal query languages associated with the relational model.
Queries in algebra are composed using a collection of operators.
⮚ A fundamental property is that every operator in the algebra accepts (one or two) relation instances as
arguments and returns a relation instance as the result. This property makes it easy to compose
operators to form a complex query.
⮚ Relational Algebra Expression is recursively defined to be a relation, a unary algebra operator
applied to a single expression, or a binary algebra operator applied to two expressions.
⮚ The basic operators of the algebra (selection, projection, union, cross- product, and difference.
⮚ Each relational query describes a step-by-step procedure for computing the desired answer, based on

8
the order in which operators are applied in the query. The procedural nature of the algebra allows us to
think of an algebra expression as a plan, for evaluating a query, and relational systems in fact use
algebra expressions to represent query evaluation plans.
The operators in relational algebra are classified

SELECT (σ)
The SELECT operation is used for selecting a subset of the tuples according to a given selection condition. It is
denoted by sigma (σ).

Notation: σ p(r)
Where: σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and NOT. These relational
can use as relational operators like =, ≠, ≥, <, >, ≤.
Example 1
σ topic = "Database" (Tutorials)
Output - Selects tuples from Tutorials where topic = 'Database'.
Example 2
σ topic = "Database" and author = "Raghuramakrishna"( Tutorials)
Output - Selects tuples from Tutorials where the topic is 'Database' and 'author' is Raghuramakrishna
Example 3
σ sales > 50000 (Customers)
Output - Selects tuples from Customers where sales is greater than 50000 For example: LOAN Relation

9
BRANCH_NAME LOAN_NO AMOUN
T
Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300

Input:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUN
T
Perryride L-15 1500
Perryride L-16 1300

Projection(π)
• The projection eliminates all attributes of the input relation but those mentioned in the projection list.
• This helps to extract the values of specified attributes to eliminates duplicate values.
• (pi) The symbol used to choose attributes from a relation. This operation helps you to keep specific columns
from a relation and discards the other columns.
Notation: ∏ A1, A2, An (r)
Where A1, A2, A3 is used as an attribute name of relation r.
Example of Projection:
Consider the following table
CustomerID CustomerName Status
1 Google Active
2 Amazon Active
3 Apple Inactiv
e
4 Alibaba Active

Here, the projection of CustomerName and status will give

1
Π CustomerName, Status (Customers)
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active

Union operation (U)


UNION is symbolized by ∪ symbol. It includes all tuples that are in tables A or in B. It also eliminates
duplicate tuples. So, set A UNION set B would be expressed as:
Notation: A∪B
R and S must have the attribute of the same number. Duplicate tuples are eliminated automatically.
For a union operation to be valid, the following conditions must hold -
● R and S must be the same number of attributes.
● Attribute domains need to be compatible.
● Duplicate tuples should be automatically removed.
Example
Consider the following tables.

Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3

A ∪ B gives
Table A ∪ B

column 1 column 2

1 1

1 2

1
1 3

Set Difference (-)


- Symbol denotes it. The result of A - B, is a relation which includes all tuples that are in A but not in B.
⮚ The attribute name of A has to match with the attribute name in B.
⮚ The two-operand relations A and B should be either compatible or Union compatible.
⮚ It should be defined relation consisting of the tuples that are in relation A, but not in B.
Example
Consider the following tables.
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3

A-B
Table A - B
column 1 column 2
1 2

Intersection Operator (∩)


Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples) from two tables
(relations).
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.
Syntax of Intersection Operator (∩)
table_name1 ∩ table_name2
Example

1
Query:
∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)

Student_Name

Aditya
Steve
Paul
Lucy

Cartesian product (X)


● Cartesian Product is denoted by X symbol.
● For example we have two relations R1 and R2 then the cartesian product of these two relations (R1 X R2)
would combine each tuple of first relation R1 with the each tuple of second relation R2.
Syntax of Cartesian product (X)
R1 X R2
Example

cartesian product of table R and S R X S

1
Rename (ρ)
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.

Syntax:
ρ(new_relation_name, old_relation_name)
Example
Lets say we have a table customer, we are fetching customer names and we are renaming the resulted relation
to CUST_NAMES.
Table: CUSTOMER

Customer_Id Customer_Na Customer_Ci


me ty
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:

CUST_NAMES

Steve
Raghu
Chaitanya
Ajeet
Carl

1
Join Operations
● The join operation is one of the most useful operations in relational algebra and is the most commonly used
way to combine information from two or more relations.
● Join can be defined as a cross-product followed by selections and projections.
● The result of a cross-product is typically much larger than the result of a join there are several variants of the
join operation.
⮚ Inner joins
⮚ Outer joins
Inner Join
• An inner join is the widely used join operation and can be considered as a default join-type.
• The inner JOIN is used to return rows from both tables which satisfy the given condition.
• An Inner join or equijoin which uses equality comparisons in the join- predicate. If we use other comparison
operators like ">" can't be called equijoin.
Inner Join further divided into three subtypes:
● Theta join
● Natural join
● EQUI join

Theta Join
Theta Join allows you to merge two tables based on the condition represented by theta. Theta joins work for all
comparison operators.
It is denoted by symbol θ
Syntax: A ⋈θ B
Theta join can use any conditions in the selection criteria.
Consider the following tables.
Table A Table B
column column column column
1 2 1 2
1 1 1 1
1 2 1 3

For example:
A ⋈ A.column 2 < B.column 2 (B)

column 1 column 2

1
1 2

EQUI join:
When a theta join uses only equivalence condition, it becomes an equi join.
For example:
A ⋈ A.column 2 = B.column 2 (B)
column 1 column 2
1 1

Natural Join
Natural join is a type of inner join in which we do not need any comparison operators. In natural join,
columns should have the same name and domain. There should be at least one common attribute between the two
tables.
Example: Consider the following two tables

C ⋈ DNum
C D
Num
C ⋈Square
D Cube
2 2
Num Square Cube
2 4 8
3 9 18
Outer Join
An outer join doesn't require each record in the two join tables to have a matching record. In this type of join,
the table retains each record even if no other matching record exists.
Three types of Outer Joins are:
● Left Outer Join
● Right Outer Join
● Full Outer Join
Left Outer Join (A B)
The LEFT JOIN returns all the rows from the table on the left even if no matching rows have been found in
the table on the right. Where no matching record found in the table on the right, NULL is returned.

Consider the following 2 Tables

A B
Num 1 Num
Square Cube
A⋈B
A B

Num Square Cube


2 4 4
3 9 9
4 16 -

Right Outer Join ( )


RIGHT outer JOIN is the opposite of LEFT JOIN. The RIGHT JOIN returns all the columns from the table
on the right even if no matching rows have been found in the table on the left. Where no matches have been found in
the table on the left, NULL is returned

A⋈B
Num Cube Square
2 8 4
3 18 9
5 75 -

Full Outer Join (A B)


In a full outer join, all tuples from both relations are included in the result, irrespective of the matching condition.
Example:

A⋈B
Num Cube Square
2 4 8
3 9 18
4 16 -
5 - 75

RELATIONAL CALCULUS:
Relational calculus is a non-procedural query language. In the non-procedural query language, the user is
concerned with the details of how to obtain the end results. The relational calculus tells what to do but never explains
how to do.
Types of Relational calculus:

1
Tuple Relational Calculus (TRC)
The tuple relational calculus is specified to select the tuples in a relation. The result of the relation can have one or
more tuples.
Notation: {T | P (T)} or {T | Condition (T)}
Where
T is the resulting tuples

P(T) is the condition used to fetch T.


For example:
{T.name | Author(T) AND T.article = 'database' }
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a tuple with 'name' from Author
who has written an article on 'database'.
{t | TEACHER (t) and t.SALARY>20000}
It implies that it selects the tuples from the TEACHER in such a way that the resulting teacher tuples will
have a salary higher than 20000.

Domain Relational Calculus (DRC)


In domain relational calculus, filtering variable uses the domain of attributes. Domain relational calculus uses the
same operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Notation:
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
a1, a2 are attributes
P stands for formula built by inner attributes
For example:
{< article, page, subject > | ∈ cse ∧ subject = 'database'}

1
Output: This query will yield the article, page, and subject from the relational cse, where the subject is a database.
Consider the “Students” table:

I
Name Age Grade
D

1 Alice 20 A

2 Bob 19 B

3 Charlie 22 A

4 David 18 C

Example 1: Find the Names of Students with Grade ‘A’


TRC Query:
{ t | t ∈ Students ∧ t.Grade = ‘A’ }
DRC Query:
{<Name> | ∃ ID, Age, Grade (Students(ID, Name, Age, Grade) ∧ Grade = ‘A’)}
Example 2: Find the Names and Ages of Students Older Than 18
TRC Query:
{ t | t ∈ Students ∧ t.Age > 18 }
DRC Query:
{<Name, Age> | ∃ ID, Grade (Students(ID, Name, Age, Grade) ∧ Age > 18)}

1
Basic SQL (introduction)
 SQL stands for Structure Query Language it is used for storing and managing data in relational
database management system.
 It is standard language for relational database system. It enables a user to create, read, update
and delete relational databases and tables.
 All the RDBMS like MYSQL, Oracle, MA access and SQL Server use SQL as their
standarddatabase language.
 SQL allows users to Query the database in a number of ways using statements like
common English.
Rules: SQL follows following rules
● SQL is not a case sensitive. Generally, keywords are represented
in UPPERCASE.
● Using the SQL statements, you can perform most of the actions in a database.
● Statements of SQL are dependent on text lines. We can use a single SQL
statement on one or multiple text line.
SQL Process:
 When an SQL command is executing for any RDBMS, then the system figure out the best way
to carry out the request and the sql engine determines that how to interrupt the task.
 In the process, various components are included. These components can be
optimization engine, query engine, query dispatcher etc.,
 All the non-sql queries are handled by the classic query engine, but sql query engine
handle logical files.
Characteristics of SQL:
 SQL is easy to learn.
 SQL is used to access data from relational database management system.
 SQL is used to describe the data.
 SQL is used to create and drop the database and table.
 SQL allows users to set permissions on tables, procedures and views.
**************

2
SQL Commands:
● SQL commands are instructions. It is used to communicate with the database. It is also used to
perform specific tasks, functions, and queries of data.
● SQL can perform various tasks like create a table, add data to tables, drop the table, modify the
table, set permission for users.
● SQL Commands are five types: DDL, DML, DCL, TCL, and DQL.

1. Data Definition Language (DDL)


● DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc.
● All the command of DDL are auto-committed that means it permanently save all the changes in the
database.
● Here are some commands that come under DDL:
a) CREATE
b) ALTER
c) DROP
d) TRUNCATE
a. CREATE: It is used to create a new table in the database.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,... ]);
Example:
CREATE TABLE EMP
(Name VARCHAR2(20), Class VARCHAR2(100), DOB VARCHAR2(20));

2
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE ;
Example
DROP TABLE EMPLOYEE;
c. ALTER: It is used to alter the structure of the database. This change could be either to modify the
characteristics of an existing attribute or probably to add a new attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
To modify existing column in the table:
ALTER TABLE MODIFY(COLUMN DEFINITION... );
EXAMPLE
ALTE TAB STU_DETAI ADD(ADDRESS VARCHAR2(20));
R LE LS
ALTE TAB STU_DETAI MODIFY (NAME
R LE LS VARCHAR2(20));
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
2. Data Manipulation Language
● DML commands are used to modify the database. It is responsible for all form of changes in the
database.
● The command of DML is not auto-committed that means it can't permanently save all the changes
in the database. They can be rollback.
● Here are some commands that come under DML:
a) INSERT
b) UPDATE
c) DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME
(col1, col2, col3, col N)

2
VALUES (value1, value2, value3, valueN);

2
Or
INSERT INTO TABLE_NAME
VALUES (&col1, &col2, &col3, &colN);
For example:
INSERT INTO emp (name, job) VALUES ("Rams",
"Manager"); Or
INSERT INTO EMP VALUES(&EMPNO, ‘&ENAME’ , ‘&JOB’);
Enter value for empno : 001
Enter value for ename : Rams
Enter value for job: Manager
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name
SET [column_name1= value1, column_nameN = valueN]
[WHERE CONDITION]
For example:
UPDATE students
SET User_Name = 'Sonu'
WHERE Student_Id =
'3' ;
c. DELETE: It is used to remove one or more row from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
For example:
DELETE FROM student
WHERE sname="Sonu";
3. Data Control Language
 DCL commands are used to grant and take back authority from any database user.
 Here are some commands that come under DCL:
a) Grant
b) Revoke
a. Grant: It is used to give user access privileges to a database.
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

2
b. Revoke: It is used to take back permissions from the user.

2
Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
4. Transaction Control Language
 TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.
 These operations are automatically committed in the database that's why they cannot be used while
creating tables or dropping them.
 Here are some commands that come under TCL:
a) COMMIT
b) ROLLBACK
c) SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already been saved to the
database.
Syntax:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire
transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;

2
5. Data Query Language
● DQL is used to fetch the data from the database.
● It uses only one command:
SELECT
SELECT: This is the same as the projection operation of relational algebra. It is used to select the attribute
based on the condition described by WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT emp_name
FROM employee
WHERE age > 20;
************
SQL Data Types:

2
 SQL Datatype is used to define the values that a column can contain.
 Every column is required to have a name and data type in the database table.
Data type of SQL:

1. Binary Datatypes
There are Three types of binary Datatypes which are given below:

Data Type Description

Binary It has a maximum length of 8000 bytes. It contains fixed-length binary data.

varbinar It has a maximum length of 8000 bytes. It contains variable-length binary data.
y
Image It has a maximum length of 2,147,483,647 bytes. It contains variable-length binary data

2
2. Approximate Numeric Datatype:
The subtypes are given below:

Data type From To Description

Float -1.79E + 308 1.79E + 308 It is used to specify a floating-point value e.g. 6.2, 2.9 etc.

Real -3.40e + 38 3.40E + 38 It specifies a single precision floating point number

3. Exact Numeric Datatype


The subtypes are given below:

Data type Description

Int It is used to specify an integer value.

Smallint It is used to specify small integer value.

Bit It has the number of bits to store.

Decimal It specifies a numeric value that can have a decimal number.

Numeric It is used to specify a numeric value.

4. Character String Datatype


The subtypes are given below:

Data type Description

Char It has a maximum length of 8000 characters. It contains Fixed-length non-unicode


characters.

Varchar It has a maximum length of 8000 characters. It contains variable-length non-unicode


characters.

Text It has a maximum length of 2,147,483,647 characters. It contains variable-length non-


unicode characters.

15
5. Date and time Datatypes
The subtypes are given below:

Datatype Description

Date It is used to store the year, month, and days value.

Time It is used to store the hour, minute, and second values.

Timestamp It stores the year, month, day, hour, minute, and the second value.

**************

You might also like