Professional Documents
Culture Documents
Lecture 1
Definitions
Data: Meaningful facts, text, graphics, images, sound, video segments. Database: An organized collection of logically related data. Information: Data processed to be useful in decision making. Metadata: Data that describes data.
Data in Context
Summarized data
Tables
Relational databases views all data in the form of tables Following Figs. a and b shows four tables, Customer, Product, Order and OrderLine (the 4 entities shown in the previous ER diagram). Each column represents an attribute, e.g. the Customer table has attributes ID, Name, Address..etc. Relationships between entities are represented by values stored in columns of the corresponding tables, e.g. Customer_ID is an attribute of both the Customer table and the Order table. This makes it easy to link an order with its customer.
SQL Is:
Structured Query Language (some for historical reasons call it Sequel The standard and most common language for relational database management systems An SQL-based relational database application involves a user interface, a set of tables in the database, and a RDBMS with an SQL capability Within the RDBMS SQL will be used to create the tables, translate user requests, maintain the data dictionary and system catalog, update an maintain the tables, establish security, and carry out backup and recovery procedures
Following Fig. Shows a schematic of an SQL environment Each database is contained in a catalog, which describes any object that is part of the database (regardless of which user created that object). Most companies keep at least two versions of any database they are using. PROD_C is the live production version, which captures real business data and this must be very tightly controlled and monitored. DEV_C is the development version used when the database is being built and continues to serve as a development tool where enhancements and maintenance efforts can be tested before application to the production database. Each database will have a set of schemas associated with a catalog. Schema = the structure that contains descriptions of objects created by a user (base tables, views, constraints)
Creating tables
Once data model is designed and normalised, the columns needed for each table can be defined using the CREATE TABLE command. The syntax for this is shown in the following Fig. These are the seven steps to follow: 1. Identify the appropriate datatype for each , including length and precision 2. Identify those columns that should accept null values. Column controls that indicate a column cannot be null are established when a table is created and are enforced for every update of the table
Creating tables
3. Identify those columns that need to be UNQUE - when the data in that column must have a different value (no duplicates) for each row of data within that table. Where a column or set of columns is designated as UNIQUE, this is a candidate key. Only one candidate key may be designated as a PRIMARY KEY 4. Identify all primary key-foreign key mates. Foreign keys can be established immediately or later by altering the table. The parent table in such a parent-child relationship should be created first. The column constraint REFERENCES can be used to enforce referential integrity
Creating tables
5. Determine values to be inserted into any columns for which a DEFAULT value is desired - can be used to define a value that is automatically inserted when no value is provided during data entry. 6. Identify any columns for which domain specifications may be stated that are more constrained than those established by data type. Using CHECK it is possible to establish validation rules for values to be inserted into the database 7. Create the table and any desired indexes using the CREATE TABLE and CREATE INDEX statements
Table creation
General syntax for CREATE TABLE
Table creation
The following Fig. Shows SQL database definition commands Here some additional column constraints are shown, and primary and foreign keys are given names For example, the CUSTOMER tables primary key is CUSTOMER_ID The primary key constraint is named CUSTOMER_PK, without the constraint name a system identifier would be assigned automatically and the identifier would be difficult to read