0% found this document useful (0 votes)
28 views27 pages

Review of DB Concepts

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

Review of DB Concepts

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

CMSC325

Advanced SQL
DR. RICHA SHARMA
L O C K H AV E N U N I V E R S I T Y

1
Introduction
 Database
 Relational Database
 Entity Relationship Diagrams
 Why Data modeling?
 Few Key Terms
 Review of basic SQL

2
Database
 Database: an organized collection of data or structured
information that allow computer-based systems to store,
manage, and retrieve data very quickly.
 Such computer-based systems are known as database
management systems (DBMS).
 Database systems eliminate drawbacks of file-based data
storage such as:
 Data Redundancy leading to data inconsistency and
integrity issues, lack of security.
 Data Anomalies such as update, insertion, deletion
anomalies
 Dependence on structure of data
 Difficult to quick answers and limited data sharing
3
Benefits of Database
 Better data integration within an organization.
 Improved data sharing and security
 No more data anomalies.
 Improved and better access to data
 Reduced data inconsistency

4
Relational Database
 Relational databases: based on mathematical theory of
Relations and Relational Algebra.
 Being based on mathematical construct, it’s easier to think
of a relation as a table – that can be viewed as 2D
structure organized in the form of rows and columns.
 Data is actually stored in a set of relations (tables).
 Each table contains a set of records or tuples (rows).
 Each record contains a set of attributes or fields (columns).
 Each record has an attribute or set of attributes that
uniquely identifies that row – known as a primary key.

5
Relational Database (ctd.)
 A table may contain an attribute or set of attributes that
refers to another table’s primary key. This reference is
called a foreign key.
 Unlike hierarchical and network database models, relational
database models allow the database designers to focus on
the logical representation of the data and its relationships,
rather than on the physical storage details.
 To summarize, relational models enable us to view data
logically rather than physically!
 Logical view of the relational database is identified by the
creating data relationships based on a logical construct of a
relation – pictorial representation is Entity-Relationship
diagram.
6
Customer & Salesperson Tables
CustomerID Last First SalesID email
Name Name
123 Smith Dave 12 [email protected]
177 Jones Jane 12 [email protected]
299 Lee Ann 14 [email protected]

SalesID Last First Phone SalesOffic


Name Name e
12 Andrews Renee X2345 Denver

14 Fenwick Davis X6789 Dallas

7
Mathematical Description
 Relation schema: A name (of the relation) R with a set of
attributes A1,..., An denoted as:
R(A1,..., An)
 Database schema: A set of relation schemas with different
names:
D = {R1(X1), ..., Rn(Xn)}

 Relation or Table: instance on relation schema, i.e. set r of


tuples or records or rows on X.
 Database: instance on a schema D= {R1(X1), ..., Rn(Xn)},
i.e. set of relations r = {r1,..., rn} (with ri relation on Ri)

8
Entity-Relationship (ER) Diagrams
 Entity: anything about which data are to be collected and
stored. Example - Customers, Books, Students, etc.
 Attribute: a characteristic of an entity. Example: Customer
Id, Student Id, Book ISBN, Book Author etc.
 Relationship: describes an association among entities.
Relationships can be unary, binary, ternary, etc. Types:
 One-to-one (1:1) relationship
 One-to-many (1:M) relationship
 Many-to-many (M:N) relationship
 Constraint: a restriction placed on the data. Example:
Book ISBN must be 13 digit number.

9
ER Diagram – an example

10
Review of Basic SQL
 SQL functions fit into two broad categories:
 Data definition language (DDL)
 Data manipulation language (DML)
 Basic command set has vocabulary of less than 100 words
and it is much simpler than coding in a general purpose
languages!
 DDL allows us to create and affect the structure of the
table, define keys too! Eg. create table, update table etc.
 DML allows us to affect the contents of the table not the
structure! Eg. select, insert etc.

11
Review of DDL
 Using DDL, we can:
 Create and drop table
 Alter table structure
 Define keys
 Define constraints
 An example of creating the table:
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
deptId TEXT NOT NULL,
salary NUMERIC(8,2)
);

12
Review of DDL – Data Types
 The pattern for each field in create table is:
attribute_name datatype properties
 SQL (99) Data Types:
 char(n) and varchar(n)
 text
 Integer, numeric(p,d) and decimal(p,d),
 binary, Boolean … and a few more!

 VARCHAR is best for storing short to medium-length strings,


while TEXT is better suited for storing large amounts of textual
data.

13
Data Types
 In CHAR, If the length of the string is less than set or fixed-
length then it is padded with extra memory space.
 In VARCHAR, If the length of the string is less than the set or
fixed-length then it will store as it is without padded with extra
memory spaces.
 NUMERIC data type is strict - enforces the exact precision
and scale being specified. This is in stark contrast to
DECIMAL, which allows more numbers than the stated
precision.
 Oracle data types include most of SQL99 plus others:
 varchar2(n) – up to 4000 chars, variable number
 BLOB – Binary Large Object (like an image) up to 4GB

