RDMS
RDMS
RDMS
Advantage of file sys : we need physical files but to store everything on computers
but it is not necessary that all data should be on the same computer.
rdms Page 1
- Backup and recovery
Advantage :
1. Security [role based security]
2. Sharing of data
3. Avoid redundancy
4. Maintenance of integrity
5. Data independence
Database user :
Application developers :
who writes application to interact with database. Application program written in
Kobalt, java, c++, etc.
Sophisticated Users :They are those users who interact with the system without
writing the program instead they form their request in database query language.
They are the SQL programmers, who are going to deal directly with the database.
They write queries to delete or select or insert and update the database.
End users : are basically those people whose jobs require access to the database for
querying, updating, and generating reports. The database primarily exists for their
use
Data model :
rdms Page 2
This concept uses a hierarchical tree structure to organise the data. The
hierarchy begins at the root, which contains root data, and then grows into a
tree as child nodes are added to the parent node.
One to many relationship , parent child relationship.
Problems :
• Relationships that are complex are not supported.
• Because it only supports one parent per child node, if we have a complex
relationship in which a child node needs to have two parents, we won’t be
able to describe it using this model.
• When a parent node is removed, the child node is removed as well.
2. Network model :
The main difference between this model and the hierarchical model is that any
record can have several parents in the network model. It uses a graph instead
of a hierarchical tree.
rdms Page 3
Relational Model
The data in this model is kept in the form of a table that is two-
dimensional. All of the data is kept in the form of rows and columns.
Tables are the foundation of a relational.
Popular Relational Database Management Systems:
• IBM – DB2 and Informix Dynamic Server
• Oracle – Oracle and RDB
• Microsoft – SQL Server and Access
• Sybase
Keys : normal attribute / variable . It uniquely identify two or more tuples [rows of
table]. This key values will be unique for each entity. Can be treated as candidate
key [ unique]
- primary key : [ unique + not null]
The PRIMARY KEY constraint uniquely identifies each record in a
table.
Primary keys must contain UNIQUE values, and cannot contain
NULL values.
A table can have only ONE primary key; and in the table, this
primary key can consist of single or multiple columns (fields).
[ composite ]
rdms Page 4
Relation = table
Cardinality = no of rows in table
Tuple = row of table/ entity
Attributes = columns in table / characteristics of entity
Degree = no of column / attributes
Domain = range of attribute
Properties of relation :
a. No duplicate rows
b. Tuples are unordered.
c. Attributes are unordered
d. Attributes values are atomic [
An atomic attribute is always represented by a single value,
something which cannot divided further] name can't be atomic
value because
It has first name and last name. age can be atomic value.
rdms Page 5
Sql is command like language.
rdms Page 6
A data definition language (DDL) is a computer language used to create and
modify the structure of database objects in a database.
Post quiz 1 : oracle , mysql , sybase are relational db and unify is not.
Prequiz 1 :
rdms Page 7
Char data types :
Char(n) : static memory allocation, size is fixed
Varchar2(n) : dynamic memory allocation, size can expand and sink depend on
data value.
Number(p): stores number of size p;
Number (p,s) : precision , scale == number and floating point
Date : DD-MON-YY. Stores date.
Only letters, underscore , number are valid, name should start with letter
Unique key can be multiple but it can only have a null value.
If you define a CHECK constraint on a column it will allow only certain values
for this column
Foreign key is used to enforce the link between the tables. The referenced
table is called the parent table while the table with the foreign key is called
the child table.
rdms Page 8
rdms Page 9
Truncate :
Removes all rows from table / data is only removed here
Cannot be done if table is linked with another table.
Syntax :
TRUNCATE TABLE TableName;
Ex : TRUNCATE TABLE Customer;
DROP :
○ Removes whole table structure from db.
Add columns :
ALTER TABLE tablename ADD columnname datatype
Remove column :
ALTER TABLE Tablename DROP COLUMN columnName
Add constraints:
Remove constraints :
rdms Page 10
Rename columns/table:
For column :
ALTER TABLE TableName RENAME COLUMN oldcolumnName TO newColumnname
For table :
ALTER TABLE TableName RENAME TO NewTableName;
Insert rows:
INSERT INTO TableName(col1,col2..coln) VALUES(val1,val2..valn)
Varchar , char and date values should be within single quote
Where is the condition if we wont use all the value of that col will be update
DELETE ROW :
DELETE FROM TABLENAME [WHERE CONDITION]
rdms Page 11
Delete from tablename == delete every row from table.
Cascade : if delete row from parent table , child table will be deleted
Set null : if row from parent is deleted then then child table row is set with null.
Restrict : rejects the delete or update operation for parent table. This is default.
SYNTAX:
REFRENCES TABLENAME (COL_NAME)[ON DELETE REF_OPT]
Ref_opt : restrict | cascade | set null
Merge :
rdms Page 12
When we want to update , insert delete in same time we can do this with merge
It needs target table and source table
Select statement :
Its used to retrieve data from one or more table.
The retrieved result Is stored in a temporary result table called result set
It can be helpful In retrieving :
- All rows from table
- Specific rows
- Specific values
rdms Page 13
- Specific values
Select * from tablename ; it will everything from table.
Select c1name,c2name from tablename;
rdms Page 14
In operator be used with any data type.
rdms Page 15
Asc is default but if we want to desc for another asc for another table we need to
mention
=====================================================================
Function :
A block of code that sometimes take argument and always return a value.
Single- row : also called scalar func. , operates on single row only return a
result.
rdms Page 16
Character function .
Conact (string,string);
rdms Page 17
SELECT CEILING(25.00) AS CeilValue;
Return the smallest integer value that is greater than or equal to a number:
From <https://www.w3schools.com/sql/func_sqlserver_ceiling.asp>
Return the largest integer value that is equal to or less than
25.75: 26
Round value :
From <https://www.w3schools.com/sql/func_sqlserver_round.asp>
-1 10th place make zero and check the 10-1th place to round it
rdms Page 18
Or a no can be subtracted from a date
These opr can also done between two dates
Answers:
1. 11/20/2018 it will only round after 13 hours completed .
2. 11/20/2018
3. 12/01/2018 as in sysdate 20 dec half of month completed
4. 12/01/2018 as in sysdate 20 dec half of month completed
5. 12/01/2018 as in sysdate 20 dec half of month completed
6. 11/01/2018 as '11/15/2018' as round will done only after 16.
rdms Page 19
TO_DATE : CONVERTS STRING TO DATE.
SYNATX : TO_date(DATE_IN_STRING', FROMAT);
TO_CHAR(NUMBER/DATE TO STRING)
SYNTAX : TO_CHAR(NUMBER OR DATE , FORMAT);
rdms Page 20
If else case
Case clause :
Multiple condition :
rdms Page 21
rdms Page 22
If we remove default / else value then null value will be return
Multi row :aggregate , operates on multiple row and return a result for multi
rows:
rdms Page 23
It takes n no of i/p and returns single op.
Count :
The COUNT() function returns the number of rows that matches a specified
Criterion
Count(*) count all rows;
rdms Page 24
Grouping identical data in groups , it is used with select
Where clause is used for all rows in table but not in group by then we use
'having
rdms Page 25
Group by is used without distinct works as distinct
But group shows the result in order wise
================================================================
Types :
rdms Page 26
Self -join : table joins with itself ,
rdms Page 27
Natural joins return mactching values from t1,t2
Note : in equi join returns rows only when there
Is at least one row from both the table matches the
Join condition
Left outer:
rdms Page 28
Using clause : as in natural join if attr from t1,t2 matches then join happens,
In using clause if two or more att matches from t1 and t2 then we can decide
Join , It provides more flexiblity
rdms Page 29
rdms Page 30
In natural join we used to write only common attribute.
And by using clause also we write common clause but
By using ON we can use conditions also and equality
Multiple join :
rdms Page 31
Sub query :
Subquey is also called as inner query or inner select and outer is called outer
query and outer select
rdms Page 32
Subquery : inner query should be enclosed by ()
We can't use order by in inner query but we can use group by.
We use = , < , <= , >= <> when inner queries return single value.
rdms Page 33
It will bring salary of emp greater than 1600 ======= greater than > all
rdms Page 34
rdms Page 35
\
rdms Page 36
Inner query take values from outer query
=====================================================================
DCL command :
Its used to allow users to access and manipulate data base.
From <https://www.geeksforgeeks.org/difference-between-grant-and-revoke/>
View :
This object is used in the database to create a type of view of the database. It
is a logical view based on one or more tables or even another view. A view
does not contain any data of its own but is used to view data in other tables.
The view is based on a table, and that table is known as the base table. The
data dictionary stores the view as a SELECT statement
rdms Page 37
Select att1,att2. from [select…]
Removing view :
Syntax --- Drop view view_name;
Views advantage :
Security
Simple
Abstraction
Top n analysis:
rdms Page 38
Top n analysis:
Top-N queries ask for the n smallest or largest values of a column. Both
smallest and largest values sets are considered Top-N queries. Following this
type of searching technique could save lot of time and complexities. Top-N
analysis are useful in cases where the need is to display only the n bottom-
most or the n top-
most records from a table based on a condition. This result set can be used
for further analysis.
From <https://www.geeksforgeeks.org/sql-top-n-queries/>
Sequence :
By default cache is 20
=---------------------------
CREATE SEQUENCE sequence_name
START WITH initial_value
rdms Page 39
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
sequence_name: Name of the sequence.
initial_value: starting value from where the sequence
starts.
Initial_value should be greater than or equal
to minimum value and less than equal to maximum
value.
increment_value: Value by which sequence will
increment itself.
Increment_value can be positive or negative.
minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.
cycle: When sequence reaches its set_limit
it starts from beginning.
nocycle: An exception will be thrown
if sequence exceeds its max_value.
From <https://www.geeksforgeeks.org/sql-sequences/>
rdms Page 40
Synomys:
Index :
rdms Page 41
1. Clustered Index :
Clustered index is created only when both the following
conditions satisfy –
5. The data or file, that you are moving into secondary
memory should be in sequential or sorted order.
6. There should be a key value, meaning it can not have
repeated values.
From <https://www.geeksforgeeks.org/difference-between-clustered-and-non-clustered-
index/>
From <https://www.geeksforgeeks.org/difference-between-clustered-and-non-clustered-
index/>
rdms Page 42