Professional Documents
Culture Documents
Unit 2 DBMS
Unit 2 DBMS
Unit 2 DBMS
POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
1 Ajeet 24 B.Tech
Name
Aryan
Mahesh
Ratan
Vimal
8
• BIGINT − A large integer that can be signed or unsigned. If signed, the allowable
range is from -9223372036854775808 to 9223372036854775807. If unsigned, the
allowable range is from 0 to 18446744073709551615. You can specify a width of up
to 20 digits.
• FLOAT(M,D) − A floating-point number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D). This is not required and will
default to 10,2, where 2 is the number of decimals and 10 is the total number of
digits (including decimals). Decimal precision can go to 24 places for a FLOAT.
e.g1234567890.11
• DOUBLE(M,D) − A double precision floating-point number that cannot be unsigned.
You can define the display length (M) and the number of decimals (D). This is not
required and will default to 16,4, where 4 is the number of decimals. Decimal
precision can go to 53 places for a DOUBLE. REAL is a synonym for DOUBLE.
e.g123456789013456.0000
• DECIMAL(M,D) − An unpacked floating-point number that cannot be unsigned. In
the unpacked decimals, each decimal corresponds to one byte. Defining the display
length (M) and the number of decimals (D) is required. NUMERIC is a synonym for
DECIMAL.
9
• Date And Time
• DATE − A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-
31. For example, December 30th, 1973 would be stored as 1973-12-30.
• DATETIME − A date and time combination in YYYY-MM-DD HH:MM:SS
format, between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For
example, 3:30 in the afternoon on December 30th, 1973 would be stored
as 1973-12-30 15:30:00.
• TIMESTAMP − A timestamp between midnight, January 1st, 1970 and
sometime in 2037. This looks like the previous DATETIME format, only
without the hyphens between numbers; 3:30 in the afternoon on
December 30th, 1973 would be stored as 19731230153000
( YYYYMMDDHHMMSS ).
• TIME − Stores the time in a HH:MM:SS format.
• YEAR(M) − Stores a year in a 2-digit or a 4-digit format. If the length is
specified as 2 (for example YEAR(2)).
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Miss.I.B.Tirse 10
• String Type
• VARCHAR(M) − A variable-length string between 1 and 255 characters in
length. For example, VARCHAR(25). You must define a length when creating a
VARCHAR field.
• BLOB or TEXT − A field with a maximum length of 65535 characters. BLOBs are
"Binary Large Objects" and are used to store large amounts of binary data,
such as images or other types of files.
• Fields defined as TEXT also hold large amounts of data. The difference
between the two is that the sorts and comparisons on the stored data are case
sensitive on BLOBs and are not case sensitive in TEXT fields. You do not specify
a length with BLOB or TEXT.
• ENUM − When defining an ENUM, you are creating a list of items from which
the value must be selected (or it can be NULL).
• For example, if you wanted your field to contain "A" or "B" or "C", you would
define your ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could
ever populate that field.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Miss.I.B.Tirse 11
• EF Codd Rules
• Rule 0:
• This rule states that for a system to qualify as an RDBMS, it must be
able to manage database entirely through the relational capabilities.
• Rule 1: Information Rule
• All the information in the database should be represented in the
term of relational or table. Information should be stored as an values
in a tables.
• Rule 2: Guaranteed Access Rule
• All data must be accessible. The Rule say that there is fundamental
requirement of primay key for each record in table , and there should
be no ambiguity by stating the table name and its primary key of the
each record in the table along with columns name to be accessed.
abc@gmail.co
1 Tom Computer
m
xyz@gmail.co
2 Nick Computer m
xyz@gmail.co
2 12 Nick Wright
m
mno@yahoo.c
3 13 Dana Natan om
<StudentInfo> <ProjectInfo>
• Non-prime attributes: In the given table, all attributes except EMP_ID are
non-prime.
• Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP
dependent on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY)
transitively dependent on super key(EMP_ID). It violates the rule of third
normal form.
• That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Miss.I.B.Tirse 38
• EMPLOYEE table:
EMP_ID EMP_NAME EMP_ZIP
222 Harry 201010
333 Stephan 02228
444 Lan 60007
555 Katharine 06389
666 John 462007
• EMPLOYEE_ZIP table:
42
• 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:
• CREATE
• ALTER
• DROP
• TRUNCATE
43
a.CREATE It is used to create a new table in the database
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DO
B DATE);
b. DROP: It is used to delete both the structure and record stored in the
table.
Syntax:
• DROP TABLE table_name;
• Example
• DROP TABLE EMPLOYEE;
44
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 table_name MODIFY(column_definitions....);
• EXAMPLE
• ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
• ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
45
• d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
• Truncate command deletes the data inside a table, but not the table
itself.
• Syntax:
TRUNCATE TABLE table_name;
• Example:
• TRUNCATE TABLE EMPLOYEE;
46
• 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.
(undo transcations)
• Here are some commands that come under DML:INSERT, UPDATE,
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) VALUES (value1, valu
e2, value3, .... valueN);
OR
• INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
47
• For example:
• INSERT INTO javatpoint (Author, Subject) VALUES (“ISRD
Group", "DBMS");
• 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 = val
ueN] [WHERE CONDITION]
• For example:
• UPDATE students SET User_Name = ‘Sunita' 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 javatpoint WHERE Author=“ISRD Group"; 48
• 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:
• Grant
• Revoke
• a. Grant: It is used to give user access privileges to a database. This
command also allows users to grant permissions to other users too.
• Example
• GRANT insert, select on account to Ram
• By the above command user ram has granted permissions on accounts
database object like he can insert into accounts
• b. Revoke: It is used to take back permissions from the user.
• Example
• Revoke insert, select on account to Ram
49
• 4. Transaction Control Language
• Transcation control language commands are used to manage transcation in database.
• TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.
• Here are some commands that come under TCL:
• COMMIT
• ROLLBACK
• SAVEPOINT
• a. Commit: Commit command is used to save all the transactions permanently to the
database.
• Syntax:
• COMMIT;
• Example:
• DELETE FROM CUSTOMERS WHERE AGE = 25;
• COMMIT;
50
• b. Rollback: Rollback command is used to undo transactions that have
not already been saved to the database.(undo change)
• 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.(to save temporarily)
• Syntax:
• SAVEPOINT SAVEPOINT_NAME;
51
• 5. Data Query Language
• DQL is used to fetch the data from the database.
• It uses only one command:
• SELECT
• a. SELECT:It retrieve records from one or more table. 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;
• SELECT * from student;
52
Constraint
Constrains that control data insertion and data retrieval speed are known as I/O
(Input-Output) constraints.
Constraints could be either on a column level or a table level. The column level
constraints are applied only to one column, whereas the table level constraints are
applied to the whole table.
• It includes the following constraints.
• Primary Key, Foreign Key, Unique Key, Not Null, Primary Key
• Primary key is used to identify a record uniquely from the database table.
• A primary key means UNIQUE + NOT NULL.
• Value must be available for the column on which primary key has been defined.
• User cannot leave the field blank.
• It cannot contain duplicate values.
• Primary key can be defined either at table level or at column level.
• Primary key keyword is used to define primary key constraint
53
Here is the syntax to define the ID attribute as a primary key in a
CUSTOMERS table:
CREATE TABLE CUSTOMERS( ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );
55
Foreign Key
// Parent table definition
CREATE TABLE student ( roll_no number (3) primary key, name
varchar2 (15), city varchar2 (15) );
// child table definition
CREATE TABLE stud_detail ( roll_no number (3) references student
(roll_no), address varchar2 (30), city varchar2 (15) );
56
• Referential integrity
• Referential integrity say the column which contain foreign key in one
table must be primary key of another table In general term, Foreign key
of Table A must be Primary key of Table B.
• Example Customer Table
57
• Not Null
• We can define not null constraint when we do not want the user to leave
the field blank.
• When any column contains not null constraint, the user must have to
insert value for the column and it cannot be left blank.
• However it allows duplicate value.
• A table can contain not null constraint on multiple columns.
• Not null constraint can only be defined at column level that means it
cannot be defined at table level.
• ‘Not Null’ keyword is used to define not null constraint.
• Example:
CREATE TABLE student ( roll_no number (3) Primary key, name varchar2 (15)
not null, mob number (15) not null );
58
• SQL Comparison Operators
• Assume 'variable a' holds 10 and 'variable b' holds 20, then
Operator Description Example
Checks if the values of two operands are equal or not, if yes
= then condition becomes true. (a = b) is not true.
Checks if the values of two operands are equal or not, if
!= values are not equal then condition becomes true. (a != b) is true.
Checks if the value of left operand is greater than the value
> of right operand, if yes then condition becomes true. (a > b) is not true.
59
• SQL Logical Operators
Logical
Description
Operators
OR For the row to be selected at least one of the
conditions must be true.
For a row to be selected all the specified
AND
conditions must be true.
NOT For a row to be selected the specified
condition must be false.
61
• "NOT" Logical Operator:
• If you want to find rows that do not satisfy a condition, you can use the
logical operator, NOT. NOT results in the reverse of a condition. That is, if
a condition is satisfied, then the row is not returned.
• For example: If you want to find out the names of the students who do
not play football, the query would be like:
• SELECT first_name, last_name, games
FROM student_details
WHERE NOT games = 'Football‘
• The output would be something like,
62
Operator Description
AND The AND operator in SQL is used to compare data with more than one
condition. If all the conditions return TRUE then only it will display
records.
ALL The ALL operator in SQL returns true when value matches all values in
a single column set of values. It’s like AND operator it will compare the
value against all values in column.
ANY The Any operator in SQL returns true when the value matches any
value in single column set of values. It’s like an OR operator and it will
compare value against any value in the column.
LIKE The LIKE operator in SQL is used to search for character string with the
specified pattern using wildcards in a column.
63
IN The IN operator in SQL is used to search for specified value matches
any value in set of multiple values.
64
SQL Arithmetic Operators
Operator Description Example
65
• Set Operation
• SQL supports few Set operations which can be performed on the table
data.
• 4 different types of set operation
• UNION
• UNION ALL
• INTERSECT
• MINUS
1.Union
• UNION is used to combine the results of two or more SELECT statements.
66
• The First table,
ID Name
1 Abhi
2 Ashish
• The Second table,
ID Name
2 Ashish
3 Adesh
D NAME
1 abhi
2 Ashish
2 Ashish
3 adesh
68
• INTERSECT
• Intersect operation is used to combine two SELECT statements, but it
only returns the records which are common from both SELECT
statements. In case of Intersect the number of columns and datatype
must be same.
• NOTE: MySQL does not support INTERSECT operator.
• MINUS
• The Minus operation combines results of two SELECT statements and
return only those in the final result, which belongs to the first set of the
result.
69
References
Introduction To Database Management Systems, ISRD Group
70
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Miss.I.B.Tirse 71