Er. Nawaraj Bhandari Topic 11 Implementation
Create Table
The various ‘create table’ statements will be run as part of a script.
Any domains referred to, if they are created as separate objects in the database, will need to be created first.
Create Domain
Explicitly create a domain:
Create domain Allowable_Colours as Char Default ‘Red’ Check (Value in (‘Red’,’Blue’,’Green’));
Types of Constraints
Referential integrity Propagation constraints Domain constraints Table constraints
Plus... Constraint Violation
Referential Integrity Constraint
Create table workers(emp_no number(5) not null,first_name varchar(30),last_name varchar(30),job_title varchar(30),age number(3),dept_no number(5),primary key emp_no,foreign key (dept_no) references
Propagation Constraint
What happens if we delete a Boat from our Boat Hire database?
There are lots of Rental records that reference it. What happens to them?
Table with Propagation Constraint
Create Table Rental(BoatID number NOT NULL,CustomerID number NOT NULL,RentalStartDate date NOT NULL,RentalEndDate date NOT NULL,Primary Key (BoatID, CustomerID, RentalStartDate)Foreign Key (CustomerID) REFERENCES Customer(CustomerID),Foreign Key (BoatID) REFERENCES Boat (BoatID)On delete no actionOn update cascade);
Domain Constraints
Boat Type could be enforced as...
a check constraint separate domain using Create Domain
statement as a foreign key to another table
Check Constraint
Create Table Boat
(BoatID number NOT NULL,
BoatName varchar (30) NOT NULL,
BoatType varchar (20),
Primary Key (BoatID)
Check (BoatType in ‘Yacht’,’Cruiser’,’Rower’));
As a Separate Domain
Create Domain BoatType As varchar(20) Default ‘Yacht’ Check (Value in (‘Yacht’,’Cruiser’,’Rower’)); The table’Boat’ will set the BoatType attribute as this domain
BoatType. Create Table Boat(BoatID number NOT NULL,BoatName varchar (30) NOT NULL,BoatTypeCode BoatType,Primary Key (BoatID));
As a Separate TableCreate Table BoatType(BoatTypeCode Varchar(3),BoatTypeDescription Varchar(20)Primary Key (BoatTypeCode));
With the corresponding Foreign Key in BoatCreate Table Boat(BoatID number NOT NULL,BoatName varchar (30) NOT NULL,BoatTypeCode varchar (3),Primary Key (BoatID)Foreign Key (BoatTypeCode) References BoatType;
Table ConstraintsCreate Table Rental(BoatID number NOT NULL,CustomerID number NOT NULL,RentalStartDate date NOT NULL,RentalEndDate date NOT NULLConstraint MaximumRentalsCheck(Not Exists(Select BoatIDFrom RentalsGroup By BoatIDHaving Count(*) >10)),Primary Key (BoatID, CustomerID, RentalStartDate)Foreign Key (CustomerID) REFERENCES Customer(CustomerID),Foreign Key (BoatID) REFERENCES Boat (BoatID)On delete no actionOn update cascade);
Inserting Data The basic SQL feature for inserting multiple rows:
INSERT INTO ''TABLE'' (''column-name'', [''column-name', ...])
VALUES (''1st row value a'',"1st row value b"),
(''2nd row value a', "2nd row value b"),
...
Example
Insert into boats (boatID, boatName)
Values (1,'Esmerelda'),
(2,'Missy'),
(3,'Lord George');
Data Loading Tools
Bulk insert in SQL server http://sqlserver2000.databases.aspfaq.com/how-do-i-
load-text-or-csv-file-data-into-sql-server.html Oracle SQL loader http://oreilly.com/catalog/orsqlloader/chapter/ch01.html My-SQL uses something called 'Bulk Insert' http://mysql.bigresource.com/Bulk-insert-from-text-
files-dDPRzHYo.html#2t6P0D5I
Server
Database
Network
Internet
Server
Applications
Other systems
http://192.168.2.223:5560/isqlplus
Oracle
For many years, Oracle was the largest market share AND largest by value.
It was challenged recently by MySQL.
Support for Languages
Java Programming – allowing sophisticated manipulation of data using the logic of a programming language
XML – allowing development of own tags so capable of transmitting data in almost any format
Support for...
Distributed database features Advanced security Data warehousing Internet ready features
Objects
Tables, indexes, views, domains... More complex objects with internal structure PL/SQL Stored functions Stored procedures Triggers
Logical Structure of Oracle
Tables spaces – logical storage units that contain the other objects, which have memory divided into blocks, extents, and segments.
Database schemas contain or own the objects like tables.
Definition of users
Physical Structure of Oracle DatafilesDatabase datafiles are physical files stored on disk. These files are used to store data on disk. Database datafiles are only written to by the DBWR processes
Redo logsLike a tape recorder that records every change in the Oracle database. As changes occur, they are regularly recorded in the online redo logs, just like you might record a movie on your VCR.
Control filesThe Control File of the database is a binary file that contains a great deal of database information. The control file contains the database name, data about the database log files. Oracle cannot function without valid control files.
Oracle Instance Contains all the processes and memory areas. A database instance is a set of memory structures that manage
database files The instance manages its associated data and serves the users of the
database. Also contains...
Oracle Instance
- System Global Area: When an instance is started, Oracle Database allocates a memory area called the SGA.
- Program Global Area: A PGA is a memory region that contains data and control information for a server process. It is no shared memory created by Oracle Database when a server process is started
- User Processes: User roles and user management
References Benyon-Davis, P. (2003). Database Systems, 3rd edition. Palgrave Macmillan. Chapter 12. Connolly, T. & Begg, C. (2004). Database Systems: A Practical Approach to Design,
Implementation, and Management, 4th Edition. Addison Wesley. Chapters 8 & 17.