You are on page 1of 33

College of electronics technology

________Baniwalid _______

Introduction to Structure query language

(SQL)

Week 11
Mustafa Abuali spring 2017
Outline
 Working with tables.
 How to create table.
 How to drop table.
 How to alter table.
 Working with constrains.
 Working with Data.
 Insert new data rows .
 Update existing data.
 Delete existing row.
 Retrieving Data.
 Select statement .
 Where .
 Join table .
 Grouping data.
 Sorting data.
Introduction

• Structured Query Language (SQL):

– Popular and widely used language for retrieving and


manipulating database data

– Developed in mid-1970s under the name SEQUEL

– Renamed SQL in 1980

– Used by most DBMSs


Client-Server Interaction

Make a request
(SQL query)

MySQL Client
Server Get results Program

Client program can be a MySQL command line client,


GUI client, or a program written in any language such
as C, Perl, PHP, Java that has an interface to the
MySQL server.
SQlServer Vs MySQL Vs Oracle
Data types
SQL CREATE TABLE statement

The basic syntax for create table is :

-each column must have a datatype.


-the column should either be defined as “null” or “not null”.
-can use default value
WARNING WARNING

 WARNING
 Always assume that everything is case
sensitive, especially table names.
Working with table (CREATE TABLE )
Create table
The DROP Command

 DROP TABLE table_name;


For example (DROP TABLE Employee).
Also for d_b
 DROP DATABASE db_name;

 Note: Don't confuse DROP with DELETE which deletes


rows of a table.
Don’t forget Entering commands (1)
 Show all the databases
 SHOW DATABASES;
mysql> SHOW DATABASES;
+-------------+
| Database |
+-------------+
| bookstore |
| employee_db |
| mysql |
| student_db |
| test |
| web_db |
+-------------+
Entering commands (2)
 Choosing a database and showing its tables
 USE test;
SHOW tables;
mysql> USE test;
Database changed
mysql> SHOW tables;
+----------------+
| Tables_in_test |
+----------------+
| books |
| name2 |
| names |
| test |
+----------------+
4 rows in set (0.00 sec)
mysql>
Entering commands (3)
 Show the structure of a table
 DESCRIBE names;
mysql> DESCRIBE names;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| firstName | varchar(20) | | | | |
| lastName | varchar(20) | | | | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql>
Alter Table
Alter Table
Working with Data.

Insert new data rows .


Update existing data.
Delete existing row.
INSERT
 To Inserting rows into a table use insert statement
 The syntax for the INSERT statement is :

 Here you must apply the column order as the table


organized
For example :

INSERT INTO Supplier


VALUES (100,’IBM’,’Mr Ahmed’);
Insert with specified column

 The inset statement allows to insert a single or multiple records into the table.

 Here you can specify the column as you write . By attention with PK,NOT NULL
For example :
Update data .

 The inset statement allows to UPDATE a single or multiple records into the table.
 The syntax is .

Here you apply the changes to all values stored in this column .there is problem

Here all the value of name column will be changed into “HP”.
UPDATE with condition
 The syntax for update statment is :

Here you apply the change on specific record or specific


Update Example

Here only the supplier_name with the value IBM will be changed to HP
DELETE existing data

 The inset statement allows to DELETE a single or multiple records into the
table.
 The syntax is .

Here you will delete all the data rows from the table .

Here you will delete all data inside the supplier table . Not table !!!(Drop !!!!*)
Delete with condition
 The syntax is :

 Example:

Here you delete only the data rows that meet the where condition .
Retrieving Data.(select statement)
 The select statement allows to Retrieve a single or multiple records from one
or more table in your database .
 The syntax is

 For Example :
Tables
Select statement (where)

____For Example:____
Simple SQL Query

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT *
FROM Product
WHERE category=‘Gadgets’

PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
“selection” 30
Simple SQL Query

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100

PName Price Manufacturer


“selection” and SingleTouch $149.99 Canon
“projection” MultiTouch $203.99 Hitachi
31
Eliminating Duplicates

Category
SELECT DISTINCT category Gadgets
FROM Product
Photography
Household

Compare to:

Category
Gadgets
SELECT category Gadgets
FROM Product
Photography
Household
32

You might also like