DB Practical SQL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

University of Salahaddin -Erbil

PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

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.

• What are the advantages of SQL?


1. Almost every major DBMS supports SQL, so learning
this one language will enable you to interact with
just about every database you'll run into.
2. SQL is easy to learn.
3. SQL is actually a very powerful language.

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

• Numeric Data Types


Numeric Types Description
TINYINT A very small integer
SMALLINT A small integer
MEDIUMINT A medium-sized integer
INT A standard integer
BIGINT A large integer
DECIMAL A fixed-point number
A single-precision floating-
FLOAT
point number
A double-precision floating-
DOUBLE
point number
BIT A bit field
MySQL Data Types(Cont.)
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

• Text
Text Types Description

CHAR A fixed-length non-binary (character) string

VARCHAR A variable-length non-binary string


BINARY A fixed-length binary string
VARBINARY A variable-length binary string
TINYBLOB A very small BLOB (binary large object)
BLOB A small BLOB
MEDIUMBLOB A medium-sized BLOB
LONGBLOB A large BLOB
TINYTEXT A very small non-binary string
TEXT A small non-binary string
MEDIUMTEXT A medium-sized non-binary string
LONGTEXT A large non-binary string
An enumeration; each column value may be
ENUM
assigned one enumeration member
A set; each column value may be assigned zero
SET
or more set members
MySQL Data Types(Cont.)
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

• Date and Time Data Types

Date and Time Types Description

DATE A date value in ‘YYYY-MM-DD’ format

TIME A time value in ‘hh:mm:ss’ format

A date and time value in


DATETIME
‘YYYY-MM-DD hh:mm:ss’ format

A timestamp value in ‘YYYY-MM-DD


TIMESTAMP
hh:mm:ss’ format

YEAR A year value in YYYY or YY format


PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

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

CREATE TABLE Student (


studentId int(11) NOT NULL
AUTO_INCREMENT,
Sname varchar(45),
department varchar(45) DEFAULT
NULL,
start_date DATE,
address varchar(200) DEFAULT
“Erbil”,
PRIMARY KEY (studentId )
);
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,...);

• These allow you to insert data to the DB table.


• After inserting data to table use the command to
show all records:
SELECT * FROM table_name;

13
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

Insert operation (cont.)


• INSERT INTO Students VALUES(1, ‘Azad',
‘Software', ‘2016-02-12', 'Hawler');

• Another to multiple values will be added


using a command:
INSERT INTO Students VALUES
(2,’Shna',‘Software', ‘2016-03-12', 'Hawler'),
(3,‘Sara',‘Survey', ‘2015-02-12', 'Hawler'),
(4,‘Ahmad',‘Software',‘2016-01-10','Hawler');

14
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

Retrieve Operation
• SELECT *
FROM Students;

• SELECT Sname , department


FROM Students;

15
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

MySQL WHERE statement


• The WHERE clause allows you to specify exact rows
to select based on condition.
• SELECT Sname , department
FROM Students
WHERE Sname = “Ali”;
• You can form a very simple condition like the query
above, or a complex one which combines multiple
expressions with logical operators such as AND and
OR.
• >, <, IS Null, IS NOT NULL,[NOT] between x
and y, Like’S%’, NOT Like ‘_ _ _’,

16
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

Write SQL to:


1. Insert information to the table.
2. Retrieve all student names which is in software
department.
3. Retrieve student name and their department for
which they are in Erbil or Sulaymania.
4. Retrieve all student names which they are in
started at date 1-10-2018.
5. Retrieve address for students in which name is “Ali
Azad”
6. For each department find number of students, sort
the information from A-Z.

17
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

Syntax of SQL Retrieve


SELECT column_1,column_2...
FROM table_1,table_2
WHERE conditions
GROUP BY group
HAVING group_conditions
ORDER BY column_1 [ASC | DESC],….

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.

• Note: after operation use retrieve


commands to see the effect of operation.

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 two table as follows:


1. Department ( depid , depname)
2. Student (id , sname , dateOfBirth, depid)
3. Make a relation between them
4. Insert data to the tables
5. Write a query to delete student information for
those in the department software.
6. Write a query to update department id to 2 for
student “Mahmood Omer”.
7. Retrieve the information of students for those born
in 1990th , and sort ascending by student name.
8. Retrieve the number of students for each
department.
9. Use alter keyword to change the table schema.
21
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

Update Table Schema


• Add new column:
ALTER TABLE table_name
ADD column_name datatype [AFTER column_name];
• Delete column:
ALTER TABLE table_name
DROP COLUMN column_name;
• Change column properties:
ALTER TABLE table_name
ALTER COLUMN column_name your_changes;

24
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

Update Table Schema(cont.)


• Change table name:
ALTER TABLE table_name
RENAME TO new_name;
• Add foreign key:
ALTER TABLE table_name
ADD CONSTRAINT FKName FOREIGN KEY (P_Id)
REFERENCES table_table(P_Id);
• Delete a foreign key :
ALTER TABLE table_name
DROP FOREIGN KEY FKName;

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;

• ALTER TABLE student


DROP COLUMN address;

• ALTER TABLE student


ALTER COLUMN address set DEFAULT “Erbil";

26
Update Table Schema
PDF Created with deskPDF PDF Creator X - Trial :: http://www.docudesk.com

example (cont.)
• ALTER TABLE student RENAME TO st1;

• ALTER TABLE student


ADD CONSTRAINT aa FOREIGN KEY (depid)
REFERENCES department (depid);
• ALTER TABLE student
ADD FOREIGN KEY (depid)
REFERENCES department(depid);

• ALTER TABLE student


DROP FOREIGN KEY aa;

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

SQL Foreign Key


• CONSTRAINT anyName FOREIGN KEY
(column_name) REFERENCES
table_name(primary_key)
• ON DELETE , ON UPDATE (SET NULL , CASCADE)
• ON DELETE SET NULL
• ON DELETE CASCADE

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

You might also like