Download Chapter 7 (Part 1)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

SQL wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Ingres (database) wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
CHAPTER 7
DATABASE ACCESS THROUGH WEB
PART 1 OF 2
[Data Table and Creating Database]
Madam Hazwani binti Rahmat
http://fsktm2.uthm.edu.my/hazwani/v2/
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW




A database is a collection of data organized in a particular way (tables).
A database is also known as Database Management System (DBMS). It is
a software program that enables the creation and management of
databases.
Generally, a databases could be as simple as a text file with a list of
names.
It could also be as complex as a large, Relational Database Management
System (RDBMS), complete with in-built tools to help in maintaining data.
Slide 2 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW



Most of today's database systems are referred to as a Relational Database
Management System (RDBMS), because of their ability to store related data
across multiple tables.
Common relational database management systems include:
1)
Microsoft Access
2)
Microsoft SQL Server
3)
MySQL
4)
Oracle
A database is essential for many common website functions (Ex:shopping
catalog, forums, blogs, and many more).
Slide 3 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW



DATABASE
DATABASE ::
collection of data
organized in a
particular way
(tables)
Databases are usually organized into one or more tables.
A table is a collection of related records regarding an entity. (Ex: a student
could be an entity)
A table stores data in a series of columns and rows.
Slide 4 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW
DATABASE :
TABLE :
collection of data
collection of
organized in a
related records
particular way
regarding an entity
(tables)

Columns are the attributes or qualities of the entity described by the table.

Example:
student_id
 Entity: Student
 Attribute (column) : student_id

Each column contains a different type of attribute.
Slide 5 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW
DATABASE :
TABLE :
collection of data
collection of
organized in a
related records
particular way
regarding an entity
(tables)

Columns are known as fields.

A field is an area (within a record) reserved for a specific piece of data.

Example:
student name, matric number, faculty

Fields are defined by field name, data type and field size.
Slide 6 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW
DATABASE :
Fields are defined
collection of data
by field name,
organized in a
data type and
particular way
field size.
(tables)
Field name

Each column needs a heading to identify the data it contains.

These headings are called field names.

Field names are used to ensure that the data for each record is entered in
the same sequence.
Slide 7 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW
DATABASE :
Fields are defined
collection of data
by field name,
organized in a
data type and
particular way
field size.
(tables)
Data type


Data type ensures that all the data in a column is entered using the same
format.
Example :
 Character: text, including such things as telephone numbers, zip codes
 Numeric: numbers which can be manipulated using math operators
 Date: calendar dates which can be manipulated mathematically
 Logical: True or False, Yes or No
Slide 8 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW
DATABASE :
Fields are defined
collection of data
by field name,
organized in a
data type and
particular way
field size.
(tables)
Field size
 Field size are the amount of space reserved for storing data
Slide 9 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW



DATABASE :
collection
Columnofare
data
known
organized
as fields
in a /
particular
attributes
way
(tables)
Rows are knows as records.
A record is the collection of values for all the fields pertaining to one
entity.
Example :
student name, matric number, faculty

Every record in a table has exactly the same structure, but different data.
Slide 10 of 36
7.1 DATA TABLE
DATABASE | TABLE | COLUMN | ROW



DATABASE :
collection
Columnofare
data
known
organized
as fields
in a /
particular
attributes
way
(tables)
Each row corresponds to a single record.
Rows hold the actual data, with one (or zero) items for each column of the
table.
Each row is identified by the values appearing in a particular column which
has been identified as a unique key index.
Slide 11 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP



A primary key is a special column or combination of columns that uniquely
identifies each record (row) in a table.
The value that primary key hold must be unique for each record (row), and
must not contain any nulls (non-values).
If it’s possible that TWO or more records (past, present, or future) may
share the same value for an attribute, it’s a poor choice for a primary key.
Slide 12 of 36
7.1 DATA TABLE
If it’s
possible : that
DATABASE
TWO
or more
collection
ofrecords
data
may share the same
organized in a
value for an attribute,
way
it’s particular
a poor choice
for a
primary(tables)
key.
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP



