0% found this document useful (0 votes)
44 views33 pages

ADBMS

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

ADBMS

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

Introduction

Saturday, June 17, 2023 11:11 AM

. What is database?
. Is an organized collection of structured data information typically stored electronically in computer system.
. Electronically organized cabinet.
. Why do we need a database?
. Database is useful to store large scale data. Because of electronic storage system it can be used to crud
operation by multiple users at het same time.
. What is DBMS?
A system that is used to manage databases.
. What are the elements in a database?
. Indexes.
. Tables
. Fields and columns
. Records and rows
. Relationships.
. What are relationship in database?
. Association between tables.
. What are defined as PK, FK and composite PK, referential integrity? (Explanation will be continued here.)

. In the past, some data facts are not stored inside the computer for example favorite color of a student those are
not stored inside the databases. It will in current trend data and information are also considered as important.
Using this we can get details because those details can get to marketing.

Manual data processing system.


. physical
. Some computerized databases but there is no integration between them (tradition file system)
. Entities (strong and weak) are called as table.
. Columns are called as attributes.
. Physically or conceptually available objects are called as entities.
. The explanation that given to entities or the characteristics are called as attributes.

Relationship
. Connectivity between entities is called as relationship.
. This is a conceptual design which means it is not be seen by anyone now the opposite is physical design.

Relationship model.
How these relationship models are available in real time. Every connection / relationship have a separate
meaning.

Strong entity.
If an entity can stand alone it can be called as a strong entity.

Weak entity.
. If an entity will change(influence) what it contain in entity x it is called as weak entity.
. No two weak entities can influence or stay together.

Different types of attribute


Multivalued attribute
. If there are two values for same attribute it is called as multivalued attribute.
. And also if there are range of values which repeating inside the database those ranges are called as

ADBMS Page 1
. And also if there are range of values which repeating inside the database those ranges are called as
multivalued attributes. - range of values. For example: countries in the world. It is a constant they are
not changing and repeating in the database.

Composite attribute
. if there are attributes which can be divided into meaningful sub parts these are called as composite
attributes. For example: name (first name, last name)

Derived attribute
Dynamically updating values are called as derived attribute. For example: age - get the birthday and
calculate the age.

Unique attribute
If a value is available uniquely inside the table this is called as unique attribute.

Total and partial participation


Total participation

ADBMS Page 2
Total participation
. If a each and every record connected with the other table it is called as total participation.

Partial participation

Cardinality ratio (1:M), (1:1), (M:N), (M:1)


. How records are communication from one entity to another.
1:1 - one to one
1:M - one to many
M:N - many to many - M:M

Steps to identify cardinality

ADBMS Page 3
Unary relationship
The table connected with itself is called as unary relationship

Example:

Employee Name Supervisor


1 B 2
2 C 3
3 D 4

Ternary relationship
If all considering three relationships bounded with one relationship it is called as ternary relationship.

Relationship types
If any attribute appear only when the person buys it that is called like this

ADBMS Page 4
In here quantity appears only when the customer purchase product from the store.

ADBMS Page 5
Relational schema
Saturday, June 17, 2023 1:50 PM

Exercise
. The company is organized into departments. Each department has a unique name, a unique number,
and a particular employee who manages the department. We keep track of the start date when that
employee began managing the department. A department may have several locations.

. A department controls number of projects each of which has a unique name, a unique number and a
single location

ADBMS Page 6
. We store each employees name, social security number, address, salary, sex and birth date. An
employee is assigned to one department. But may work on several projects, which are not necessarily
controlled by the same department. We keep track of the number of hours per week that an employee
works on each project. We also keep track of the direct supervisor of each employee.

ADBMS Page 7
. When there is a unary relationship and designing a table for that relationship is unnecessary a column
is enough in the same table. In this case in employee table. With the name of SSN

Exercise 2

