Faculty of Applied Sciences - Rajarata University of Sri Lanka
COM 1302 – Database Management Systems
Practical Sheet 01
Introduction
● My SQL is the database management system we are working with.
● It is an Open Source Database Management System available online to be downloaded
freely.
Starting Work with MySQL
1. Open Command Prompt
2. Enter a copy of the link to your MySQL Bin Directory in the Command Prompt
cd <path to your MySQL Bin Directory>
3. Log in to MySQL by typing (consider your username as root)
mysql -u root -p
4. Give your password
5. You will see mysql> in your command prompt. This means that you have successfully logged
into MySQL.
MySQL Data Types
● MySQL Data Types help you to properly define fields in a table, for overall optimization of
your database.
● MySQL uses many different data types broken into three categories. They are as
follows.
A) String Data Types
B) Numeric Data Types
C) Date and Time
1. Create Database
Here is a generic SQL syntax to create a MySQL Database. The name of the database we are
creating is EMPLOYEE.
CREATE DATABASE COMPANY;
2. Use the Created Database
The USE statement tells MySQL to use the named database as the default (current) database
for subsequent statements.
USE COMPANY;
3. Create Tables
To begin with, the table creation command requires the following details.
● Name of the table
● Name of the fields
● Definitions for each field
Now, we will create the following table in the EMPLOYEE database.
CREATE TABLE EMPLOYEE(
Fname VARCHAR(15),
Minit CHAR,
Lname VARCHAR(15),
Ssn CHAR(9),
Bdate DATE,
Address VARCHAR(15),
Sex CHAR,
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT);
4. Get a Description of the Tables
● We use the DESCRIBE command to show the structure of our table, such as column
names, constraints etc.
● The DESC command is a short form of the DESCRIBE command.
DESCRIBE employee;
DESC employee;
5. Modify the Tables
MySQL allows you to modify tables. The following commands will help you with them.
Add new columns
ALTER TABLE EMPLOYEE
ADD tel_no VARCHAR (255);
Drop columns
ALTER TABLE EMPLOYEE
DROP COLUMN tel_no;
Modify Columns
ALTER TABLE EMPLOYEE
MODIFY COLUMN Address VARCHAR(30);
6. Insert Data to the Table
There are two methods to insert data to the table.
Method 1
INSERT INTO EMPLOYEE
(Fname, Minit, Lname, Ssn, Bdate, Address, Sex,Salary, Super_ssn, Dno)
VALUES
('John','B','Smith','12345789','1965-01-09','731 Fondren, Houston, TX','M',30000,'33344555',5);
Method 2 - Insert Multiple Records
INSERT INTO EMPLOYEE(
Fname, Minit, Lname, Ssn, Bdate, Address, Sex,Salary, Super_ssn, Dno)
VALUES
('Franklin','T','Wong','33344555','1955-12-08','638 Voss, TX','M',40000,'88866555',5),
('Alicia','J','Zelaya','999887777','1968-01-19','3321 Castle, Houston, TX','F',25000,'987654321',4);
7. Update Tables
There are several ways in which you can update a table.
Update a single record
UPDATE EMPLOYEE
SET Fname = 'Nimal'
WHERE Ssn ='33344555';
Update Multiple Records
UPDATE EMPLOYEE
SET salary = 50000.00
WHERE Dno=5;
Update Multiple Columns
UPDATE EMPLOYEE
SET salary = 45000.00, Dno='2'
WHERE Super_ssn='88866555';
Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
8. Delete
You can delete data in tables in the following methods.
Delete Specific Records
DELETE FROM EMPLOYEE WHERE Fname = 'Alicia';
Delete All Records
DELETE FROM EMPLOYEE;
Drop a Database
DROP DATABASE COMPANY;