You are on page 1of 33

Database Management

Systems I - (Practical)
CST 123-3

I.S.Pathirana
Basic Terms
• Data:
– Data is a raw fact (collection of characters, numeric values,
special characters etc)
– Whatever we input from the keyboard is known as data.
– Data will not provide any meaningful statements to the
user.
– Ex:- ec@mw2lo1e3

Department of Compuer Science and Informatics 2


Basic Terms
• Information:
– Processing the data is called as information.
– Information always provides meaningful statements to
the user.
– Ex:- welcome@123

• Table
–A collection of related data entries with columns and
rows

Department of Compuer Science and Informatics 3


Basic Terms
• Database:
– Database is a collection of information that
can be written in predetermined manner and
saved at a particular location is called database.

• Database Management System (DBMS):


– DBMS is a tool which can be used to maintain & manage
the data with in the database.
– DBMS is used for storing the information, accessing the
information, sharing the information and providing
security to the information.
Department of Compuer Science and Informatics 4
What is SQL???

• Structured Query Language

• A relational database language used for controlling


and interacting with database management systems

• Not a type of DBMS

Department of Compuer Science and Informatics 5


Types of SQL Statements

1) Data Definition Language (DDL)


2) Data Manipulation Language (DML)
3) Data Control Language (DCL)

Department of Compuer Science and Informatics 6


1. Data Definition Language (DDL)

• Used to create new objects, alter the structure of


existing objects, or to remove the objects from the
system
• These statements cannot be used to modify data

Department of Compuer Science and Informatics 7


Contd’
• CREATE TABLE - Creates a new table
• DROP TABLE - Removes an existing table
• ALTER TABLE - Modifies the structure of a table
• CREATE VIEW - Creates a new view
• DROP VIEW - Removes an existing view
• CREATE INDEX - Build an index for a column
• DROP INDEX - Removes an index
• TRUNCATE - Removes all table records

Department of Compuer Science and Informatics 8


2. Data Manipulation Language
(DML)
• These statements change the data in the
database
• INSERT - Adds new rows of data
• DELETE - Removes row of data
• UPDATE - Modifies existing data
• SELECT - Retrieve data

Department of Compuer Science and Informatics 9


3. Data Control Language (DCL)

• These statements control the access of the data in


the database & determines how, when & whom can
manipulate data

• GRANT - Gives user access privileges


• REVOKE - Removes granted privileges

Department of Compuer Science and Informatics 10


ANSI/ISO SQL Keywords

• ANSI/ISO specifies standard SQL keywords which


cannot be used to name databases objects like
tables, columns and users.

• Example of some keywords:

ALL COUNT FOUND MAX PRIVILEGES


AND CREATE FROM MIN REFERENCE
AVG DEFAULT GO NOT ROLLBACK
BEGIN DELETE GRANT NULL SELECT
Department of Compuer Science and Informatics 11
Contd’

BETWEEN DISTINCT GROUP NUMERIC SET


BY END HAVING OF SQL
C EXEC IN OR TABLE
CHAR FETCH INSERT ORDER VIEW
CHARACTER FLOAT INT FOREIGN INTEGER
PRECISION COMMIT FORTRANKEY PRIMARY

• Note: Keywords used may varies with the different


implementations of SQL

Department of Compuer Science and Informatics 12
SQL Data Types

• Numeric, Character string, Boolean, Date and time


are the basic data types

• Those data types may vary from different


implementations of SQL

Department of Compuer Science and Informatics 13


Basic SQL Data Types
• Numeric
– Include integer numbers of various sizes (INTEGER or
INT) and real numbers of various precision (FLOAT or
REAL or DOUBLE).

– Formatted numbers can be declared by using


DECIMAL(i,j) or NUMERIC(i,j) where i is the precision
and j the scale is the number of digits after the decimal
point.

Department of Compuer Science and Informatics 14


Basic SQL Data Types

• Character-string
– Data types are either fixed length –CHAR(n) or
CHARACTER(n) where n is the number of characters or
varying length VARCHAR(n) where n is the maximum
number of characters.

• Boolean
⁻ Boolean data type has the values of TRUE or FALSE

Department of Compuer Science and Informatics 15


Basic SQL Data Types

• Date and time


⁻ The DATE data type has ten positions and its components
are YEAR, MONTH and DAY in the form of YYYY-MM-DD. The
time data type has at least eight positions with the
components HOUR, MINUTE and SECOND in the form of
HH:MM:SS

Department of Compuer Science and Informatics 16


Some Tips for Practical Classes
• To cancel the currently typing command
⁻ mysql> \c

• To clear the terminal


⁻ Press Ctrl+L on the keyboard

• To get previously executed commands


⁻ Press Up and Down arrow keys on the
keyboard
Department of Compuer Science and Informatics 17
Some Tips for Practical Classes
• Copy from the terminal and paste to the terminal
– Right click the mouse -> Copy or Paste
• To exit from MySQL prompt
– mysql> exit
• OR
– mysql> QUIT
• OR
– mysql> \q

Department of Compuer Science and Informatics 18


Some prompts and their meanings in
MySQL

Department of Compuer Science and Informatics 19


Department of Compuer Science and Informatics 20
Let’s Start with MySQL

Department of Compuer Science and Informatics 21


Start with Linux
• Command:
$ mysql –u<username>–p<password>
• Example:
$mysql –uroot –proot

Department of Compuer Science and Informatics 22


• If the server is located in a different machine,
specify the host as well.
$mysql –h<host> –u<username> –p<password>

Department of Compuer Science and Informatics 23


Displaying Databases

• To display the existing databases


SHOW DATABASES;

• This command lists all the databases on the MySQL


server host

Department of Compuer Science and Informatics 24


Creating a Database
• To create a Database
CREATE DATABASE <DATABASE NAME>;

• Example:
CREATE DATABASE Hospital;

Department of Compuer Science and Informatics 25


Using/Accessing the Database
• To access any existing Database
USE <DATABASE NAME>;
• Example:
USE Hospital;

Department of Compuer Science and Informatics 26


Creating a Table
• Syntax:
CREATE TABLE <TableName>
(<Field1> <DataType> (<FieldSize>),
<Field2> <DataType>(<FieldSize>),
<Field3> <DataType> (<FieldSize>));
• Example:
CREATE TABLE Patient (P_ID CHAR(4),
PatientName VARCHAR(30),
Age INT,
Gender CHAR(1));

Department of Compuer Science and Informatics 27


Displaying Tables
• To display an existing Table in a Database
• Syntax:
SHOW TABLES;
• This command lists all the tables in the current
database
• Note: You have to use (go inside) the database
before typing this command

Department of Compuer Science and Informatics 28


Display the Structure of the Table
• To display the information about the columns in a
table (the table structure).
• Syntax
DESCRIBE <TableName>;
OR
DESC <TableName>;

• Example:
DESCRIBE Patient;

Department of Compuer Science and Informatics 29


Removing an Existing Table
• Syntax:
DROP TABLE <TableName>;
• Example:
DROP TABLE Patient;

Department of Compuer Science and Informatics 30


Removing an Existing Database
• Syntax
DROP DATABASE <DatabaseName>;
• Example:
DROP DATABASE Hospital;

Department of Compuer Science and Informatics 31


Summary
• Basic Terms
• Types of SQL Languages / Statements
• ANSI/ISO SQL Keywords
• SQL Data Types
• Creating a Database
• Using the Database
• Creating a Table
• Displaying the Structure of a Table
• Displaying Databases and Tables
• Removing an Existing Table
• Removing an Existing Database
Department of Compuer Science and Informatics 32
Questions???

Department of Compuer Science and Informatics 33

You might also like