You are on page 1of 32

Fundamental Database Lab

1
The MySQL CREATE and Drop DATABASE Statement

 The CREATE DATABASE statement is used to create a new


SQL database.
 Syntax: CREATE DATABASE databasename;
 Example: create database university;
 to show a database : show databases;
 The DROP DATABASE statement is used to drop an
existing SQL database.
 Syntax: DROP DATABASE databasename;
 Example: DROP DATABASE testDB;

2
The MySQL CREATE and Drop Table Statement

 The CREATE TABLE statement is used to create a new table in a


database.
 Before creating table we use database by: use database name;
 Syntax: CREATE TABLE table_name (
    column name datatype,
    column name datatype,
    column name datatype,
   ....
);
 Example : stud table
 create table stud(studId int not null primary key , fname varchar(50),mname
varchar(40),lname varchar(30), address varchar(50), age int(2) , phone_number
int(12));
 To show table: show tables;
3
Cont..

• create table employee(empId int primary key not null, fname


varchar(50),mname varchar(40),lname varchar(30), address
varchar(50), age int(2) , phone_number int(12));
• The DROP TABLE statement is used to drop an existing
table in a database.
• Syntax: DROP TABLE table_name;
• Example:drop table employee;

4
:
The MySQL INSERT INTO Statement

 The INSERT INTO statement is used to insert new records in a table.


 Syntax: INSERT INTO table_name VALUES (value1, value2, value3, ...);

insert into stud values(1,'abebe','kebede','zeleke','AA',12,0911202325);


insert into stud values(2,'hiwot','Lemisa','chala','jimma',16,0917202325);

insert into stud values(3,'dawit','hailu','temesgn','Bdr',18,0918202320);

insert into stud values(4,'gemechu','bikila','abebe','tepi',20,0917504030);

5
The MySQL SELECT Statement

 The SELECT statement is used to select or retrieve data from a


database.
 The data returned is stored in a result table, called the result-set.
• Syntax: SELECT column1, column2, ...
FROM table_name;
• Example: select fname,age from stud;
• Here, column1, column2, ... are the field names of the table you want to select
data from. If you want to select all the fields available in the table, use the
following syntax.
• SELECT * FROM table_name;
• Example: select*from stud;

6
The MySQL WHERE Clause

 The WHERE clause is used to filter records. It is used to extract only those


records that fulfill a specified condition.
• Syntax: SELECT column1, column2, ...
FROM table_name WHERE condition;
 Note: The WHERE clause is not only used in SELECT statements, it is also used
in UPDATE, DELETE, etc.
• Example: SELECT * FROM stud
WHERE studId=2;

7
The MySQL UPDATE Statement

 The UPDATE statement is used to modify the existing records in a


table.
• Syntax: UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
• Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should
be updated. If you omit the WHERE clause, all records in the table will be
updated!
• Example: UPDATE stud SET fname= 'zinash',address='AA' WHERE
studId=4;

8
.
The MySQL DELETE Statement
• The DELETE statement is used to delete existing records in a table
• Syntax:DELETE FROM table_name WHERE condition;
• Note: Be careful when deleting records in a table! Notice the WHERE clause in
the DELETE statement. The WHERE clause specifies which record(s) should be
deleted. If you omit the WHERE clause, all records in the table will be deleted

• Example: delete from stud where studId=6;

9
MySQL ALTER TABLE Statement

 The ALTER TABLE statement is used to add, delete in an existing table.


 The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
• ALTER TABLE - ADD Column
• To add a column in a table, use the following syntax:
• ALTER TABLE table_name
ADD column_name datatype;
• Example
• ALTER TABLE stud
ADD Email varchar(55);

