0% found this document useful (0 votes)
332 views

Database Development and Implementation Lesson

This document provides information on SQL programming and database development using MySQL. It discusses how to create, view, select, and drop databases in MySQL. It also covers how to create tables, define constraints like primary keys, foreign keys, and more. The document concludes with an activity to create tables for an online shop database with various constraints defined.

Uploaded by

Yenuma Lanbon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
332 views

Database Development and Implementation Lesson

This document provides information on SQL programming and database development using MySQL. It discusses how to create, view, select, and drop databases in MySQL. It also covers how to create tables, define constraints like primary keys, foreign keys, and more. The document concludes with an activity to create tables for an online shop database with various constraints defined.

Uploaded by

Yenuma Lanbon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

DATABASE

DEVELOPMENT AND
IMPLEMENTATION
BOANSI, KUFUOR OLIVER
SQL PROGRAMMING
• Remember these about SQL:

• The language is not case-sensitive

• A query is not deemed to be complete unless there is a semi-colon(;) and


hence it wont be executed
CREATING A DATABASE IN MYSQL
• To create a database in MySQL, the syntax is as follows
• CREATE DATABASE databasename;

• Lets create a database called Accomodation.


• To do this, the line below will ensure that.

• Create Database Accomodation;


VIEWING THE DATABASE
• To see the database created and other databases, the query from the
command prompt is:

• Show Databases;

• This query will list all databases available.


SELECTING A DATABASE TO USE
• In order to work with a database in SQL, it must be selected.
• The query format
• USE databasename;

• To select the Accomodation database for use, the query would be:
• Use Accomodation;

• On a successful execution, a message, “Database changed” is


displayed.
DROPPING A DATABASE
• The DROP Database statement is used to discard an existing SQL
database.
• A user must be careful when dropping a database. This will result in loss of all
information stored in the database.

• The syntax to drop a table is


• DROP DATABASE databasename;

• So to drop our database named Accomodation, the query would be…


• DROP DATABASE Accomodation;
CREATING A TABLE
• To create a database table, the syntax is below:

• CREATE TABLE tablename (column1 datatype(Size) Constraint,


• column2 datatype (Size) Constraint,
• …
• column N datatype (Size) constraint
• );
CONSTRAINTS
• They are used to specify rules for data in table.
• Specification of the constraint can be done during or after the
creation of the table
• These constraints includes:
• NOT NULL
• UNIQUE
• Default
• Check
• Primary Key
• Foreign Key
NOT NULL CONSTRAINT
• Columns by default accept null values or entries.

• The NOT NULL constraint ensures that a column always contains a


value

• What this means is that a column or field cannot be updated or


inserted without a value.
NOT NULL EXAMPLE
• Example, to ensure that the columns “LastName” and
“PhoneNumber” will not accept NULL values, the syntax will be:

• PhoneNumber varchar(10) NOT NULL,

• LastName varchar(20) NOT NULL


UNIQUE CONSTRAINT
• The UNIQUE constraint ensures that all values in a record or column
are different.
• By default, a primary key constraint has the unique constraint.
• Example: To ensure that the “Address” column is different for every
entry in the table, the syntax below ensures that in MySQL.
• UNIQUE (ColumnName)

• UNIQUE (Address)
DEFAULT CONSTRAINT
• This constraint is used to provide a default value for a column if no
value is entered. The syntax is:
• ColumnName datatype DEFAULT ‘value’

• Example: To make the default value for a column called “Region” Volta
Region, the query will be:
• Region varchar(20) DEFAULT ‘Volta Region’;
CHECK CONSTRAINT
• This constraint is used to limit the value range that can be placed in a
column.
• EXAMPLE: To accept ages greater than or equal to 20 years in the Age
column of a table, the syntax would be:
• Age int NOT NULL,
• Check (Age>=20)

• This constraint appears not to work on MySQL. But it is worth lnowing


that SQL provides that feature.
PRIMARY KEY(PK) CONSTRAINT
• Primary key constraints helps in the unique identification of a row in a
table.
• Primary keys are NOT NULL and UNIQUE
• A table can have one PK which could be from a single column or multiple
columns

• To make a column a primary key, the syntax is:


• PRIMARY KEY (columnName)

• To make multiple columns a primary key, the syntax is:


• PRIMARY KEY (columnName1, ColumnName2...ColumnNameN)
PRIMARY KEY EXAMPLE
• To make a column called CellNO the primary key of a table in MySQL,
the query would be like:
• Primary Key (CellNO)

• To make these columns (OrderID, OrderDate) primary key, i.e. is a


composite key, the query will be:

• Primary Key (OrderID, OrderDate)


FOREIGN KEY
• A foreign key is used to link two tables

• It’s a column or collection of columns in one table (Child table) that


refers to the primary key in another table (Referenced or parent table)
• The “one side” in the relation is the parent and the “many side”, the
child
FOREIGN KEY SYNTAX
• FOREIGN KEY (Column(s)) REFERENCES ParentTable(Columns)
• ON DELETE ACTION
• ON UPDATE ACTION

• The FOREIGN KEY clause specifies the columns in the child table that refers
to primary key columns in the parent table

• The REFERENCES clause specifies the parent table and its columns to which


the columns in the child table refer. The number of columns in the child table
and parent table specified in the FOREIGN KEY and REFERENCES must be the
same.
ON DELETE OR UPDATE ACTIONS
• CASCADE
• ask MySQL to delete/update records in the child table that refers to a record in the parent table when
the record in the parent table is deleted/updated.
• SET NULL
• If you don’t want the related records in the child table to be deleted, you use the ON DELETE SET
NULL action instead. MySQL will set the foreign key column values in the child table to NULL when
the record in the parent table is deleted, with a condition that the foreign key column in the child
table must accept NULL values.

• NO ACTION

• RESTRICT
• These two(NO ACTION and RESTRICT rejects deletion and updates)
ACTIVITIES
• CREATE A DATABASE CALLED “OBSHOPRITE”
• CREATE THE TABLES IN THE DIAGRAM WITH ALL THEIR ATTRIBUTES
• CHOOSE APPROPRIATE DATA TYPES FOR THE ATTRIBUTES
• IN THE CUSTOMER TABLE, MAKE THE DEFAULT VALUES FOR CITY AND
COUNTRY “JASIKAN” AND “GHANA” RESPECTIVELY
• IN THE ORDER TABLE, MAKE THE DEFAULT VALUE FOR COMMENT,
“NO COMMENT”
• ENFORCE THE CONSTRAINT THAT WILL ENSURE THAT UPON
DELETION, THE CHANGE WILL AFFECT BOTH THE CHILD AND PARENT
IMPLEMENT THIS
ASSIGNMENT
• PRODUCE A TWO PAGE REPORT ON MySQL STORAGE ENGINES OR
TABLE TYPES

• THERE ARE ABOUT 7 OF THEM

• THIS IS AN INDIVIDUAL ASSIGNMENT!!! 

You might also like