1 Introduction Database Definition
1 Introduction Database Definition
PROGRAMMING
LECTURER:
GOLOOBA MOSES (DR.)
PHD IT, MIT, PGDIT, BIT
E-MAIL: [email protected]
TEL: 0773 582 487 / 0751 050 976
A PRACTITIONERS APPROACH
INTRODUCTION
Before tables and other objects can be created, a general schema that
contains these objects is created.
Example:
USE traveldb;
NB:
The USE command activates the database so that any queries are directed
towards it, since there are many databases in a single database server
TABLES
OPERATIONS, CREATION, MANIPULATION,
EXAMPLES
TABLES
NB:
Table operations are not reversible – no UNDO
CREATING TABLES:
SINGLE COLUMN PRIMARY KEY
Syntax:
To create a new table the following general syntax applies:
CREATE TABLE table_name(column_1 <Data Type>, column_2
<Data Type> <Constraint>,…, column_n <Data Type> <Constraint>
PRIMARY KEY (column);
Where:
<Data Type> can be CHAR, INT, NUMBER etc.
<Constraint> can be PRIMARY KEY, NOT NULL etc.
…
• Example:
– Consider a table with the following definition:
• Column Data Type Constraint
• CUSTOMERID INT Primary Key
• CUSTOMER NAME CHAR NOT NULL
• MOBILE NUMBER INT NOT NULL
• EMAIL CHAR NOT NULL
• BIRTHDATE DATE NOT NULL
…
NB:
Auto increment facility is provided to enable automatic generation of
continuous numbers without much user intervention.
Primary Key fields must be defined for a proper table
…
Tasks:
Given the following table structure, create the table in your current database:
AGENT:
Column Data Type Constraint
AGENTID INT Primary Key
AGENTNAME CHAR NOT NULL
MOBILENUMBER INT NOT NULL
EMAIL CHAR NOT NULL
PHYSICALADDRESS VARCHAR NOT NULL
COUNTRYOFSPECIALITY CHAR NOT NULL
…
• Example:
• Consider the following table definition
• TOUR:
• Column Data Type Constraint
• RESORTID INT Primary Key
• CUSTOMERID INT Primary Key
• NOOFDAYS INT NOT NULL
• TOURDATE DATE Primary Key
• EMPLOYEEID INT NOT NULL
• PACKAGEDETAILS TEXT
…
Syntax:
PAYROLL
EmployeeID
SalaryMonth (eg JANUARY, FEBRUARY)
BasicSalary
NSSF
PAYE
NETPAY
…
• Solution
– CREATE TABLE payroll(employeeID INT, salaryMonth
CHAR(10),basicSalary DOUBLE NOT NULL, nssf DOUBLE NOT
NULL, paye DOUBLE NOT NULL, netPay DOUBLE NOT NULL,
PRIMARY KEY(employeeID,salaryMonth) );