Example :
name
department
position
John
Customer management
clerk
Paul
Customer management
manager
Bill
Human Resource
accountant
No field is going to be guaranteed unique.
There are many people that share the same name / department / position
that may be added to the database in the future.
Slide 13 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP
Solution :



To avoid the uncertainty of using a data column as a primary key, many
developers will create their own column which contains a computer
generated unique number.
The Auto Increment data type automatically increments the field each time
a new record is created.
Hence, a better choice might be to use a unique employee ID number that
you assign to each employee record when they’re created.
Slide 14 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP


Once you decide upon a primary key and set it up in the database, the
database management system will enforce the uniqueness (entity integrity
– PK must be unique and not be null) of the key.
If a record to be inserted into a table has a primary key that duplicates an
existing record, the insertion will fail.
Slide 15 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP
A primary
DATABASE
key :is a
special
collection
column
of data
that
uniquely
organized
identifies
in a
each
particular
record way
(row)
in a table.
(tables)

Foreign keys are columns in a table which provide a link to another table.

It is used to create relationships (correlate information) between tables.

While a primary key uniquely defines a record, while a foreign key is used
to reference the same record from another table.
Slide 16 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP



When a table's primary key field is added to another table in order to
relates the two tables, it is called a foreign key in the referenced table.
It is not necessarily that a primary key has any corresponding value in the
referenced table.
Similarly, the value used in a Foreign Key column is not necessarily unique
to the table it is in but must be unique to the table it is referring to
(Referencial Integrity).
Slide 17 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP
Example :
cust_id (PK) cust_name cust_gender


order_id(PK) item cust_id (FK)
001
John
Male
o1
book
001
002
Paul
Male
o2
shirt
002
004
Lilly
Female
o3
pen
002
We don’t need to store all the other customer information (name, gender,
etc.) in the order table.
This is the elegance of the relational model.
Slide 18 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP



Referential integrity is a database concept that ensures that relationships
between tables remain consistent.
When one table has a foreign key to another table, the concept of
referential integrity states that you may not add a record to the table that
contains the foreign key unless there is a corresponding record in the linked
table.
It also includes the techniques known as cascading update and cascading
delete, which ensure that changes made to the linked table are reflected in
the primary table.
Slide 19 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP

Referential integrity enforces the following three rules:
1)
The Order’s FK attribute must points to a valid record in the Customer
table.
2)
If the primary key for a record in the Customer table changes, all
corresponding records in the Order table must be modified using a
cascading update.
3)
If a record in the Customer table is deleted, all corresponding records
in the Order table must be deleted using a cascading delete.
Slide 20 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP




Relationships are the very core of relational databases .
The primary key, together with the closely related foreign key concept, are
the main way in which relationships are defined.
Relationships allows description of the connections between different
database tables in powerful ways.
Once the relationships between your tables are described, that information
can be used to perform powerful cross-table queries, known as joins.
Slide 21 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP



In relational databases, a relationship exists between two tables when one
of them has a foreign key that references the primary key of the other
table.
There are three types of relationships which can exist between tables:
1)
One-to-one relationships
2)
One-to-many relationships
3)
Many-to-many relationships
Each relationship are named according to the number of table rows that
may be involved in the relationship.
Slide 22 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP
DATABASE :
There are 3 types
collection of data
of relationships
organized in a
which can exist
particular way
between tables.
(tables)
One-to-one relationships


Occur when each entry in the first table has one, and only one,
counterpart in the second table. One-to-one relationships are rarely used
because it is often more efficient to simply put all of the information in a
single table.
It would be possible, although not really desirable, to store all the
information in one table in this case.
Slide 23 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP
DATABASE :
There are 3 types
collection of data
of relationships
organized in a
which can exist
particular way
between tables.
(tables)
One-to-many relationships



This is the most common type of database relationship.
It occur when each record in the first table corresponds to one or more
records in the second table but each record in the second table
corresponds to only one record in the first table.
For example, the relationship between a Teachers table and a Students
table in an elementary school database would likely be a one-to-many
relationship, because each student has only one teacher, but each teacher
may have multiple students.
Slide 24 of 36
7.1 DATA TABLE
PRIMARY KEY| FOREIGN KEY | RELATIONSHIP
DATABASE :
There are 3 types
collection of data
of relationships
organized in a
which can exist
particular way
between tables.
(tables)
Many-to-many relationships



