0% found this document useful (0 votes)
7 views13 pages

MySQL Database Practical

The document provides an overview of MySQL database commands, including Data Definition Language (DDL) commands such as CREATE, DROP, RENAME, ALTER, and TRUNCATE, as well as Data Manipulation Language (DML) commands like INSERT, SELECT, UPDATE, and DELETE. It includes syntax examples for creating databases and tables, modifying table structures, inserting data, and querying data with various conditions. Additionally, it explains the use of wildcards in SELECT statements for pattern matching in text data.

Uploaded by

nurmohammad0560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

MySQL Database Practical

The document provides an overview of MySQL database commands, including Data Definition Language (DDL) commands such as CREATE, DROP, RENAME, ALTER, and TRUNCATE, as well as Data Manipulation Language (DML) commands like INSERT, SELECT, UPDATE, and DELETE. It includes syntax examples for creating databases and tables, modifying table structures, inserting data, and querying data with various conditions. Additionally, it explains the use of wildcards in SELECT statements for pattern matching in text data.

Uploaded by

nurmohammad0560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MySQL Database

DDL Commands
1. CREATE
2. DROP
3. RENAME
4. ALTER
5. TRUNCATE
To Create Database
Syntax
CREATE DATABASE name_of_database;
SQL Query
CREATE DATABASE college;
To Create Table: student
CREATE TABLE student(
roll_no int(5),
name varchar(20),
address varchar(50),
faculty varchar(20)
);
To rename table name from student to some other name
Syntax:
RENAME TABLE old_table_name TO new_table_name;
SQL Query
RENAME TABLE student TO student1;
To add column into student Table
ALTER TABLE student1 ADD COLUMN date_of_birth date;

To change name of the column we use following command


ALTER TABLE student1 CHANGE name first_name varchar(20);

To change data type and constraints of the column we use the following command
ALTER TABLE student1 MODIFY roll_no int UNIQUE;
To delete all the rows from a table we use TRUNCATE command,
Syntax
TRUNCATE table name_of_table
SQL Query
TRUNCATE TABLE student1;
To drop table from database
Syntax
DROP TABLE name_of_table;
DROP TABLE student1;
To drop database
Syntax:
DROP DATABASE name_of_database;
SQL Query
DROP DATABASE college;
DML Commands
1. INSERT
2. SELECT
3. UPDATE
4. DELETE
To create database
Syntax
CREATE DATABASE name_of_database;
Example
CREATE DATABASE p6;
After executing above SQL Query database p6 created
To create table in p6 database
Syntax
CREATE TABLE table_name (
column1_name data_type constraints,
column2_name data_type constraints,
...
);
Example
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50),
email VARCHAR(100) UNIQUE,
designation varchar(20),
salary DECIMAL(10, 2),
hire_date TIMESTAMP,
);
To insert data into employee table
Syntax
The basic syntax to insert data into a table in MySQL is:
INSERT INTO table_name (column1, column2... columnN)
VALUES (value1, value2... valueN);
Example
To insert single record
INSERT INTO employee(emp_id,first_name,last_name,email,designation,salary)
VALUES(101,'shankar','yadav','[email protected]','lecturer',45000);

To insert multiple Records


INSERT INTO employee(emp_id,first_name,last_name,email,designation,salary)
VALUES
(102,'Hari','Shrestha','[email protected]','Sales Manager',30000),
(103,'Ram','Thapa','[email protected]','software developer',70000),
(104,'Gita','Adhikari','[email protected]','lecturer',50000),
(105,'Sabin','shrestha','[email protected]','database designer',80000),
(106,'sunita','Shrestha','[email protected]','lecturer',48000),
(107,'samir','Khadka','[email protected]','admin',25000),
(108,'Binod','Thapa','[email protected]','manager',45600);
SELECT Operations
The SELECT statement in MySQL is used to retrieve data from one or more tables
in a database. It is the most commonly used SQL command.
The basic syntax for a SELECT query in MySQL is:
SELECT column1, column2, ..., columnN
FROM table_name
WHERE condition
ORDER BY column [ASC|DESC]
Select All Columns
SELECT * FROM employee;
Select Specific columns
SELECT first_name,last_name,salary FROM employee;

Select with a Where Clause (filter results)


SELECT *FROM employee WHERE emp_id=105;

Select with ORDER BY


SELECT *FROM employee WHERE salary>=50000 ORDER BY first_name DESC;
Select with WHERE and multiple condition
SELECT *FROM employee WHERE salary>=35000 AND designation='lecturer';

Limiting Results
SELECT * FROM employee LIMIT 5;

Select multiple records using IN Operator


SELECT *FROM employee WHERE emp_id IN(102,104,106);
UPDATE Query
An UPDATE query in MySQL is used to modify existing records in a table.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2...
WHERE condition;
Example
UPDATE employee
SET first_name='Ramesh', last_name='Adhikari'
WHERE emp_id=101;
UPDATE employee SET salary=salary*1.10 WHERE salary>=45000;

UPDATE employee SET salary=salary+3000


WHERE salary>=50000 AND designation='lecturer';
UPDATE employee SET salary=salary+1000;

Wild card
In MySQL, wildcards are special characters used with the LIKE operators in WHERE
clauses to search for patterns in text data.
The common wild card character in MySQL is percentage (%) and underscore (_)
Percentage (%):- matches zero or more character
Underscore (_):- Matches exactly one character
Syntax
SELECT * FROM table_name
WHERE column_name LIKE 'pattern';
SELECT *FROM employee WHERE first_name like 's%';

Note: - find names starting with s


SELECT *FROM employee WHERE first_name LIKE '%n';

Note: - find names ending with n

SELECT *FROM employee WHERE last_name LIKE '%hi%';

Note: - it searches the name containing hi anywhere in the last name

SELECT *FROM employee WHERE first_name LIKE '%ra%';


SELECT * FROM employee WHERE designation LIKE '%er%';

SELECT * FROM employee WHERE first_name LIKE 's___r';

SELECT * FROM employee WHERE first_name LIKE '_a%';

Return all customers that have "a" in the second position: SELECT * FROM
employee WHERE first_name LIKE '_a%';
SELECT * FROM employee WHERE designation LIKE 'L____';

Match any 5-letter word starting with 'L':

SELECT * FROM employee WHERE designation LIKE '_e%';

Match words where the second letter is 'e':

You might also like