Download presentation
Presentation is loading. Please wait.
1
CHAPTER 7 DATABASE ACCESS THROUGH WEB
PART 1 OF 2 [Data Table and Creating Database] Madam Hazwani binti Rahmat
2
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.
3
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: Microsoft Access Microsoft SQL Server MySQL Oracle A database is essential for many common website functions (Ex:shopping catalog, forums, blogs, and many more).
4
DATABASE : collection of data organized in a particular way (tables)
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) DATABASE | TABLE | COLUMN | ROW 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.
5
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) TABLE : collection of related records regarding an entity DATABASE | TABLE | COLUMN | ROW 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.
6
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) TABLE : collection of related records regarding an entity DATABASE | TABLE | COLUMN | ROW 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.
7
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) Fields are defined by field name, data type and field size. DATABASE | TABLE | COLUMN | ROW 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.
8
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) Fields are defined by field name, data type and field size. DATABASE | TABLE | COLUMN | ROW 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
9
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) Fields are defined by field name, data type and field size. DATABASE | TABLE | COLUMN | ROW Field size Field size are the amount of space reserved for storing data
10
Column are known as fields / attributes
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) Column are known as fields / attributes DATABASE | TABLE | COLUMN | ROW 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.
11
Column are known as fields / attributes
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) Column are known as fields / attributes DATABASE | TABLE | COLUMN | ROW 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.
12
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.
13
DATABASE : collection of data organized in a particular way (tables)
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) If it’s possible that TWO or more records may share the same value for an attribute, it’s a poor choice for a primary key. PRIMARY KEY| FOREIGN KEY | RELATIONSHIP Example : 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. name department position John Customer management clerk Paul manager Bill Human Resource accountant
14
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.
15
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.
16
DATABASE : collection of data organized in a particular way (tables)
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) A primary key is a special column that uniquely identifies each record (row) in a table. PRIMARY KEY| FOREIGN KEY | RELATIONSHIP 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.
17
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).
18
7.1 DATA TABLE PRIMARY KEY| FOREIGN KEY | RELATIONSHIP Example : 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. cust_id (PK) cust_name cust_gender 001 John Male 002 Paul 004 Lilly Female order_id(PK) item o1 book o2 shirt o3 pen cust_id (FK) 001 002
19
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.
20
7.1 DATA TABLE PRIMARY KEY| FOREIGN KEY | RELATIONSHIP Referential integrity enforces the following three rules: The Order’s FK attribute must points to a valid record in the Customer table. 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. If a record in the Customer table is deleted, all corresponding records in the Order table must be deleted using a cascading delete.
21
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.
22
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: One-to-one relationships One-to-many relationships Many-to-many relationships Each relationship are named according to the number of table rows that may be involved in the relationship.
23
DATABASE : collection of data organized in a particular way (tables)
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) There are 3 types of relationships which can exist between tables. PRIMARY KEY| FOREIGN KEY | RELATIONSHIP 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.
24
DATABASE : collection of data organized in a particular way (tables)
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) There are 3 types of relationships which can exist between tables. PRIMARY KEY| FOREIGN KEY | RELATIONSHIP 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.
25
DATABASE : collection of data organized in a particular way (tables)
7.1 DATA TABLE DATABASE : collection of data organized in a particular way (tables) There are 3 types of relationships which can exist between tables. PRIMARY KEY| FOREIGN KEY | RELATIONSHIP 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.
26
CREATE DATABASE db_name
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.
27
7.1 CREATING DATABASE DATABASE | TABLE Before creating any table, it is necessary to identify: Table name Field name, its data type and characteristics (null / unique) 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 UNIQUE, attribut2_name data type NOT NULL, PRIMARY KEY(pk_attribute) );
28
7.1 CREATING DATABASE Example : To enforce Entity Integrity
DATABASE | TABLE Example : CREATE TABLE student( ic int(12) NOT NULL UNIQUE, name varchar(100), 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
29
7.1 CREATING DATABASE DATABASE | TABLE Primary key can be assigned to / remove from a table on following situation: Upon creating table After table has been created (PK attribute should be defined beforehand) Remove primary key from a table
30
7.1 CREATING DATABASE Case 2 :
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)
31
7.1 CREATING DATABASE Case 3 : Remove primary key from a table Syntax:
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
32
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 UNIQUE, attribut2_name data type NOT NULL, PRIMARY KEY(pk_attribute), FOREIGN KEY(fk_attribute) REFERENCES primary_table(pk) );
33
7.1 CREATING DATABASE Example : To enforce Referential Integrity
DATABASE | TABLE Example : CREATE TABLE subject( subject_id int(3) NOT NULL UNIQUE, subject_code varchar(8) NOT NULL, ic int(12), 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
34
7.1 CREATING DATABASE DATABASE | TABLE Foreign key can be assigned to / remove from a table on following situation: Upon creating table After table has been created (FK attribute should be defined beforehand) Remove foreign key from a table
35
7.1 CREATING DATABASE Case 2 :
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)
36
7.1 CREATING DATABASE Case 3 : Remove foreign key from a table Syntax:
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.