Module 2-3
Module 2-3
SQL scripts are useful for making complex database changes and can be used to create, modify, or delete database objects such as tables, views, stored procedures, and
functions.
Create tables
You can use SQL scripts to create new tables in your database. This is useful when you need to add new functionality to your application or when you want to store
new types of data.
Drop tables
SQL scripts often have commands to Drop tables from databases. This is especially important before Create table commands to make sure that a table with the same
name doesnt exist in the database already.
Insert data
SQL scripts can also be used to insert data into your tables. This is useful when you need to populate your database with test data or when you want to import data
from an external source.
Update data
You can use SQL scripts to update existing data in your tables. This is useful when you need to correct errors or update records based on changing business
requirements.
Delete data
SQL scripts can also be used to delete data from your tables. This is useful when you need to remove old or obsolete records from your database.
Create views
Views are virtual tables that allow you to query data from multiple tables as if they were a single table. You can use SQL scripts to create views that simplify
complex queries and make it easier to work with your data.
Create triggers
Triggers are special types of stored procedures that are automatically executed in response to certain events, such as an insert, update, or delete operation. You can
use SQL scripts to create triggers that enforce business rules and maintain data integrity.
1. SQL scripts are basically a set of SQL commands compiled in a single file.
2. Each command must be terminated with a delimiter or terminator. Most often, the default delimiter is a semicolon ;.
3. It is advisable to keep the extension of the file as .sql.
4. Upon importing this file in the phpMyAdmin interface, the commands in the file are run sequentially.
about:blank 1/3
4/10/25, 5:03 PM about:blank
CREATE TABLE MEDICAL_PROCEDURES (
PROCEDURE_ID CHAR(9) NOT NULL,
PROCEDURE_NAME VARCHAR(30),
PROCEDURE_DATE DATE,
PATIENT_ID CHAR(9) NOT NULL,
DEPT_ID CHAR(9),
PRIMARY KEY (PROCEDURE_ID)
);
CREATE TABLE MEDICAL_DEPARTMENTS (
DEPT_ID CHAR(9) NOT NULL,
DEPT_NAME VARCHAR(15),
MANAGER_ID CHAR(9),
LOCATION_ID CHAR(9),
PRIMARY KEY (DEPT_ID)
);
CREATE TABLE MEDICAL_LOCATIONS (
LOCATION_ID CHAR(9) NOT NULL,
DEPT_ID CHAR(9) NOT NULL,
LOCATION_NAME VARCHAR(50),
PRIMARY KEY (LOCATION_ID, DEPT_ID)
);
This script incorporates commands to first drop any tables with the mentioned names in the database. After that, the script contains commands to create 5 different tables.
All these commands are executed sequentially on the interface.
The contents of this file can be saved in a .sql file format and executed on the phpMyAdmin interface. This can be done by first selecting the database, uploading the SQL
script in the provided space, and executing it, as shown in the image below.
Upon successful execution of each statement in sequence, an note appears on the interface as shown in the image below. It is also prudent to note that the tables created
are now visible in the tree structure on the left under the selected database.
about:blank 2/3
4/10/25, 5:03 PM about:blank
You may click any of the tables to see its Table Definition (its list of columns, data types, and so on). The image below displays the structure of the table PATIENTS.
Author(s)
Abhishek Gagneja
about:blank 3/3