Professional Documents
Culture Documents
DB Practical SQL
DB Practical SQL
DB Practical SQL
College of Engineering
Software &Informatics Engineering Department
SQL
2 stage
nd
Database Lab
Prepared by:
Hanan Kamal
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Introduction
• SQL (Structured Query Language), SQL is a
language designed specifically for communicating
with databases.
2
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Creating Database
• CREATE DATABASE database_name;
• CREATE DATABAS are key word for creating a
DB, this will followed by the meaning full
name for my database.
• To run SQL statement must end with a
semicolon (;).
• SHOW DATABASES;
• SHOW DATABASES used to display the DBs.
3
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Select DB
• USE database_name;
• When you want to work with a DB first must select
the DB by using command (USE database_name)
4
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Delete DB
• DROP DATABASE database_name;
• DROP DATABASE used to delete the DB.
• Every thing will be deleted in the DB all
tables relation (objects totally).
5
MySQL Data Types
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
• Text
Text Types Description
Creating Tables
CREATE TABLE table_name(
column_list
);
• In order to create a table within a
database:
1. first select your database using:
USE database_name;
2. Then use create command to create the DB
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Creating Tables(Cont.)
• To define a column for the table in the
CREATE TABLE statement, you use the
following syntax:
column_name data_type[size] [NOT NULL
|NULL][DEFAULT value] [AUTO_INCREMENT]
• If you want to set particular columns of
the table as the primary key, you use
the following syntax:
PRIMARY KEY (col1,col2,...)
• UNIQUE (col_name) used to uniqly
identify column in the DB, but
Primary Key.
Creating Tables(Cont.)
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Structure of Table
• DESCRIBE table_name; used to view the
structure of the table.
12
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Insert operation
1. INSERT INTO table_name VALUES (value1,
value2, value3,...);
2. INSERT INTO table_name (column1, column2,
column3,...)
VALUES (value1, value2, value3,...);
13
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
14
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Retrieve Operation
• SELECT *
FROM Students;
15
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
16
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
17
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
18
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Delete Operation
• DELETE FROM table [WHERE conditions];
• DELETE FROM Students
WHERE Sname = “Ali”;
o THIS will delete only the row which
satisfy the condition.
• DELETE FROM Students;
o The above will delete all rows.
19
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Update Operation
• UPDATE table_name
SET column_name1=expr1
[,column_name2=expr2 ...]
[WHERE condition]
• UPDATE Students
SET address=“Sulaymaniah”
WHERE id = 1;
20
Activity
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Create table
CREATE TABLE department
(
depid int ,
depname varchar(200),
primary key (depid)
);
22
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Create table
CREATE TABLE student
(
id int ,
sname varchar(200),
DateOfBirth Date ,
depid int ,
primary key (id),
FOREIGN KEY (depid) REFERENCES
department(depid)
);
23
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
24
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
25
Update Table Schema
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
example
• ALTER TABLE student
ADD address varchar(200) AFTER id;
26
Update Table Schema
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
example (cont.)
• ALTER TABLE student RENAME TO st1;
27
28
where DateOfBirth like "1990______";
query
from student
1. SELECT *
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
29
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Home Work
• Write command with simple example for
how to use:
• Aggregate functions in SQL .
• Aliases in the SQL (select , from ) clauses.
30
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com
Home Work
• Soft copy report required:
o How make update in table schema
(structure) using SQL command.
o With examples.
31