The company is organized into departments, each department has a name, number and a employee
who manages the department. We keep track of the start date of the manager. Each department
controls a number of projects. Each project has a name, number and located at a single location. We
store each employees SSN, address, salary, sex and DOB. Each employee works on one department but
may work on several projects. We keep track of the number of hours per week that the employee work
on each project. We also keep track of the direct supervisor of each employee. Each employee may have
number of dependents who are depending on the employee. For each dependent we keep track of their
name, sex, birthday and relationship to employee.

ADBMS Page 8
Assumptions,
I assume that an employee can work for many project that is not related with the department.

Relational schema conversion.


i. Derive attribute
a. Remove then don't put inside the schema.
ii. Multivalued attribute
a. Temperarily remove them from the current table.
iii. Composite attribute
a. Simplify and connect directly
iv. Weak and strong entities
a. Primary key of strong entity will be going to weak entity as foreign key.
v. 1:1 relationship
a. Any primary key can be the foreign key to the next entity.
b. But if there is a total participation in one side then partial sides primary key added to the
total sides primary key.
vi. 1:M relationship
a. 1 side primary key is going to M side
vii. m:N relationship
a. Separate table for each entity and for the cardinality ratio have both side's primary keys and
relationship types.
viii. Unary relationship
a. Separate column
ix. Ternary / n-ary relationship
a. Create a new table with all primary keys and relationships types and another cardinality
ratio table with relationship types.
x. For multivalued attributes create a new table and with the primary key as a foreign key.

ADBMS Page 9
Normalization.
Tuesday, June 20, 2023 9:24 PM

In the normal schema already converted to 1NF when creating


. Next step is to convert the schema to certain format by following the 7 rules when designing the
relational schema,
. attribute level design,
. 1:1 relationship mapping
. 1:M relationship mapping
. M:N relationship mapping
. Weak entities mapping
. Unary relationships mapping
. multivalued attribute mapping.

ADBMS Page 10
Employee
EID Ename Supervisor title

Department
DID Dname PID

Department_Employee
EID DID

Project
PID confirmation

ADBMS Page 11
PID confirmation

Resources
perID Description ExpirationDate

Department_location
DID Location

When considering weak and strong entity always add the strong entities primary key to the weak entity.
That is enough.

ADBMS Page 12
ADBMS Page 13
Functional dependancies
Example:

In here X column decides the Y column,


. If transaction1 of x = transaction2 of x then transaction1 of y is equal to transaction2 of y
If t1(x) = t2(x), then t1(y) = t2(y)

Issues of having those dependencies in a one table if it is not related with the primary key of the table,
. Data redundancy.
. Data updating anomalies - data updating is a headache
. Insert anomaly - without an employee you cannot create an position. Because the primary key is
not the position.
. Potential deletion anomaly - if only one sample of position available and that employee resigned
from the company. And now we need to delete them. So in future registration we don't know
what is the salary of particular position.

Method to solve,

ADBMS Page 14
. in here above addressed problems are solved.
. This will not be a problem if there is a functionally dependant column which is with a primary key.

A, B -> C

A -> B, C, D

Or

. From a table which have composite primary key and those two keys are depending by another key,
P, Q => R
Then you cannot say, P => R, Q => R
. Decomposing the unsatisfactory functional dependacy will be doing in second normal form,

First normal form (1NF)


By default the relational schema is in the 1NF because we need to build the relational schema to
the rules that are in the 1NF. So because we are building that to this orders the building relational
schema is in the 1NF. All the none key attribute should directly or indirectly depend on a primary
key.

Second normal form (2NF)


Separate functional (partial) dependancy. That is explained in above

Third normal form (3NF)


So, finally solve multivalued attribute and repeating groups.

ADBMS Page 15
This is not in 1NF because here P, Q -> T. it cannot happen in 1NF because Q is normal key and in
here P and Q both select for a key

Composite attribute
Joining two columns to work as primary key.
Example:

. So if there is a composite primary key the graph should have to move to the 2NF or else by
default the graph is in 2NF
. in here if both keys have deciding a column those are FD (Full dependacies)
. one key decides another attribute it is called as PD (partial dependancy)

Example question,

1NF

ADBMS Page 16
2NF

