Fundamental Database Lab1
Fundamental Database Lab1
1
The MySQL CREATE and Drop DATABASE Statement
2
The MySQL CREATE and Drop Table Statement
4
:
The MySQL INSERT INTO Statement
5
The MySQL SELECT Statement
6
The MySQL WHERE Clause
7
The MySQL UPDATE Statement
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
9
MySQL ALTER TABLE Statement
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
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;
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
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.
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).
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).
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