Database | slide # 1 |
...Fixed vs. Variable length records | slide # 4 |
...Indexing | slide # 7 |
...Roll your own vs. using existing | slide # 8 |
...Concurrency | slide # 9 |
Interacting with a database - SQL | slide # 12 |
...SQL: CREATE | slide # 13 |
...SQL: DROP | slide # 14 |
...SQL: INSERT | slide # 15 |
...SQL: DELETE | slide # 16 |
...SQL: SELECT | slide # 17 |
...SQL: UPDATE | slide # 20 |
Why databases in a client-server course?
95% or more of all servers will use some sort of database.
Examples we have looked at so far:
Services which do not use a database include:
Andrew's definitions:
A database holds information and provides for a mechanism to access this information.
Examples of some common (electronic) databases:
Table:
A collection of records
Record:
A collection of fields which belong together
Field:
A piece of data (datum)
Example record:
Lastname Firstname MI ID Phone Email
Example table of these records:
Scherpbier | Andrew | W | 42 | 45026 | none |
Over | Ben | D | 57 | 411 | a@b |
Records can be fixed-length or variable length.
Variable length records:
Examples:
Advantages:
Disadvantages:
Advantages:
Disadvantages:
Example of wasted space:
PC with 1.2GB disk:
64 sectors per cluster512 bytes per sector
Result: A 10 byte file takes up 32K on disk.
Once we store data, we need a way to get at the data.
Indexing provides a method to find records.
An index maps keys to records
Keys can be:
Keys are normally stored in a separate data structure.
Some common data structures used:
Multiple indexes can be created on a single table
Issues to consider when using indexes:
The decision of what to use depends on what needs to be accomplished and what resources are available.
(Some data structures do not (easily) allow for duplicate keys.)
There are many database management systems available.
Most of them commercial but others are free or come with the OS.
In many cases it is quite possible to design and implement your own database.
For a concurrent server, you have to worry about concurrency.
Goal:
The table and its indexes must always be in sync.
Some solutions:
Other problems:
Oracle:
ndbm:
mSQL:
Sometimes database means a collection of data
Sometimes database means a set of tables, indexes, and views
Database programs tend to extend SQL to produce an "improved" version
Oracle has sqlplus, mSQL has msql
Data Manipulations | |
SELECT | Retrieves data from table(s) |
INSERT | Adds row(s) to a table |
UPDATE | Changes field(s) in record(s) |
DELETE | Removes row(s) from a table |
Data Definition | |
CREATE TABLE | Define a table and its columns(fields) |
DROP TABLE | Deletes a table |
ALTER TABLE | Adds a new column, add/drop primary key |
CREATE INDEX | Create an index |
DROP INDEX | Deletes an index |
CREATE VIEW | Define a logical table from other table(s)/view(s) |
DROP VIEW | Deletes a view |
The create clause is used to create a table.
CREATE TABLE table_name ( col_name col_type [ NOT NULL | PRIMARY KEY] [, col_name col_type [ NOT NULL | PRIMARY KEY]]*)
Example:
CREATE TABLE students ( firstname CHAR(20) NOT NULL, lastname CHAR(20), phone CHAR(10), code INTEGER ) CREATE TABLE codes ( code INTEGER, name CHAR(20) )
firstname | lastname | phone | code |
code | name |
Drop is used to remove a table definition from the database.
DROP TABLE table_name
DROP TABLE students
Putting data into a table is done with the INSERT clause:
INSERT INTO table_name [(column [, column ]*)] VALUES (value [, value]*)
INSERT INTO students VALUES (`John', `Smith', `555-9876', 1516) INSERT INTO students VALUES (`Ben', `Over', `555-1212', 9500) INSERT INTO codes VALUES (1516, `Good guy') INSERT INTO codes VALUES (9500, `Bad guy')
firstname | lastname | phone | code |
John | Smith | 555-9876 | 1516 |
Ben | Over | 555-1212 | 9500 |
code | name |
1516 | Good guy |
9500 | Bad guy |
Records can be deleted from a table using the DELETE clause.
Note that we need to specify which record or records need to
be deleted:
DELETE FROM table_name WHERE column OPERATOR value [ AND | OR column OPERATOR value ]*
Example:
DELETE FROM students WHERE firstname = `Ben'
firstname | lastname | phone | code |
John | Smith | 555-9876 | 1516 |
Accessing data with SQL is done using a query.
A query always starts with SELECT:
SELECT [table.]column [, [table.]column]* FROM table [=alias][, table [= alias]]* [ WHERE [table.]column OPERATOR VALUE [ AND | OR [table.]column OPERATOR VALUE]*] [ ORDER BY [table.]column [DESC][, [table.]column [DESC]]
The expression syntax supported by the LIKE clause can have
the following special characters:
Simple example:
SELECT firstname, lastname FROM students
firstname | lastname |
John | Smith |
Ben | Over |
Queries can be more complex by adding keywords:
SELECT * FROM students WHERE code < 5000
firstname | lastname | phone | code |
John | Smith | 555-9876 | 1516 |
Relations can be made between tables: SELECT students.firstname, codes.name FROM students, codes WHERE students.code = codes.code
SQL: SELECT examples (cont.)
firstname | name |
John | Good guy |
Ben | Bad guy |
Modifying a record is done with the UPDATE clause:
UPDATE table_name SET column=value [,column=value]* WHERE column OPERATOR value [ AND | OR column OPERATOR value]*
UPDATE students SET firstname=`Johnny' WHERE code=1516
firstname | lastname | phone | code |
Johnny | Smith | 555-9876 | 1516 |
Ben | Over | 555-1212 | 9500 |