14
Another example: Create table
 This example defines two extra properties - a validation check
on the gender character and a foreign key referencing a table
called department with a PK attribute called dId:
CREATE TABLE Employees2
(
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
gender CHAR(1) CONSTRAINT validate_gender
(CHECK
(gender IN ('M', 'F’)),
salary NUMERIC(8,2),
deptId INTEGER,
FOREIGN KEY(deptId) REFERENCES
department(dId)
);
15
Another example (ctd.)
 Other foreign key options: Assume we are creating a
dependent table that references an employees table then we
might use:

FOREIGN KEY(emp_Id) REFERENCES


Employees(empId) ON DELETE
CASCADE

 This would ensure that if we delete an employee in the


Employee table, then we are delete the corresponding
dependent info.

16
Keys
 Each row in a table must be uniquely identifiable!
 Key is one or more attributes that determine other attributes.
 Key’s role is based on determination - if you know the value of
attribute A, you can determine the value of attribute B.

 Functional dependence: attribute B is functionally dependent


on A if the value of A determines the value of B:
A→B
Example: SSN → Birthdate

17
Keys (ctd.)
 Composite key - Composed of more than one attribute
 Key attribute - Any attribute that is part of a key
 Superkey - Any key that uniquely identifies each row. Any
superset of PK is also a superkey.
 Candidate key - A superkey without unnecessary attributes
(minimal superkey). An alternate key is one of the candidate
key not chosen as primary.
 Primary Key – is the selected or chosen candidate key to be
the primary key!

18
Keys (ctd.)
 Secondary key - Key used strictly for data retrieval purposes.
 Foreign key (FK) - An attribute whose values match primary
key values in a related table.
 Referential integrity constraint - FK contains a value that
refers to an existing valid tuple (row) in another relation.
 Surrogate Key - Created to replace a natural key when natural
key is a poor candidate (eg. Student ID rather than SSN)

19
Nulls
 Indicates an empty field - no data entry!
 Nulls are not permitted in primary key.
 Should be avoided in other attributes, but sometimes needed
as nulls can represent:
 An unknown attribute value
 A known, but missing, attribute value
 Nulls may create problems with aggregate functions such as
COUNT, AVERAGE, and SUM!
 Nulls can create logical problems when relational tables are
linked.
20
Other DDL commands
 To remove or drop a table, this is how we do:
DROP TABLE employees;

 To modify a table's structure, we alter it as:


ALTER TABLE employees ADD join_date
DATE;

 In this example, we have added. We can add or remove


columns, change data types, add constraints, etc. using
ALTER TABLE command.
 Existing data in the table must meet the new criteria or the
system will report an error.
21
Other DDL commands (ctd.)
 Few more examples:
Oracle:
ALTER TABLE EMPLOYEE MODIFY name
varchar(30);
MySQL:
ALTER TABLE EMPLOYEE MODIFY COLUMN name
varchar(30));
SQL Server:
ALTER TABLE EMPLOYEE ALTER COLUMN name
varchar(30));

 What if the existing data in table does not support change?


Reference: https://www.w3schools.com/sql/ 22
Other DDL commands (ctd.)
 Few more examples:
Oracle:
ALTER TABLE EMPLOYEE MODIFY salary DEFAULT
10000;
MySQL:
ALTER TABLE EMPLOYEE ALTER salary SET DEFAULT
10000;
SQL Server:
ALTER TABLE EMPLOYEE ADD constraint df_sal
DEFAULT 10000 for salary;
 What if the table has large number of rows then adding
default value will result in?
23
DDL commands - Index
 An index is a database structure that is used to improve the
performance of database in terms of data retrieval.
 A database table can have one or more indexes associated
with it.
 Index provides quick select, insert, delete operations if the
index is created properly identifying which fields are frequently
used for accessing the data. For example:

CREATE INDEX deptId_indx ON EMPLOYEE(deptId);


 Searching on deptId on EMPLOYEE table using a SELECT
statement will now be much faster!

24
DML commands
 DML commands affect the contents of the table. These
command include:
DELETE, INSERT, UPDATE, SELECT

 Example of deleting data:


 DELETE FROM employee;
 DELETE FROM employee WHERE deptId = 4;

25
DML commands (ctd.)
 Inserting data into table:
INSERT INTO EMPLOYEE VALUES (0001, 'Clark',
'Sales’);
 Columns can be in any order if we specify the attribute
names:
INSERT INTO EMPLOYEE(name, empId, deptId)
VALUES ('Ava', 0003, 'Sales');
 Attribute names should be specified if we do not want to add
value for null or default field as:
INSERT INTO EMPLOYEE(empId, name, deptId)
VALUES (0001, 'Clark', 'Sales’);
26
DML commands (ctd.)
 Example of updating existing record data: :

 UPDATE employee SET salary = 1.05*salary;


 UPDATE employee SET salary = 1.05*salary WHERE
deptId = ‘Sales’;

27

You might also like