DATABASE
PROGRAMMING
LECTURER:
GOLOOBA MOSES (DR.)
PHD IT, MIT, PGDIT, BIT
E-MAIL: mousagmoses@[Link]
TEL: 0773 582 487 / 0751 050 976
A PRACTITIONERS APPROACH
INTRODUCTION
The largest number of business software systems today depend on
databases.
Database management systems e.g. MySQL, ORACLE, SQL
SERVER etc. provide the data storage and querying capability that is
not available in many other systems
Today’s modern database management systems support advanced
programmability to the extent that entire systems can be built fully at
the database layer alone
LEARNING OUTCOMES
By the end of the course students are expected to be able to:
Create and manipulate data structures like tables and views.
Insert and manipulate data within a given table.
Perform adhoc querying on single and multiple tables based on
established data requirements.
Create programs [stored procedures] to perform additional computation
and data manipulation operations.
Build interfaces for User-Database Interaction
SCHEMAS
[DATABASES]
CREATION, ACTIVATION
INTRO
Before tables and other objects can be created, a general schema that
contains these objects is created.
The schema is referred to as a database.
To create and activate a database the following syntax is used:
CREATE DATABASE <Database Name>;
USE <Database Name>;
…
Example:
CREATE DATABASE traveldb;
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
• Tables are the primary data containers.
• A table definition is composed of mainly 3 issues:
– Table Name – which must be unique
– Table attributes – Columns and their data types
– Table Constraints – Keys [Primary & Foreign], Unique, Check and
others
TABLE OPERATIONS
CREATE – Enables the generation of a new table
ALTER – Enables modification to a table’s structure by adding or
removing columns, changing column properties etc
DROP – Deletes the table from the database
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
…
The syntax for creation of such a table would be:
CREATE TABLE customer(CustomerID INT AUTO_INCREMENT,
AUTO_INCREMENT
CustomerName CHAR(45) NOT NULL,
NULL MobileNumber INT NOT
NULL,
NULL Email CHAR(65) NOT NULL,
NULL BirthDate DATE NOT NULL,
PRIMARY KEY (CustomerID) );
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
…
Create the definitions and implement the following tables in MySQL:
Resort
(resortID[PK],
ResortName,
Region
Lowest Room Price,
Employee
(EmployeeID[PK],
Employee Name,
Mobile Number,
Email,
Basic Salary (Monthly))
CREATING TABLES: MULTI-
COLUMN PRIMARY KEYS
Syntax:
The general syntax is the same except that the primary key definition
comes at the end of the column specification:
CREATE TABLE table_name (column_1 <Data Type> <Constraint>,
column_2 <Data Type> <Constraint>,…., column_n <Data Type>
<Constraint>, PRIMARY KEY (column_1, column_2))
NB:
The 2 columns column_1 and column_2 are the composite primary key for
the table
…
• 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:
CREATE TABLE tour(resortID INT, customerID INT, no_days INT
NOT NULL, tourDate DATE, employeeID INT NOT NULL,
packageDetails TEXT, PRIMARY KEY
(resortID,customerID,tourDate), FOREIGN KEY(resortID)
REFERENCES resort(resortID), FOREIGN KEY(customerID)
REFERENCES customer(customerID) );
TASK
Create the following table using an appropriate SQL statement: Choose
appropriate data types
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) );