10
• ALTER TABLE - DROP COLUMN
• To delete a column in a table, use the following syntax (notice that
some database systems don't allow deleting a column):
• ALTER TABLE table_name
DROP COLUMN column_name;
• Example: ALTER TABLE stud
DROP COLUMN Email;

11
The MySQL ORDER BY Keyword

 The ORDER BY keyword is used to sort the result-set in ascending or descending order.


 The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
• Syntax: SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
 The following SQL statement selects all students from the “stud" table, sorted by the
“age" column:
• Example: SELECT*FROM stud ORDER BY age;
• ORDER BY DESC Example: The following SQL statement selects all stud from the
“stud" table, sorted DESCENDING by the “address" column:
• Example
• SELECT * FROM  stud
ORDER BY  address  DESC;

12
• The following SQL statement selects all student from the “stud" table, sorted
ascending by the “address" and descending by the “Fname" column:
• Example: SELECT * FROM  stud ORDER BY  address  ASC, Fname  DESC;

SELECT * FROM  student ORDER BY  address  ASC, Fname  DESC;

13
The MySQL GROUP BY Statement

 The GROUP BY statement groups rows that have the same values into summary
rows, like "find the number of stud in each address".
 The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or
more columns.
• Syntax: SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
• The following SQL statement lists the number of stud in each address:
 SELECT COUNT(studId), address
FROM stud
GROUP BY address;

14
The MySQL HAVING Clause

• The HAVING clause was added to SQL because the WHERE keyword cannot be


used with aggregate functions.
• HAVING Syntax: SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
• The following SQL statement lists the number of student in each address. Only
include address with more than 5 student:
• Example: SELECT COUNT(studId), address
FROM stud
GROUP BY address
HAVING COUNT(studId) > 2;

15
• The following SQL statement lists the number of students in each address, sorted
high to low (Only include address with more than 3 student):
• Example: SELECT COUNT(studId), address
FROM stud
GROUP BY  address
HAVING COUNT(studId) > 3
ORDER BY COUNT(studId) DESC;

16
MySQL AUTO_INCREMENT Keyword
• MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By
default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each
new record.
• The following SQL statement defines the " EmpId " column to be an auto-increment primary
key field in the " Employee " table:
CREATE TABLE Employee(
    EmpId int NOT NULL AUTO_INCREMENT,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    PRIMARY KEY (EmpId )
);
• To let the AUTO_INCREMENT sequence start with another value, use the following SQL
statement:
• ALTER TABLE employee AUTO_INCREMENT=1;
When we insert a new record into the " Employee " table, we do NOT have to specify a value
for the " EmpId " column (a unique value will be added automatically):

17
MySQL Joining Tables
• A JOIN clause is used to combine rows from two or more tables, based on a
related column between them. Different types of join such as inner join, left join,
right join, outer join and cross join.
• MySQL INNER JOIN Keyword
• The INNER JOIN keyword selects records that have matching values in both tables.

INNER JOIN Syntax


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

18
• The following SQL statement selects all orders with customer information:
• create table customer(custId int not null primary key, fname varchar(20),lname
varchar(30),address varchar(50));
• insert into customer values(1,'demas','dawit','jimma');
• insert into customer values(2,'hana','hailu','tepi');
• insert into customer values(3,'abdelah','dawud','AA');
• insert into customer values(4,'aminat','ahimed','mizan');
• insert into customer values(5,'alemu','awoke','bahir dar');
• Create table orders(order_id int not null primary key, custId int, order_date date);
• insert into orders values(1,2,'2020/8/7');
• insert into orders values(2,1,'2019/2/13');
• insert into orders values(3,4,'2021/3/4');
• Example for inner join: SELECT Orders.order_id, customer.fname,
Orders.Order_date FROM Orders INNER JOIN customer ON
Orders.custId=customer.custId;
19
MySQL LEFT JOIN Keyword
• The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records (if any) from the right table (table2).

• LEFT JOIN Syntax: SELECT column_name(s)


FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
• The following SQL statement will select all customers, and any orders they might have:
• Example
• SELECT Customer.fname, Orders.order_id FROM Customer LEFT JOIN Orders ON Customer.custId
= Orders.custId ORDER BY Customer.fname;

20
MySQL RIGHT JOIN Keyword
• The RIGHT JOIN keyword returns all records from the right table (table2), and
the matching records (if any) from the left table (table1).
• RIGHT JOIN Syntax: SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
• The following SQL statement will return all customer, and any orders they might
have placed: :
• Example: SELECT Orders.order_id, customer.lname, customer.fnameFROM
Orders RIGHT JOIN customer ON Orders.custId = customer.custId ORDER BY
Orders.order_id;

21
SQL CROSS JOIN Keyword
• The CROSS JOIN keyword returns all records from both tables (table1 and
table2).

• CROSS JOIN Syntax: SELECT column_name(s)


FROM table1
CROSS JOIN table2;
• The following SQL statement selects all customers, and all orders:
• Example: SELECT Customer.fname, Orders.order_idFROM CustomerCROSS
JOIN Orders;

22
The MySQL UNION Operator
• The UNION operator is used to combine the result-set of two or
more SELECT statements.
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
• UNION Syntax: SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL Syntax: The UNION operator selects only distinct values by default.
To allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

23
• The following SQL statement returns the cities (only distinct values) from both
the "Customers" and the "Suppliers" table:
• Example: SELECT City FROM Customer
UNION SELECT City FROM supplier ORDER BY City;
• SQL UNION ALL Example: The following SQL statement returns the cities
(duplicate values also) from both the "Customers" and the "Suppliers" table:
SELECT City FROM Customer
UNION ALL
SELECT City FROM supplier
ORDER BY City;

24
MySQL CREATE VIEW Statement
• In SQL, a view is a virtual table based on the result-set of an SQL
statement.
• A view contains rows and columns, just like a real table. The fields in
a view are fields from one or more real tables in the database.
• You can add SQL statements and functions to a view and present the
data as if the data were coming from one single table.
• A view is created with the CREATE VIEW statement.
• CREATE VIEW Syntax: CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

25
• MySQL CREATE VIEW Examples
• The following SQL creates a view that shows all customers from Ethiopia:
• Example: CREATE VIEW Ethiopia
Customer AS
SELECT fname,city
FROM Customer WHERE country ='Ethiopia';
• We can query the view above as follows:
• Example: select*from EthiopiaCustomer;

26
MySQL Updating a View
• A view can be updated with the CREATE OR REPLACE VIEW statement.
• CREATE OR REPLACE VIEW Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
• The following SQL adds the "City" column to the " Ethiopia Customer " view:
Example
• CREATE OR REPLACE VIEW EthiopiaCustomer AS
SELECT fname, address, City
FROM Customer WHERE Country = 'Ethiopia';

27
MySQL Dropping a View
• A view is deleted with the DROP VIEW statement.
• DROP VIEW Syntax: DROP VIEW view_name;
• The following SQL drops the “Ethiopia Customer " view:
• Example
• DROP VIEW  EthiopiaCustomer;

28
MySQL CREATE INDEX Statement
• The CREATE INDEX statement is used to create indexes in tables.
• Indexes are used to retrieve data from the database more quickly than otherwise.
• The users cannot see the indexes, they are just used to speed up searches/queries.
• Note: Updating a table with indexes takes more time than updating a table without
(because the indexes also need an update). So, only create indexes on columns
that will be frequently searched against.
• CREATE INDEX Syntax: Creates an index on a table. Duplicate
values are allowed:
• CREATE INDEX index_name
ON table_name (column1, column2, ...);

29
• Example:CREATE INDEX idx_lname
ON stud(lname);
• If you want to create an index on a combination of columns, you can list the
column names within the parentheses, separated by commas:
• CREATE INDEX idx_pname
ON stud (lname, fname);

30
DROP INDEX Statement
• The DROP INDEX statement is used to delete an index in a table.
ALTER TABLE table_name
DROP INDEX index_name;

31
Thank you!

32

You might also like