Академический Документы
Профессиональный Документы
Культура Документы
2.
Title
First name
Surname
Address
City
Postcode
Telephone
Mr
Tom
Smith
42 Mill Street
London
WE13GW
010344044
Mrs
Sandra
Jones
10 Low Lane
Hull
HU237HJ
022344033
Mr
John
Jones
10 Low Lane
Hull
HU237HJ
022344033
Issues
1. Potential duplication. With perhaps thousands of records in a file, it can be a very tedious process to spot
duplicated records. Especially if more than one person is maintaining the table.
2. Non-unique records. Notice that Mr & Mrs Jones have identical ID's '2'. There is nothing in a flat file system to
stop this. But it is a very poor idea to have identical ID's in a database.
3. Harder to update. Suppose this table now needs to also store their work details - this will result in multiple records
for each person. Again, this is fine - but suppose Sandra Jones now wanted to be known as 'Sandra Thompson'?
Now multiple records need to be updated.
4. Inherently inefficient. What if an email field needs to be added?. If there are tens of thousands of records, there
may be many people having no email address, but every record in a flat file database has to have the same fields,
whether they are used or not.
5. Harder to change data format. Suppose the telephone numbers now have to have a dash between the area code
and the rest of the number, like this 0223-44033. Adding that extra dash over tens of thousands of records would be a
significant task in a flat file database.
6. Poor at complex queries. Flat files are excellent for simple filtering. For example, show all records where the field
'City' contains Hull. But for anything a bit more complicated, a flat file becomes very difficult to use. For example, find
all records whose post code contains '23'.
7. Almost no security. You can protect a flat file database using a password for opening it. But once it is open, that
person can usually see all fields. This is often not a good thing, for example there may be a confidential field
containing their salary that only some people should be able to see.
Disadvantages
Less security easy to extract information.
Data Inconsistency
Redundancy
Sharing of information is cumbersome task
Slow for huge database
Searching process is time consuming
3.
1.
Databases can handle querying tasks, so you don't have to walk over files manually. Databases can
handle very complicated queries.
2.
Databases can handle indexing tasks, so if tasks like get record with id = x can be VERY fast
3.
4.
5.
6.
7.
8.
9.
Databases + ORMs let you manipulate data in very programmer friendly way.
A database is generally used for storing related, structured data, with well defined data formats, in an efficient
manner for insert, update and/or retrieval (depending on application).
On the other hand, a file system is a more unstructured data store for storing arbitrary, probably unrelated
data. The file system is more general, and databases are built on top of the general data storage services
provided by file systems.
There are also differences in the expected level of service provided by file systems and databases. While
databases must be self consistent at any instant in time (think about banks tracking money!), provide isolated
transactions and durable writes, a file system provides much looser guarantees about consistency, isolation and
durability. The database uses sophisticated algorithms and protocols to implement reliable storage on top of
potentially unreliable file systems. It is these algorithms that make database storage more expensive in terms of
processing and storage costs that make general file systems an attractive option for data that does not require
the extra guarantees provided by a database.
As technology moves forward, though, the lines are blurring, as some file systems pick up features previously
the domain of databases (transactions, advanced queries) and some databases relax the traditional constraints
of consistency, isolation and durability. ZFS and BTRFS might be considered examples of the former, MongoDB
and CouchDB examples of the latter.
A very simple computer system may be able to be supported by a very simple database design that only includes a
single table. However, if the database design needs to be enhanced to support more complex requirements, the single
table design would almost always end up being normalized into multiple tabled linked together through relationships.
This is required to reduce data redundancy and to improve efficiency.
There are 3 types of table relationships:
1. One-to-one relationships
2. One-to-many relationships
3. Many-to-many relationships
One-to-One Relationships
In a one-to-one relationship, each row in one database table is linked to one and only one other row in another table.
In a one-to-one relationship between Table A and Table B, each row in Table A is linked to another row in Table B.
The number of rows in Table A must equal the number of rows in Table B.
It would be apparent that one-to-one relationships are not very useful since the database designer might as well
simply merge both tables into a single table. This is true in general. However, there are some situations in which the
one-to-one relationship may improve performance. For example, if a database table contains a few columns of data
that is frequently used and the remaining columns being infrequently used, the database designer may split the single
table into 2 tables linked through a one-to-one relationship. Such a design would reduce the overhead needed to
retrieve the infrequently used columns whenever query is performed on the contents of the database table.
One-to-Many Relationships
In a one-to-many relationship, each row in the related to table can be related to many rows in the relating table. This
effectively save storage as the related record does not need to be stored multiple times in the relating table.
For example, all the customers belonging to a business is stored in a customer table while all the customer invoices
are stored in an invoice table. Each customer can have many invoices but each invoice can only be generated for a
single customer.
Many-to-Many Relationships
In a many-to-many relationship, one or more rows in a table can be related to 0, 1 or many rows in another table. A
mapping table is required in order to implement such a relationship.
For example, all the customers belonging to a bank is stored in a customer table while all the bank's products are
stored in a product table. Each customer can have many products and each product can be assigned to many
customers.
A relationship works by matching data in key columns usually columns with the same name in both tables. In
most cases, the relationship matches the primary key from one table, which provides a unique identifier for each
row, with an entry in the foreign key in the other table. For example, book sales can be associated with the specific
titles sold by creating a relationship between the title_id column in the titles table (the primary key) and
the title_id column in the sales table (the foreign key).
There are three types of relationships between tables. The type of relationship that is created depends on how the
related columns are defined.
One-to-Many Relationship
Many-to-Many Relationships
One-to-One Relationships
One-to-Many Relationships
A one-to-many relationship is the most common type of relationship. In this type of relationship, a row in table A
can have many matching rows in table B, but a row in table B can have only one matching row in table A. For
example, the publishers and titles tables have a one-to-many relationship: each publisher produces many titles, but
each title comes from only one publisher.
Make a one-to-many relationship if only one of the related columns is a primary key or has a unique constraint.
The primary key side of a one-to-many relationship is denoted by a key symbol. The foreign key side of a
relationship is denoted by an infinity symbol.
Many-to-Many Relationships
In a many-to-many relationship, a row in table A can have many matching rows in table B, and vice versa. You
create such a relationship by defining a third table, called a junction table, whose primary key consists of the
foreign keys from both table A and table B. For example, the authors table and the titles table have a many-to-many
relationship that is defined by a one-to-many relationship from each of these tables to the titleauthors table. The
primary key of the titleauthors table is the combination of the au_id column (the authors table's primary key) and
the title_id column (the titles table's primary key).
One-to-One Relationships
In a one-to-one relationship, a row in table A can have no more than one matching row in table B, and vice versa. A
one-to-one relationship is created if both of the related columns are primary keys or have unique constraints.
This type of relationship is not common because most information related in this way would be all in one table. You
might use a one-to-one relationship to:
Store data that is short-lived and could be easily deleted by simply deleting the table.
The primary key side of a one-to-one relationship is denoted by a key symbol. The foreign key side is also denoted
by a key symbol.
other hand, when the lower cardinality value is zero (1:0,1) amore efficient table
structure can be achieved by placing the one-side (1:) tables primary key in
thezero-or-one (:0,1) table as a foreign key. Assume that a company has 1000
employees but only 100 ofthem are sales staff. Assume also that each sales person
is assigned a company car. Therefore, everyoccurrence in the Employee entity is
associated with either zero or one occurrence in the Company Carentity. If we
assigned the Company Car (:0,1) side primary to the Employee (:1) table as a
foreign keythen most of the foreign will have null (blank) values. While this
approach would work, it could causesome technical problems during table searches.
Correctly applying the key-assignment rule solves thisproblem because all Company
Car records will have an employee assigned and no null values will oc-cur