3NF
Transitive dependancy removing means indirect connections cannot be have in a graph.

Example question,

ADBMS Page 17
Exercise,

ADBMS Page 18
Questions

ADBMS Page 19
Example:
Unit Code Course code
101 Comp2009
131 Comp2009
112 Comp2009
123 BIS3001

In this kind of unit code decides the course code but if I say course code decides unit code that is wrong
because duplicated values decides different codes.

ADBMS Page 20
Structured query language (SQL)
Tuesday, June 20, 2023 11:16 PM

Database types
Level1: Access type databases - come up with .mdb, .accdb file type can access. No security can be added to
protect.
Level2: SQL - no file accessing available to use or to copy. Password protection available
Level3: SQL server - username and password is available. No separate file available.
Level4: Oracle - username and password is available and also host name is available. Region wise working
introduced. An employee can access to a separate region no outsider can access the same region. To access that
need permission of that region owner. And also access level can be limited from user side.

. In here PL SQL commands are used to access the queries these are compatible with SQL servers and oracle. There are
small deviations in these codes. When it is coming to the examination it is OK to use SQL server related queries or oracle
related queries.

SQL categorization / language types


DDL: data definition language - database defining queries are come under this category. Introducing or removing.
For example: creating a table, database view, removing table, database view.

DML: data manipulation language - Manipulating the data inside the database. For example: insert, update, delete
records.

DCL: Data controlling language - giving a permission or removing a permission to accessing databases.

Queries: totally into select statements.

Note: in some books Queries are fallen under DML.

Data definition language


. creating a database
SQL server,

MY SQL,

Question,
Query for the following table creation,
Enumber | Ename | phoneNumber

Making a composite primary key,

ADBMS Page 21
Adding one column

Adding values to the table by selecting column,

. When adding values if you did not add a primary key. If it is an integer automatically it defines 0 if there is no value.
. If you didn't give a where statement or condition in update statement, the criteria will add to overall entries.
. If you want some more conditions and check before editing.

Delete MYSQL entries

This will delete all the records in the defined table name.

Conditional deletion

This will delete an entry. Try to not to add the primary key column values otherwise it will delete duplicated
every entries in the database.

Drop
This will delete overall table and if you want you can delete the database as well.

Truncate
This will delete the rows in a table but not the table itself.

Question
Display the students registration number who passed the exam. (pass mark = 40)

Display the students registration number and marks of the students who got less than or equal to 40

ADBMS Page 22
This will display all entries where the name column entries ends with the letter a
a%: beginning with a
%a%: some where there need to be an 'a'

This will display all the letters that not have the letter 'r'.

This will display the entries which have comprises with an 'a' in there second letter in the database.
'_____': this pattern confirms that only five letters are being have in the displaying entries.

If you use this between keyword for integers it will find values between the mentioned values there but the range must
be lower to upper not upper to lower (example: between 100 and 200 not 200 and 100)
But if you using this keyword for letters it will display all the entries which are having values between those mentioned
range in the above example it will find all the names which begins with sa and all the the letters which having ti at the
beginning. And will display.
As the opposite you can use not between key word.

This is used to modify the column name or data type, even you can change the primary key.

This will add a new column

This will delete the mentioning column name

Foreign key
Usual method of defining a foreign key

But if there same column names to mark a foreign key


Table 1:

Table 2:

ADBMS Page 23
Describing table names

This will describe about the table. Which means all the columns that have in this table and what is the datatype of
them.

Ordering tables,

This will order the table to the ascending order. To make it to descending order use the keyword 'desc' instead of
'asc'.

Getting the total of any column which is an integer,

This will get the sum of the clmn name and be aware that the choosing column name should be in integer format.
. and like this method you can other attribute of that same thing as well,
Max: maximum value
Min: minimum value
Avg: average of that chosen column
Count(tbl_name): to get he total table rows count.

Conditional application to sum attribute(group by condition),

This statement will group all the departments in the table emp a.k.a. employees and which means only one name
will displayed by representing by one department name and along with that the salary that company has to pay to
that group will be displayed with the name of total to the column.

