DBMS Unit 5
DBMS Unit 5
SQL
SQL (Structured Query Language) is used to perform operations on the records stored in the
database such as updating records, deleting records, creating and modifying tables, views, etc.
SQL is just a query language; it is not a database. To perform SQL queries, you need to install
any database, for example, Oracle, MySQL, MongoDB, PostGre SQL, SQL Server, DB2, etc.
What is SQL?
SQL stands for Structured Query Language.
It is designed for managing data in a relational database management system (RDBMS).
It is pronounced as S-Q-L or sometime See-Qwell.
SQL is a database language, it is used for database creation, deletion, fetching rows, and
modifying rows, etc.
SQL is based on relational algebra and tuple relational calculus.
All DBMS like MySQL, Oracle, MS Access, Sybase, Informix, PostgreSQL, and SQL
Server use SQL as standard database language.
1
Database Management System (CS/IT 4th SEM) unit 5
2
Database Management System (CS/IT 4th SEM) unit 5
A list of data types used in MySQL database. This is based on MySQL 8.0.
CHAR(Size) It is used to specify a fixed length string that can contain numbers,
letters, and special characters. Its size can be 0 to 255 characters.
Default is 1.
VARCHAR(Size) It is used to specify a variable length string that can contain numbers,
letters, and special characters. Its size can be from 0 to 65535
characters.
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size parameter
specifies the column length in the bytes. Default is 1.
VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its size
parameter specifies the maximum column length in bytes.
TEXT(Size) It holds a string that can contain a maximum length of 255 characters.
3
Database Management System (CS/IT 4th SEM) unit 5
ENUM(val1, val2, It is used when a string object having only one value, chosen from a
val3,...) list of possible values. It contains 65535 values in an ENUM list. If
you insert a value that is not in the list, a blank value will be inserted.
SET( It is used to specify a string that can have 0 or more values, chosen
val1,val2,val3,....) from a list of possible values. You can list up to 64 values at one time
in a SET list.
BLOB(size) It is used for BLOBs (Binary Large Objects). It can hold up to 65,535
bytes.
BIT(Size) It is used for a bit-value type. The number of bits per value is specified
in size. Its size can be 1 to 64. The default value is 1.
INT(size) It is used for the integer value. Its signed range varies from -
2147483648 to 2147483647 and unsigned range varies from 0 to
4294967295. The size parameter specifies the max display width that
is 255.
DOUBLE(size, d) It is a normal size floating point number. Its size parameter specifies
the total number of digits. The number of digits after the decimal is
4
Database Management System (CS/IT 4th SEM) unit 5
specified by d parameter.
DECIMAL(size, d) It is used to specify a fixed point number. Its size parameter specifies
the total number of digits. The number of digits after the decimal
parameter is specified by d parameter. The maximum value for the
size is 65, and the default value is 10. The maximum value for d is 30,
and the default value is 0.
BOOL It is used to specify Boolean values true and false. Zero is considered
as false, and nonzero values are considered as true.
DATETIME(fsp) It is used to specify date and time combination. Its format is YYYY-
MM-DD hh:mm:ss. Its supported range is from '1000-01-01 00:00:00'
to 9999-12-31 23:59:59'.
TIMESTAMP(fsp) It is used to specify the timestamp. Its value is stored as the number of
seconds since the Unix epoch('1970-01-01 00:00:00' UTC). Its format
is YYYY-MM-DD hh:mm:ss. Its supported range is from '1970-01-01
00:00:01' UTC to '2038-01-09 03:14:07' UTC.
TIME(fsp) It is used to specify the time format. Its format is hh:mm:ss. Its
supported range is from '-838:59:59' to '838:59:59'
5
Database Management System (CS/IT 4th SEM) unit 5
SQL Operators
SQL statements generally contain some reserved words or characters that are used to perform
operations such as comparison and arithmetical operations etc. These reserved words or
characters are known as operators.
Generally there are three types of operators in SQL:
SQL Arithmetic Operators
SQL Comparison Operators
SQL Logical Operators
SQL Arithmetic Operators:
Let's assume two variables "a" and "b". Here "a" is valued 50 and "b" valued 100.
Example:
+ It is used to add containing values of both operands a+b will give 150
- It subtracts right hand operand from left hand operand a-b will give -50
/ It divides left hand operand by right hand operand b/a will give 2
% It divides left hand operand by right hand operand and b%a will give 0
returns reminder
= Examine both operands value that are equal or not,if yes (a=b) is not
condition become true. true
!= This is used to check the value of both operands equal or not,if (a!=b) is true
not condition become true.
6
Database Management System (CS/IT 4th SEM) unit 5
<> Examines the operand's value equal or not, if values are not (a<>b) is true
equal condition is true
> Examine the left operand value is greater than right Operand, if (a>b) is not
yes condition becomes true true
< Examines the left operand value is less than right Operand, if (a<="" td="">
yes condition becomes true
>= Examines that the value of left operand is greater than or equal (a>=b) is not
to the value of right operand or not,if yes condition become true
true
<= Examines that the value of left operand is less than or equal to (a<=b) is true
the value of right operand or not, if yes condition becomes true
!< Examines that the left operand value is not less than the right (a!<=""
operand value td="">
!> Examines that the value of left operand is not greater than the (a!>b) is true
value of right operand
SQL Logical Operators:
This is the list of logical operators used in SQL.
Operator Description
ALL This is used to compare a value to all values in another value set.
ANY This operator is used to compare the value in list according to the condition.
BETWEEN this operator is used to search for values, that are within a set of values
NOT the NOT operator reverse the meaning of any logical operator
7
Database Management System (CS/IT 4th SEM) unit 5
EXISTS the EXISTS operator is used to search for the presence of a row in a specified
table
LIKE this operator is used to compare a value to similar values using wildcard
operator
If you want to add tables in that database, you can use CREATE TABLE statement.
SQL DROP statement is used to delete or remove indexes from a table in the database.
If you want to delete or drop an existing database in a SQL schema, you can use SQL DROP
DATABASE
Let's see the syntax of SQL DROP DATABASE:
DROP DATABASE database_name;
8
Database Management System (CS/IT 4th SEM) unit 5
SQL Table
Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is
known as relation and row as tuple.
Table is the simple form of data storage. A table is also considered as a convenient
representation of relations.
Let's see an example of an employee table:
Employee
9
Database Management System (CS/IT 4th SEM) unit 5
It is a variable where we temporary store records and results. This is same like temp table but in
the case of temp table we need to explicitly drop it.
Table variables are used to store a set of records. So declaration syntax generally looks like
CREATE TABLE syntax.
create table "tablename"
("column1" "data type",
"column2" "data type",
...
"columnN" "data type");
When a transaction rolled back the data associated with table variable is not rolled back.
A table variable generally uses lesser resources than a temporary variable.
Table variable cannot be used as an input or an output parameter.
SQL Table
Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is
known as relation and row as tuple.
Table is the simple form of data storage. A table is also considered as a convenient
representation of relations.
Let's see an example of an employee table:
In the above table, "Employee" is the table name, "EMP_NAME", "ADDRESS" and "SALARY"
are the column names. The combination of data of multiple columns forms a row e.g. "Ankit",
"Lucknow" and 15000 are the data of one row.
The SQL Table variable is used to create, modify, rename, copy and delete tables. Table
variable was introduced by Microsoft.
10
Database Management System (CS/IT 4th SEM) unit 5
The data type of the columns may vary from one database to another. For example, NUMBER is
supported in Oracle database for integer value whereas INT is supported in MySQL.
Let us take an example to create a STUDENTS table with ID as primary key and NOT NULL
are the constraint showing that these fields cannot be NULL while creating records in the table.
SQL> CREATE TABLE STUDENTS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
11
Database Management System (CS/IT 4th SEM) unit 5
You can verify it, if you have created the table successfully by looking at the message displayed
by the SQL Server, else you can use DESC command as follows:
SQL> DESC STUDENTS;
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
Now you have the STUDENTS table available in your database and you can use to store required
information related to students.
12
Database Management System (CS/IT 4th SEM) unit 5
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
13
Database Management System (CS/IT 4th SEM) unit 5
14
Database Management System (CS/IT 4th SEM) unit 5
RENAME TO new_table_name;
Optionally, you can write following command to rename the table.
RENAME old_table _name To new_table_name;
Let us take an example of a table named "STUDENTS", now due to some reason we want to
change it into table name "ARTISTS".
Table1: students
15