Professional Documents
Culture Documents
Dbms Unit II
Dbms Unit II
Dbms 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.
BASIC SQL: Simple Database schema, data types, table definitions (create, alter), different DML
operations (insert, delete, update), basic SQL querying (select and project) using where clause,
arithmetic & logical operations, SQL functions (Date and Time, Numeric, String conversion).
Relational Model
Relational Model in DBMS
After designing the conceptual model of the Database using ER diagram, we need to convert
the conceptual model into a relational model which can be implemented using any RDBMS
language like Oracle SQL, MySQL, etc.
4 SURESH DELHI 18
Table-A database table is a structure that organises data into rows and columns
row represent record and column represent attribute
Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME
Domain: All attributes have some pre-defined scope and value which is called attribute
domain.
Relation Schema: A relation schema represents the name of the relation with its attributes.
e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE, and AGE) is the relation schema for
STUDENT.
If a schema has more than 1 relation, it is called Relational Schema.
Tuple: Each row in the relation is known as a tuple. The above relation contains 4 tuples,
one of which is shown as:
Relation Instance: The set of tuples of a relation at a particular instance of time is called a
relation instance. Table 1 shows the relation instance of STUDENT at a particular time. It can
change whenever there is an insertion, deletion, or update in the database.
Column: The column represents the set of values for a particular attribute. The column
ROLL_NO is extracted from the relation STUDENT. Example: ROLL_NO
NULL Values: The value which is not known or unavailable is called a NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
While designing the Relational Model, we define some conditions which must hold for data
present in the database are called Constraints. These constraints are checked before
performing any operation (insertion, deletion, and updation ) in the database. If there is a
violation of any of the constraints, the operation will fail.
Domain Constraints: These are attribute-level constraints. An attribute can only take values
that lie inside the domain range. e.g; If a constraint AGE>0 is applied to STUDENT relation,
inserting a negative value of AGE will result in failure.
Key Integrity: Every relation in the database should have at least one set of attributes that
defines a tuple uniquely. Those set of attributes is called keys. e.g.; ROLL_NO in STUDENT is
a key. No two students can have the same roll number. So a key has two properties:
● It should be unique for all tuples.
● It can’t have NULL values.
Referential Integrity: When one attribute of a relation can only take values from another
attribute of the same relation or any other relation, it is called referential integrity. Let us
suppose we have 2 relations
STUDENT
4 SURESH DELHI 18 IT
BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
CV CIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present in BRANCH_CODE
of BRANCH which is called referential integrity constraint. The relation which is referencing
another relation is called REFERENCING RELATION (STUDENT in this case) and the relation
to which other relations refer is called REFERENCED RELATION(BRANCH in this case).
Basic SQL
Database Schema
The overall design of the database is called schema.
A database schema is a structure that represents the logical storage of the data in a
database. It represents the organization of data and provides information about the
relationships between the tables in a given database
A database schema contains schema objects that may include tables, fields, packages,
views, relationships, primary key, foreign key,
STUDENT (ROLL_NO varchar, NAME varchar, ADDRESS varchar, PHONE num, AGE num,
Subjectname string)
Faculty(Faculty id num,fname string,address string, Subjectname string)
Teaches(Faculty id num, ROLL_NO varchar,Subjectname string)
Data types are used to represent the nature of the data that can be stored in the database
table.
Data types mainly classified into three categories for every database.
o String Data types
o Numeric Data types
o Date and time Data types
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.
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.
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'
DDL is a Data Definition Language that is used to define data structures. For example:
creating a table, and altering a table are instructions in SQL. DML is a Data Manipulation
Language that is used to manipulate data itself. For example: insert, update, and delete are
instructions in SQL
Data Definition Language
DDL is used to specify a database’s structure, which includes its tables, views,
indexes, and constraints.
DDL commands come in the following types: CREATE, ALTER, DROP, RENAME,
and TRUNCATE.
DDL statements only modify the database’s schema; they have no direct effect
on the data within the database.
DDL declarations are irreversible and difficult to undo.
Data Manipulation Language
Inserting, updating, removing, and retrieving data from a database are all
possible with DML.
DML commands come in the following types: SELECT, INSERT, UPDATE, DELETE,
and MERGE.
DML statements have a direct impact on the database’s data.
In the event of an error, data can be recovered thanks to the reversibility of DML
statements.
TABLE DEFINITION:
SQL Table is a collection of data which is organized in terms of rows and columns. In DBMS,
the table is known as relation and row as a tuple.
Operation on Table
1. Create table
2. Drop table
3. Delete table
4. Alter table
Syntax
create table table_name ("column1" "data type", "column2" "data type",
"column3" "data type", ... "columnN" "data type");
Example
SQL> CREATE TABLE EMPLOYEE (EMP_ID INT NOT NULL, EMP_NAME VARCHAR (25) NO
T NULL, PHONE_NO INT NOT NULL,ADDRESS CHAR (30),
PRIMARY KEY (ID) );
Drop Table:
A SQL drop table is used to delete a table definition and all the data from a table. When this
command is executed, all the information available in the table is lost forever
Syntax
DROP TABLE "table_name";
In SQL, DELETE statement is used to delete rows from a table. We can use WHERE condition
to delete a specific row from a table. If you want to delete all the records from the table, then
you don't need to use the WHERE clause.
Syntax
1. DELETE FROM table_name WHERE condition;
Suppose, you want to add the new column Car_Model in the above table. For this, you have
to type the following query in the SQL:
1. SELECT Command
2. INSERT Command
3. UPDATE Command
4. DELETE Command
If we want to retrieve the data from all the columns of the table, we have to use the following
SELECT command:
INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES (104, Anmol, 89, 19)
;
101 Ramesh 92 20
201 Jatin 83 19
104 Anmol 89 19
Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and 'Table_name' is the name of
the table whose values you want to update.
Example 1: This example describes how to update the value of a single field.
P101 Chips 20 20
P102 Chocolates 60 40
P103 Maggi 75 5
P201 Biscuits 80 20
P203 Namkeen 40 50
Suppose, you want to update the Product_Price of the product whose Product_Id is P102. To
do this, you have to write the following DML UPDATE command:
P101 Chips 20 20
P102 Chocolates 60 40
P103 Maggi 75 5
P201 Biscuits 80 20
P202 Namkeen 40 50
Date functions
Function Description
Round(d, [fmt]) Returns the date which is rounded to the unit specified
by the format model
Trunc(d, [fmt]) Returns the date with the time portion of day
truncated to the unit
Greatest(d1, d2, d3…) Returns the latest date present in the arguments
Character Functions
Function Example
Replace(char, searchstring, [rep Select replace(‘jack and jue’, ‘j’, ‘bl’) from dual;
string])
Select replace(‘jack and jue’, ‘j’, ‘bl’) from dual=black and blue
Conversion Functions
Function Description
Numeric Functions
Function Example
After undergoing this laboratory module, the student should be able to write queries using
conversion functions, stri ng, number and date functions.
Exercise
1. Convert 12345 to string.
3. Display system date in ‘MON-DD-YYYY‘ format after converting to varchar2 data type.
6. Concate bid & bname of Boats & display along with color.
7. Lpad the string ‘Rajesh‘ to length 30 with the set of characters in string ‘*‘
8. Lpad the string bname to length 20 with ‘*’ set of characters and string color by ‘/‘.
9. Rpad the string ‘Rajesh‘ to length 30 with the set of characters in string ‘*’
10. Rpad the string sname to length 25 with ‘*‘ set of characters and remaining attributes in
normal way.
11. Rpad the string bname to length 20 with ‘*‘ set of characters and lpad the same to
make it length 30 with ‘#‘ and remaining attributes in normal way.
12. Display all sailors information by removing characters of sname if starts with ‘R‘.
13. Display all sailors information by removing characters of sname if starts with ‘H‘
14. Display all sailors information by removing characters of sname if ends with ‘o‘.
15. Display all Boats information by removing characters of color if ends with ‘r‘.
16. Display all Boats information by showing their names in lower case.
17. Display all Sailors information by showing their names in lower case.
18. Display all Sailors information by showing their names in upper case.
19. Display all Boats information by showing their color in Upper case.
20. Display all Sailors information by showing their names in Capitalizing first char.
22. Find the number of characters in the string ‘Computer Science and Engineering ‘.
23. Display length of string SID, SNAME from Sailors along with their values.
24. Display boats information by starting their names with 3rd character & show only 4
characters.
25. Display the index of string ‘AB‘ after 2nd character & 3rd occurrence in the given string
‘ABCDABCDABABAB‘.
26. Replace ‘A‘ with ‘D‘ in the given string ‘ABCDABCDABABAB‘.
27. Display Sailors information by replacing ‘A‘ with ‘I‘ from SNAME, if any.
30. Display next date on DAY after date D. Ex: Display date on Thu after 20th Feb, 2018.
33. Display SID, Day of Reservation and months between System Date & day of reservation.