Download 1 introduction database definition (1)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
DATABASE
PROGRAMMING
LECTURER:
GOLOOBA MOSES (DR.)
P H D I T, M I T, P G D I T, B I T
E-MAIL: [email protected]
T E L : 0A7 7P 3R A C5 T8 I2T I4O8N7E R/ S0 A
7 P5 P1R 0O5A0C H9 7 6
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]
C R E AT I O N , A C T I V AT I O N
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
O P E R AT I O N S , C R E AT I O N ,
M A N I P U L AT I O N , E X A M P L E S
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,
CustomerName CHAR(45) NOT NULL, MobileNumber INT NOT
NULL, Email CHAR(65) NOT 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: MULTICOLUMN 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) );