This relationship occur when each record in the first table corresponds to
one or more records in the second table and each record in the second
table corresponds to one or more records in the first table.
Example 1: the relationship between a Teachers and a Courses table would
likely be many-to-many because each teacher may instruct more than one
course and each course may have more than one instructor.
Example 2 : Orders table and a Products table: an order can
contain many products, and a product can be on many orders.
Slide 25 of 36
7.1 CREATING DATABASE
DATABASE | TABLE


With database management systems, many tasks such as creating
databases can be done either via programatically or a user interface.
To create a database, the following SQL statement are used:
Syntax :
CREATE DATABASE db_name
Example :
CREATE DATABASE BIC_21203

At this point, the created database is blank because it does not have any
tables yet.
Slide 26 of 36
7.1 CREATING DATABASE
DATABASE | TABLE


Before creating any table, it is necessary to identify:
1)
Table name
2)
Field name, its data type and characteristics (null / unique)
3)
Primary key and foreign key (if any)
To create a table with a Primary Key, the following SQL statement are
used:
Syntax :
CREATE TABLE table_name(
attribut1_name
data type
NOT NULL
attribut2_name
data type
NOT NULL,
UNIQUE,
PRIMARY KEY(pk_attribute)
);
Slide 27 of 36
7.1 CREATING DATABASE
DATABASE | TABLE
Example :
CREATE TABLE student(
ic
int(12)
NOT NULL
name
varchar(100),
UNIQUE,
PRIMARY KEY(ic)
);
To enforce Entity Integrity
NOT NULL – to ensure no null value accepted for the attribute
UNIQUE - to ensure no duplicate values for the attribute
Slide 28 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

Primary key can be assigned to / remove from a table on following
situation:
1)
Upon creating table
2)
After table has been created (PK attribute should be defined
beforehand)
3)
Remove primary key from a table
Slide 29 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

Case 2 :
After table has been created (PK attribute should be defined beforehand)
Syntax:
ALTER TABLE table_name
ADD PRIMARY KEY (pk_attribute)
Example :
ALTER TABLE student
ADD PRIMARY KEY (ic)
Slide 30 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

Case 3 :
Remove primary key from a table
Syntax:
ALTER TABLE table_name
DROP PRIMARY KEY
Example :
ALTER TABLE student
DROP PRIMARY KEY
Slide 31 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

To create a table with a Foreign Key, the following SQL statement are used:
Syntax :
CREATE TABLE table_name(
attribut1_name
data type
NOT NULL
attribut2_name
data type
NOT NULL,
UNIQUE,
PRIMARY KEY(pk_attribute),
FOREIGN KEY(fk_attribute) REFERENCES primary_table(pk)
);
Slide 32 of 36
7.1 CREATING DATABASE
DATABASE | TABLE
Example :
CREATE TABLE subject(
subject_id
int(3)
NOT NULL
subject_code
varchar(8)
NOT NULL,
ic
int(12),
UNIQUE,
PRIMARY KEY(subject_id),
FOREIGN KEY(ic) REFERENCES student(ic)
ON DELETE RESTRICT
ON UPDATE CASCASE);
To enforce Referential Integrity
ON DELETE RESTRICT – cascading delete
ON UPDATE CASCADE - cascading update
Slide 33 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

Foreign key can be assigned to / remove from a table on following
situation:
1)
Upon creating table
2)
After table has been created (FK attribute should be defined
beforehand)
3)
Remove foreign key from a table
Slide 34 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

Case 2 :
After table has been created (FK attribute should be defined beforehand)
Syntax:
ALTER TABLE table_name
ADD FOREIGN KEY (fk_attribute)
REFERENCES primary_table(pk)
Example :
ALTER TABLE subject
ADD FOREIGN KEY (ic)
REFERENCES student(ic)
Slide 35 of 36
7.1 CREATING DATABASE
DATABASE | TABLE

Case 3 :
Remove foreign key from a table
Syntax:
ALTER TABLE table_name
DROP FOREIGN KEY
Example :
ALTER TABLE subject
DROP FOREIGN KEY ic
Slide 36 of 36