- DBMS coordinate all operations
-
ERD notations
- Entity and its attributes
- Relationships
- Cardinality = N, modality = 0
- Cardinality = N, modality = 1
- Cardinality = 1, modality = 1
- Cardinality = 1, modality = 0
-
MySQL syntax
-
Create database structure
- CREATE SCHEMA AUTHORIZATION <creator>
- CREATE DATABASE <database name>;
-
Create tables
- CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
PRIMARY KEY (column_name)
)
-
Data entry
- INSERT INTO table_name
VALUES (value1, value2, value3,...)
-
Save content
- COMMIT <table names>;
-
List content
-
SELECT column_name(s)
FROM table_name
WHERE conditions
-
WHERE column_name IN (value1,value2,...)
- Sub-query
- BETWEEN value1 AND value2
-
WHERE column_name LIKE pattern
- % = wildcards
- ORDER BY column_name(s) ASC|DESC
- GROUP BY column_name
HAVING aggregate_function(column_name) operator value
-
Join tables
- SELECT column_name(s)
FROM table_name1, table_name2
WHERE table_name1.column_name=table_name2.column_name
-
Aggregate functions
- SUM( )
- AVG( )
- MAX( )
- MIN( )
- COUNT(* )
-
Stored procedure
-
DELIMITER //
CREATE PROCEDURE procedure_name()
BEGIN
...
END //
DELIMITER ;
- SELECT
- RETURNS
- DECLARE
- SET
- CALL procedure_name()
-
Procedural database objects
-
Benefits
- Performance
- Network traffic reduction
- Separate of database logic and application logic
- Maintainability
- Security
- Portability
-
Stored procedure
- Consist declarative and procedural SQL statements
- Can be called by program or trigger
- Can have internal variables, and specify inputs and outputs
-
Trigger
- Programmable events
- Executed before or after INSERT, UPDATE or DELETE statements
- Maintain integrity
- One trigger to one event
-
Hierarchy of data organization
-
Database
-
File
-
Record
- Field
- Character
- Bytes
-
Aims
- Meet users' view towards data
- Tackle atomicity problem
-
Concurrent access
- Reduce redundancy and inconsistency
- Secure data access
- Effective and efficient data management
- Make query easier
- Better user experience
-
Problems of traditional file systems
- Redundancy
-
Anomalies
- Update
- Addition
- Deletion
- Inconsistency
- Sharing
- Security
- Structural dependance
- People and database systems
-
Normalization
- Check the table created against database design rules
- Reduce redundancy and anomalies
-
Stages
-
1NF
- Have candidate key present
- No repeat groups
- All attributes dependant on primary key
-
2NF
- No partial dependancies
- Write keys in separate lines
-
3NF
- No transitive dependancies
- BCNF
-
Based on
-
Functional dependancy
- An attribute depends on another on 1 to 1 basis
-
Fully functional dependancy
- An attribute depends on another, instead of its functional dependant attribute
-
SQL
- Extract, manipulate and structure data
-
Categories
-
Data definition
- Define database structure and data schema
-
Data manipulation
- Extract, add, modify and delete data inside tables
-
Benefits
- Standard language
-
Cross system
- Application portability
-
Less training needed
- Reduce training cost
- Improve productivity
-
Relational database
-
Reason
- Give users a simplier file concept
- Better database design
-
Structure
- Relation (entity set)
- Entity
-
Attribute
- Simple
- Composite
- Null
- Derived
- Keys
-
Entity relationship diagram
-
Three basic elements
-
Entity
- Normal
-
Weak
- Existence dependent
- Derived primary key from parent
-
Attributes
- Single valued
- Multivalued
- Derived
-
Relationships
-
Cardinality
- 1:1
- 1:N
- M:N
-
Modelity
- Optional = 0
- Mandatory = 1
-
Convertion to relations
-
Basic conversion
- 1:1 - Single table
- 1:N - Primary key of 1 becomes attribute of N
- M:N - Each side has own table, plus an intersection table to hold primary keys of both sides
-
Optional max conversion
- 0:1 --> 0:N
- To minimize null attributes