Conditional bounding for group by statement,

This will display total salary that need to pay to a particular department which are over 50000.

Using where with group by,

Having key word.

Question
Create the following table,
EID Name Dep Salary
1 A HR 10000
2 B Finance 30000
3 C Finance 40000
4 D Marketing 40000
5 E Finance 20000
6 F HR 50000
7 G HR 40000
8 H HR 10000

. Display the names and the salaries of marketing department employees.

ADBMS Page 24
. Display the names and the salaries of marketing department employees.
. Display the table in ascending order of there salary
. Display the total salary distributed for each department.
. Display the name of the employee who gets minimum salary in there department the organization decided to
give 20% bonus only marketing department display the employee name and bonus.

Commit command
. This will confirm the connection with the server.
. But using the rollback command can undo the changes. But if the data is committed then rollback cannot be
done.

View
. This command will create a virtual table. In here we can edit columns which need to be edited or altered.

This is a DDL language type because it is defining the table. The table it creating can work with other tables.

This command will delete the view created

Creating a copy of a table

Changing the table name

Display the first row of the table.

Displaying first 25% of the records

Letter selection

Instead of left you can use right keyword as well.

Restricting having null columns

Instead of 'not null' you can use 'unique' keyword as well. So it will only let to keep one null entry in that
column

Default key word in mysql? This will used the default value if the column entry Is empty.

Sub queries
. It is all about joining more than one tables together.
. There is always a concept of mina queries and sub queries. Main query (sub query)
. Always the output of the subquery will be input to the main query and sub query will be executed first.
. It is better to consider about one record. But if we are providing multiple records better to go with 'joints'.

. Consider these two tables for below explanations.

Stu
Rno Name
1 A

ADBMS Page 25
1 A
2 B
3 C

Res,
Rno Mark
1 80
2 50
3 20

. In a question the given fact will become the sub query and the question will be the main query.
. Getting a value from the second value.

Getting a value who got 80 marks,

. If there are multiple row returnings,

Not in also can be used.

Distinct: if you use this keyword you can print the values that have different values. Which means if there are
same name, in this key word we ill not use these key words here.

. In union duplicated data are not considered.

SQL joins

ADBMS Page 26
SQL joins
. If there is a requirement in considering tables (for now stu and res) display the marks
The code will be: select name, mark from stu, res;

. Union all will make a new table regardless of the duplication of data.

Question,
Emp:
Eno Name
1 A
2 B

Salary:
Eno Sal
1 20000
2 30000

1. Who is getting the maximum salary

1. Person A's salary

Joins,

This will return duplicated amounts of data. Because the command returning depend on cardinality ratio.

But,

In here error may popup if you not specify emp.eno after the select command. Because MYSQL will get
confused eno to be selected from both tables.

Getting A's salary using this method,

Display details who get more that 10000?

The above methods are called as equi join in mysql.

Natural join,
. This type joins only work if the column name are same,

Inner join,
Only common columns will get saved here.
1 X 1 P
2 Y 3 y

. When comparing to columns in equi join no need to mention that these columns are same if there is no equal but

ADBMS Page 27
. When comparing to columns in equi join no need to mention that these columns are same if there is no equal but
the data type is.
. But in natural joins the columns need to be same. Otherwise it won't work.

Inner join: common things will be displayed.


Outer join: this is used to connect table the original table will be connected with this.
Eg: consider the table A and B. we can keep A table as original and can plug the table B to the A. these are called as
left or right join.

Left outer join: left table will be original from the right table matched rows will be mapped to the table (left).
Eg: orders left outer join orders;

Full outer join,


All the tables are considered as, same but common values will not be duplicated.

Example;
A
Eno Name
1 X
2 Y
3 Z

B
Eno Salary
1 10000
3 20000
4 30000

1. Display all the employees name with their salary. Using left outer join?

a.
2. Display the names and the salary in form action using full outer joins.

a.
3. Display eno, name, salary using natural join.
a.
b. There is a problem in the above statement it won't work in normal condition. See the self study guide.

