ADBMS
ADBMS
. 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.
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.
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.
ADBMS Page 2
Total participation
. If a each and every record connected with the other table it is called as total participation.
Partial participation
ADBMS Page 3
Unary relationship
The table connected with itself is called as unary relationship
Example:
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.
ADBMS Page 9
Normalization.
Tuesday, June 20, 2023 9:24 PM
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:
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,
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.
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.
MY SQL,
Question,
Query for the following table creation,
Enumber | Ename | phoneNumber
ADBMS Page 21
Adding one 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.
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.
Foreign key
Usual method of defining a foreign key
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'.
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.
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.
This will display total salary that need to pay to a particular department which are over 50000.
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
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.
Letter selection
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'.
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.
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.
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
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.
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.
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;
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
. 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.
Stored procedures,
. This is like a normal function if required able to return, otherwise just leave it without returning any values.
ADBMS Page 29
End;
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,
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>
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;
ADBMS Page 31
several types of triggers,
. Same table operations
. Different table operations
. System related operations (error messages, blocking entries)
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
ADBMS Page 33