Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
MySQL DATABASE
1. What is MySQL?
MySQL is currently the most popular database management system software used
for managing the relational database.
1.1. MySQL Create User
Syntax :-CREATE USER [IF NOT EXISTS] account_name IDENTIFIED BY 'password';
Examples:- create user ‘peter’@’localhost’ identified by 'jtp12345';
1.2. MySQL Show Users/List All Users
Syntax :-SELECT user FROM user;
Example:-show users;
1.3. MySQL Drop User
Syntax :-DROP USER 'account_name';
Example:- DROP USER 'peter';
1.4. Change MySQL User Password
Example:-
UPDATE user SET password = PASSWORD('jku12345') WHERE user = 'peter' AND
host = 'localhost'; OR
SET PASSWORD FOR 'peter'@'localhost' = jtp12345; OR
ALTER USER peter@localhost IDENTIFIED BY 'jtp123';
2. Syntax of Create Database statement in MySQL
CREATE DATABASE Database_Name;
Example :- CREATE DATABASE Student ;
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
2.1. Syntax To Show database
SHOW DATABASE ;
2.2. Syntax of Drop Database Statement in MySQL
DROP DATABASE Database_Name;
Example:- DROP DATABASE Student;
2.3. Syntax of Rename Database in MySQL
RENAME DATABASE old_database_name TO new_database_name;
Example:- RENAME DATABASE students TO college;
2.4. Syntax of USE statement in MySQL
USE database_name;
Example:- USE college;
3. MySQL TABLE
Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is
known as relation and row as tuple.
3.1. syntax of CREATE TABLE. In MySQL
create table "tablename"
("column1" "data type",
"column2" "data type",
...
"columnN" "data type");
Example:- CREATE TABLE students (
ID INT(11) NOT NULL,
name VARCHAR (20) NOT NULL,
age INT(11) NOT NULL,
marks CHAR (25),
PRIMARY KEY (ID));
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
3.2. MySQL Show/List Tables
Syntax :-SHOW TABLES;
Result:
Tables_in_college
Students
Teacher
3.3. MySQL ALTER Table
Syntax :-ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ];
original columns of table teacher
ID name gender age
Example: ALTER TABLE teacher ADD department
AFTER age;
Result:-
ID name gender age department
3.4. Syntax of RENAME statement in MySQL
RENAME old_table _name To new_table_name ;
Example:- RENAME STUDENTS To DEPARTMENT ;
3.5. MySQL SHOW COLUMNS Statement
Syntax :-SHOW COLUMNS FROM mytable_name FROM mydb_name;
OR,
SHOW COLUMNS FROM mydb_name.mytable_name;
Example:- SHOW COLUMNS FROM college.student;
Result:-
Field Type Null Key Default Extra
Id int(11) NO NULL
name varchar(22) YES NULL
age int(3) YES NULL
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
Field Type Null Key Default Extra
gender char(5) YES NULL
marks int(23) YES NULL
3.6. MySQL Rename Column
Syntax :-ALTER TABLE table_name
RENAME|CHANGE COLUMN old_column_name TO new_column_name;
Example:- ALTER TABLE student CHANGE COLUMN gender sex varchar(22);
Original columns of table students
Id name Age gender marks
Result:-
Id Name Age sex marks
3.7. ALTER TABLE ADD Column statement in
SQL
ALTER TABLE table_name
ADD (column_Name1 column-definition,
column_Name2 column-definition,
.....
column_NameN column-definition);
Example:- ALTER TABLE students ADD (mark int(12), phone int(10);
3.8. DROP TABLE Example in MySQL
DROP TABLE table_name;
Example:- DROP TABLE STUDENTS;
3.9. MySQL DESCRIBE TABLE
Syntax of DESCRIBE TABLE statement in MySQL
{DESCRIBE | DESC} table_name;
Example:- DESCRIBE students;
Result:-
Field Type Null Key Default Extra
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
Field Type Null Key Default Extra
ID int(11) NO PRI NULL
name varchar(20) YES NULL
gender varchar(10) YES NULL
age int(11) YES NULL
3.10. MySQL Rename Table
Syntax:-RENAME old_table TO new_table;
Example:- RENAME Department TO college;
4. MySQL INSERT Statement
Syntax :-INSERT INTO table_name ( field1, field2,...fieldN )
VALUES ( value1, value2,...valueN );
Example:- INSERT INTO students VALUES
(102, 'Joseph', 30, 'M', 87),
(103, 'Alex', 28, 'M', 28),
(104, 'Abebech', 23, 'F', 45);
Result:-
Id name age sex marks
102 Joseph 30 M 87
103 Alex 28 M 28
104 Abebech 23 F 45
5. MySQL UPDATE Query
Syntax :-UPDATE table_name
SET column_name1 = new-value1,
column_name2=new-value2, ...
[WHERE Clause]
Example:- UPDATE students
SET marks = 50
WHERE name = 'Alex';
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
Result:-
Id name age sex marks
102 Joseph 30 M 87
103 Alex 28 M 50
104 Abebech 23 F 45
6. MySQL DELETE Statement
Syntax :-DELETE FROM table_name WHERE condition;
Example:- DELETE FROM students WHERE id=102;
Result:-
Id name age sex marks
103 Alex 28 M 50
104 Abebech 23 F 45
7. MySQL REPLACE
Syntax :-REPLACE [INTO] table_name(column_list)
VALUES(value_list);
Example:- REPLACE INTO students (id, sex)
VALUES(4,'Fimale');
8. MySQL SELECT
8.1. Syntax of SELECT FROM statement in
MySQL
1, SELECT * FROM Table_name;
2, SELECT expressions FROM tables_name WHERE conditions;
Example:- SELECT * FROM students;
SELECT name, age FROM student WHERE age=20;
8.2. Syntax of SELECT UNIQUE statement in
MySQL
SELECT UNIQUE column_name
FROM table_name;
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
Example:- SELECT UNIQUE name FROM student;
Result:- only name columns elements are displayed in output.
8.3. Syntax of Select Count Function in MySQL
SELECT COUNT(column_name) FROM table_name; Example:
Example SELECT COUNT(ID) FROM student;
Resualt:- returns the number of records of the table in the output.
COUNT(Id)
4
8.4. Syntax of TOP Clause in MySQL
SELECT TOP number | percent column_Name1, column_Name2, ....., column_NameN
FROM table_name WHERE [Condition] ;
Example: SELECT TOP 4 Id, name, age, gender FROM student WHERE age>=18 ;
Result:- returns only four rows from total rows of table students.
8.5. MySQL SELECT IN
Syntax of IN Clause in MySQL
SELECT FROM table_name WHERE Expression IN (value 1, value 2 ... value n);
Example:- SELECT * FROM students WHERE students_name IN ( Alex , Yeshi, Eshetu)
8.6. SQL SELECT DATE
Syntax of SELECT DATE in MySQL
SELECT * FROM
table-name WHERE date-column (expression),
Example:-SELECT* FROM table-name where your date-column <= '2013-12-
13' and your date-column >= '2013-12-12';
Result: displays date between 2013-12-13 -2013-12-12
8.7. MySQL SELECT SUM
Syntax of SELECT SUM in MySQL
SELECT SUM (expression) FROM tables WHERE conditions;
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
Examle:-
SELECT marks, SUM (marks) AS "Total mark" FROM student GROUP BY marks;
Result: returns total marks of student on mark column orders.
8.8. MySQL SELECT NULL
Syntax of SELECT NULL in MySQL
SELECT SIR_NAME, NAME, MARKS FROM STUDENTS
WHERE MARKS IS NULL
9. MySQL WHERE Clause
Syntax :-WHERE conditions;
Example:- SELECT * FROM students WHERE sex = 'M';
Result:-
Id name age sex marks
103 Alex 28 M 50
10. MySQL ORDER BY Clause
Syntax:
SELECT expressions FROM tables
[WHERE conditions] ORDER BY expression [ ASC | DESC ];
Example:- SELECT * FROM students WHERE gender = 'M' ORDER BY Id;
11. MySQL GROUP BY Clause
Syntax:
SELECT expression1, expression2, ... expression_n, aggregate_function (expression
) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expr
ession_n;
Example:-
SELECT name, SUM(age) AS "Total age" FROM students GROUP BY emp_name;
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
12. Relationship
12.1. MySQL Primary Key
Syntax:
CREATE TABLE table_name(
col1 datatype PRIMARY KEY,
col2 datatype,
... );
Example:-
CREATE TABLE Login( login_id INT AUTO_INCREMENT PRIMARY KEY, usern
ame VARCHAR(40), password VARCHAR(55), email VARCHAR(55) );
12.2. MySQL Foreign Key
Syntax
[CONSTRAINT constraint_name] FOREIGN KEY [foreign_key_name] (col_name, ...) RE
FERENCES parent_tbl_name (col_name,...) ON DELETE referenceOption ON UPDATE
referenceOption
Example:- CREATE TABLE customer (
ID INT NOT NULL AUTO_INCREMENT,
Name varchar(50) NOT NULL,
City varchar(50) NOT NULL,
PRIMARY KEY (ID) );
CREATE TABLE contact ( ID INT, Customer_Id INT,
Customer_Info varchar(50) NOT NULL, Type varchar(50) NOT NULL,
INDEX par_ind (Customer_Id), CONSTRAINT fk_customer
FOREIGN KEY (Customer_Id)
REFERENCES customer(ID) ON DELETE CASCADE
ON UPDATE CASCADE
);
INSERT INTO customer(Name, City) VALUES
('Joseph', 'Jinka'),
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
('Mary', 'Konso'),
('John', 'Arbaminch');
INSERT INTO contact (Customer_Id, Customer_Info, Type) VALUES
(1, '
[email protected]', 'email'),
(1, '121-121-121', 'work' ),
(1, '123-123-123', 'home'),
(2, '
[email protected]', 'email'),
(2, '
[email protected]', 'email'),
(2, '212-212-212', 'work'),
(3, '
[email protected]', 'email'),
(3, '313-313-313', 'home');
DELETE FROM customer WHERE Name='John';
See result your self…….
13. MySQL JOINS
13.1. MySQL Inner JOIN (Simple Join)
Syntax: SELECT columns
FROM table1 INNER JOIN table2 ON table1.column = table2.column;
13.2. MySQL LEFT JOIN
Syntax
SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON Join_Condition;
13.3. MySQL RIGHT JOIN
Syntax
SELECT column_list
FROM Table1
RIGHT [OUTER] JOIN Table2
ON join_condition;
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393
Jinka branch new diplomatic college
DEPARTMENET OF information communication technology (ICT)
DATABASE LAB MANUAL practise project
14. MySQL Union
Syntax: SELECT column_list FROM table1
UNION
SELECT column_list FROM table2;
Example:- SELECT stud_name, subject FROM student1 UNION
SELECT stud_name, subject FROM student2;
15. INTERSECT
Syntax: SELECT (coloumn_names) from table1[WHERE condition] INTERSECT
SELECT (coloumn_names) from table2 [WHERE condition];
NOT INTERSECT(EXCEPT)
Syntax: SELECT (coloumn_names) from table1[WHERE condition] NOT INTERSECT
SELECT (coloumn_names) from table2 [WHERE condition];
16. MINUS
Syntax:
SELECT * FROM First_table
MINUS
SELECT * FROM Second_table;
Projection (𝝅):
Select Id, name, from student;
Prepared by instructor kayisle –k E.mail:- [email protected] +251986733393