ADBMS Page 28
PL/SQL
Sunday, September 17, 2023 1:49 PM

PL = Procedural language, SQL = Structured query language.

. If you want to enter the same command again and again with in considerable amount of time. Al
ways need to write the code. There may be some shortcut keys where we can have if those are happening in one day.
The terminal is not closed using arrow keys on the keyboard. But if there is gap between two to three days it is not
possible in this situation.
. Also if you want to process several operation with one click that is also not possible in this scenario. I mean in
normal SQL language.

These issues were solved by introducing PL into SQL. This one executable commands just like computer programs.
We can store them in the database itself and use it.
There are several types of them,
Procedure type,
Function type,
Package type: all the mentioned three are normal methods to solve the above problem and there is one
methods call
Triggers: this triggers will boot up when the command called. But for that the trigger should inject to the code
where it need to be triggered when the command runs.

PL / SQL blocks,
. Declaration (optional),
. Exception section: reserved to throw errors when the code not run properly. Declaration and exception are
optional fields
. Execution section: this is the main section.

The image shows the syntax of a PL/SQL query

Stored procedures,
. This is like a normal function if required able to return, otherwise just leave it without returning any values.

Syntax of a stored procedure,


Create procedure x,
IS/AS,
Begin,
Insert into emp values (1, 'X');
End;

To call the created stored procedure,


Begin
X();
x();
End;

ADBMS Page 29
End;

Create procedure y @ eno int (10)


As
Insert into emp values (@eno, 'd');
End;

Method to execute the function,


Exec y @eno = 1;

Sequence
Bulk entries can be entered using this method or proceudures.
. After entering one record the record will be available in the memory(cache). To be add let's look at the

. If inserting method is normal way just you can create a sequence <sequence name> is enough;

When programming a lot it is normal to forget the datatype of the firstly created table as it is. So to copy the
type of the mentioning column you can do that in this method as mentioned above.

Using this command you can print lines. But in some methods there may show errors in the code displaying the
output in that cases you need to import the following command lines,

Procedure with cursor


This will get the records and store in the memory and when we want to print them you can prit them one by
one like a loop.

ADBMS Page 30
To undestand the cursor concept more consider a situation where you want to update a record in the
database. You can do it in two ways,
One is a way that simply typing a update query,
Second you can mention a cursor to do this work.
Both the ways work perfectly but the first method will update the records instantly when you execute the
code. But the second method will update the records one by one. So any time you can stop. There are two
types of cursors, refer the presentation slide.

Records,
. Table based records
In tables there are select statement returning can store inside a variable.

After storing values you can call those stored items like <var1.emp>

. Cursor based records


. User defined records.

Function / procedures.
. Procedures may/may not return values. But in functions you should return some value.

Eg:
create function totalCutomers
Return number IS
Total number(2) := 0
Begin
Select count(*) into total from customers;
Return total;
End;

To call the function you can do it like this,


Select function_name from dual; dual: it is an imaginary table.

Automated functions (triggers)


. These methods don't have an external calling method. it automatically called (firing a trigger). There are
several types of triggers,

ADBMS Page 31
several types of triggers,
. Same table operations
. Different table operations
. System related operations (error messages, blocking entries)

. Triggers can be created only for three different operations,


Insert
Update
Delete
And for two different types,
Before, after so totally there are six type fo triggers.

. And also there is another option which is used for combining.


. The different other that procedures on trigger is that you need to mention an which table and what is the
operation you want to perform.

Code snippet,

. You can access what is going to enter or what is recently deleted using the keywords :new, :old for more see
the text file in 03rd octomber 2023 folder.

ADBMS Page 32
Extended entity relationship diagrams (EER Diagrams)
Saturday, October 7, 2023 1:01 PM

. Differentiate these classes we use 'd' and 'o'. d: disjoint, o: overlap


. 'd' is when two table are not having commo data it is divided using d. and 'o' is when there are
common data in the discussing tables.

ADBMS Page 33

You might also like