DATABASE DESIGN AND DEVELOPMENT
USING SQL IN THE IMPLEMENTATION OF A DATABASES
STRUCTURED QUERY LANGUAGE (SQL).
SQL is a database computer language designed for the retrieval and management of data in a
relational database. SQL stands for Structured Query Language.
SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database
Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and
SQL Server use SQL as their standard database language.
Why SQL?
SQL is widely popular because it offers the following advantages −
• Allows users to access data in the relational database management systems.
• Allows users to describe the data.
• Allows users to define the data in a database and manipulate that data.
• Allows to embed within other languages using SQL modules, libraries & pre-compilers.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views.
SQL IDEs
Several IDEs (Integrated Development Environments) are widely used for coding SQL in
different Database Management Systems (DBMS). Here are some popular options based on
specific DBMSs:
1. MySQL
• MySQL Workbench: Official IDE from MySQL, offering SQL development, data
modeling, server administration, and more.
• phpMyAdmin: A web-based IDE primarily used for managing MySQL databases.
• DBeaver: A versatile, cross-platform IDE that supports multiple databases, including
MySQL.
2. PostgreSQL
@MBOM 1
• pgAdmin: The official PostgreSQL IDE for database administration and SQL
development.
• DataGrip (JetBrains): A powerful IDE for multiple databases, including PostgreSQL,
with great SQL code support.
• DBeaver: Supports PostgreSQL along with various other databases.
3. Microsoft SQL Server
• SQL Server Management Studio (SSMS): The official tool for managing SQL Server
databases and executing SQL queries.
• Azure Data Studio: A lightweight, cross-platform alternative to SSMS, with excellent
SQL development features.
• DataGrip: Also supports Microsoft SQL Server with advanced SQL coding features.
4. Oracle
• SQL Developer: Oracle's official IDE for SQL and PL/SQL development.
• Toad for Oracle: Another popular IDE for Oracle database management and
development.
5. SQLite
• DB Browser for SQLite: A simple, open-source IDE for creating, designing, and editing
SQLite databases.
• SQLiteStudio: A cross-platform IDE specifically designed for SQLite.
6. MongoDB (NoSQL)
• MongoDB Compass: The official MongoDB GUI/IDE for database exploration and
query execution.
• Robo 3T: A lightweight GUI tool for MongoDB that supports running queries.
7. Cross-Platform IDEs (Multiple DBMS)
• DBeaver: One of the most versatile IDEs that supports many SQL databases (e.g.,
MySQL, PostgreSQL, SQLite, Oracle, etc.).
• HeidiSQL: Primarily for MySQL, MariaDB, and PostgreSQL, but it can connect to other
DBMSs.
• DataGrip: A robust multi-database IDE supporting numerous SQL databases with
advanced features like code completion and query analysis.
@MBOM 2
SQL DATA TYPES
SQL Data Type is an attribute that specifies the type of data of any object. Each column, variable
and expression has a related data type in SQL. You can use these data types while creating your
tables. You can choose a data type for a table column based on your requirement.
SQL data types can be broadly divided into following categories.
1) Numeric data types such as int, tinyint, bigint, float, real etc.
2) Date and Time data types such as Date, Time, Datetime etc.
3) Character and String data types such as char, varchar, text etc.
4) Boolean data types such as binary, varbinary etc.
5) Currency data type such as money
DATABASE DESIGN
PatientNo---->PatientFName, PatientLName, PatientWardCode, LabTestCode [PATIENT
TABLE]
PatientWardCode----> PatientWardName [WARD TABLE]
LabTestCode----> LabTestName [LAB TABLE]
DoctorID----> DoctorFName, DoctorLName, NoOfPatients [DOCTOR TABLE]
PatientNo, DoctorID----> PatientPrescription [PRESCRIPTION TABLE]
ENTITY RELATIONSHIP DIAGRAM
Ward 1 1..* Patient 1..* 1 Lab
1..*
1..* 1
Prescription Doctor
@MBOM 3
DATA DICTIONARY
WARD TABLE
Attribute Data type Data size Constraint
PatientWardCode int Primary key
PatientWardName varchar 20
LAB TABLE
Attribute Data type Data size Constraint
LabtestCode Char 15 PK
LabTestName varchar 25
PATIENT TABLE
Attribute Data type Data size Constraint
PatientNo char 15 PK
PatientFName varchar 25
PatientLName varchar 30
PatientWardCode int Foreign key
LabtestCode Char 15 FK
DOCTOR TABLE
Attribute Data type Data size Constraint
DoctorID int PK
DoctorFName Varchar 30
DoctorLName varchar 40
NoOfPatients int
PRESCRIPTION TABLE
Attribute Data type Data size Constraint
PatientNo char 15 PK,FK
DoctorID int PK,FK
PatientPrescription varchar 50
@MBOM 4
SQL CREATE TABLE COMMAND
The CREATE TABLE statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example
@MBOM 5
INSERTING DATA INTO TABLES
The INSERT INTO statement is used to insert new records in a table.
Syntax
It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
2. If you are adding values for all the columns of the table, you do not need to specify the
column names in the SQL query. However, make sure the order of the values is in the same
order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
@MBOM 6
Use the following SQL statements to insert data into the tables that you have created
@MBOM 7
@MBOM 8
THE SQL AGGREGATE FUNCTIONS
COUNT FUNCTION
The COUNT() function returns the number of rows that matches a specified criterion.
COUNT() Syntax
SELECT COUNT(column_name) AS (Alias)
FROM table_name
WHERE condition;
@MBOM 9
AVERAGE FUNCTION
The AVG() function returns the average value of a numeric column.
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
@MBOM 10
SUM FUNCTION
The SUM() function returns the total sum of a numeric column.
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
@MBOM 11
MIN() and MAX() FUNCTIONS
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
@MBOM 12
THE SQL UPDATE STATEMENT
The UPDATE statement is used to modify the existing records in a table.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
@MBOM 13
WRITING SQL QUERIES
The SQL SELECT Statement
▪ The SQL SELECT Statement is used to fetch the data from a database table which returns
this data in the form of a table. These tables are called result-sets.
CLAUSES and OPERATORS available in SQL can be used with the SELECT statement in order
to retrieve the filtered records of a database table.
Syntax
The basic syntax of the SELECT Query is as follows −
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2... are the fields of a table whose values you want to fetch.
If you want to fetch all the columns available in a table, then you can use the following syntax −
SELECT * FROM table_name;
Examples
1. Write the SQL statement that will select all the attributes in the ward table
2. Write the SQL statement that will display the patient number, patient first name and patient
last name.
3. Write the SQL statement that will display the patient number, patient first name and patient
last name. Order by patient first name in descending order.
The SQL WHERE Clause
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a
specified condition.
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
Different types of the JOINs in SQL include:
1. (INNER) JOIN
Returns records that have matching values in both tables
2. LEFT (OUTER) JOIN
Returns all records from the left table, and the matched records from the right table
3. RIGHT (OUTER) JOIN
Returns all records from the right table, and the matched records from the left table
@MBOM 14
4. FULL (OUTER) JOIN
Returns all records when there is a match in either left or right table
The SQL ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The SQL GROUP BY Statement
The GROUP BY statement groups rows that have the same values into summary rows, like "find
the number of customers in each country".
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Examples
1. Write the SQL statement that will display the patient ward code and the patient ward name
for all patients whose ward code is greater than 200. Order by ward code in descending order.
2. Write the SQL statement that will list the Patient number, patient last name and patient ward
code for all patients whose ward code is equal to or is greater than 124.
3. Write the SQL statement that will list the doctor identification, doctor last name, the patient
number and the prescription for all patients whose patient identification is KNH345. Produce
the result in ascending order of patient last name.
4. Write the SQL statement that will list the doctor identification, doctor last name, the patient
number and the prescription for all patients whose patient identification is KNH345. Produce
the result in ascending order of patient last name.
5. Write the SQL statement that will display the lab test code, lab test name, patient ward name
and patient last name for all patients whose patient ward code is less than or equal to 400.
Order by patient lab test name in ascending order.
SQL ALTER TABLE STATEMENT
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
@MBOM 15
ALTER TABLE - ADD Column
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype(data size);
Example
1. Create the following table and insert data
2. Add a column called Gender and insert data into it.
SOLUTION
Creating and inserting data
Adding gender column and inserting data
@MBOM 16
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE – RENAME COLUMN
To rename a column name in MySQL or SQL Server, you can follow the below syntax:
Syntax
ALTER TABLE TableName
RENAME COLUMN OldColumnName TO NewColumnName;
THE SQL BETWEEN OPERATOR
The BETWEEN operator selects values within a given range. The values can be numbers, text,
or dates.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
@MBOM 17
THE SQL LIKE OPERATOR
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
1. The percent sign (%) represents zero, one or multiple characters
2. The underscore sign (_) represents one, single character
Note:
MS Access uses an asterisk (*) instead of the percent sign (%), and a question mark (?) instead of
the underscore (_).
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE Finds any values that have "or" in any position
'%or%'
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
@MBOM 18
WHERE CustomerName LIKE Finds any values that start with "a" and are at least 2 characters in
'a_%' length
WHERE CustomerName LIKE Finds any values that start with "a" and are at least 3 characters in
'a__%' length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
@MBOM 19