MySQL Exercise Lab Report
MySQL Exercise Lab Report
LAB REPORT
ON
MY SQL EXERCISE
PREPARED BY:
Jay Prakash Shah
Symbol no.35111
(BBA, 2ND SEMESTER)
Section ‘c’
Patan Multiple campus
Patandhoka, Lalitpur
SUBMITTED TO:
Mr. Binit Shrestha
Subject Faculty Member
Patan Multiple Campus
Patandhoka, Lalitpur
Sept 2023
1
Table of Contents
CHAPTER 1: SQL BRIEF DESCRIPTION...........................................................................................................4
1.1. Introduction to SQL...........................................................................................................................4
1.2. Introductory Commands of SQL........................................................................................................4
a) Show database.............................................................................................................................4
b) Use database...............................................................................................................................5
c) Drop database.............................................................................................................................6
d) Show tables.................................................................................................................................7
e) Creating a new database.............................................................................................................7
CHAPTER 2: DDL BASIC CONCEPT................................................................................................................9
CHAPTER 3: DDL COMMAND.....................................................................................................................10
3.1. Create Table....................................................................................................................................10
3.2. Drop Table......................................................................................................................................11
3.3. Alter Table......................................................................................................................................11
3.3.1. Drop column............................................................................................................................12
3.3.2. Adding a column in existing table............................................................................................13
3.3.3. Adding multiple columns in existing table...............................................................................14
3.3.6. Rename table...........................................................................................................................15
3.3.4. Adding a column with default value........................................................................................16
3.3.5. Changing datatype of an existing column................................................................................17
3.4. Truncate Table................................................................................................................................18
3.5. Different Between Truncate and Drop Command..........................................................................19
CHAPTER 4: SQL CONSTRAINTS.................................................................................................................21
4.1. NOT NULL.......................................................................................................................................21
4.2. Unique............................................................................................................................................21
4.3. Default............................................................................................................................................22
4.4. Primary Key.....................................................................................................................................23
4.5. Foreign key.....................................................................................................................................24
4.6. Check Constraints...........................................................................................................................25
CHAPTER 5: DML DEFINITION....................................................................................................................26
CHAPTER 6: DML COMMAND....................................................................................................................27
6.1. Inserting Record..............................................................................................................................27
6.2. Inserting a NULL Value in a Table....................................................................................................27
2
6.3 Insert a Default Value in the Table..................................................................................................28
6.4 Delete all the record of table...........................................................................................................29
6.5. Delete a Particular Record Using Where Clause.............................................................................30
6.6. Updating Single Column Value in a Record.....................................................................................31
6.7. Updating Multiple Column Value in a Record.................................................................................32
CHAPTER 7: SQL CLAUSES..........................................................................................................................34
7.1. Display all Record of Table..............................................................................................................34
7.2. Displaying Certain Records Based on Condition.............................................................................34
7.3. Displaying Specific Columns from Record.......................................................................................35
CHAPTER 8: SQL SELECT CLAUSES..............................................................................................................37
8.1. Where clause..................................................................................................................................37
8.2. Like clause (pattern matching)........................................................................................................38
8.3. Describe clause...............................................................................................................................38
8.4. Order By clause...............................................................................................................................39
8.5. Group By clause..............................................................................................................................40
8.6. Distinct clause.................................................................................................................................41
8.7. AND operator.................................................................................................................................42
8.8. OR operator....................................................................................................................................43
CHAPTER 9: SQL VIEW...............................................................................................................................45
9.1. Different Between View and Table.................................................................................................45
9.2. Creating a View...............................................................................................................................45
9.3. Deleting a View...............................................................................................................................46
3
CHAPTER 1: SQL BRIEF DESCRIPTION
Syntax:
show databases;
4
Example:
b) Use database
This command is used to specify the name of database we want to select. After selecting the
database, we can perform various operations on it such as creating table, inserting data, updating
data and deleting data.
Syntax:
use database name;
Example:
5
In this figure, database is used from the databases.
c) Drop database
This command is used to deletes all user data and log files, as well as any backup and restore
history for the database.
Syntax:
drop database name;
Example:
6
In this figure, from the databases, moon is drop
d) Show tables
This command is used to show tables from the listed database.
Syntax:
show tables;
Example:
7
In this figure, list of table is shown.
Syntax:
create database name;
Example:
8
In this figure, database test is created.
9
CHAPTER 2: DDL BASIC CONCEPT
DDL stands for “Data Definition Language”. It is a subset of SQL (Structured Query Language)
used to define, manage, and manipulate the structure of a database. DDL command is used to
create, modify, and delete database object like table, index, views and store procedures.
Examples of DDL command includes CREATE, ALTER, and DROP, among others. This
command helps in setting up the database schema and defining the rules that govern the data
stored in the database.
DDL is standardized language with commands to define the storage groups, different structures
and objects in a database. It is also used in a generic sense to refer to any language that describe
data. It also includes statements to alter the objects and impose or drop certain constraints on
table, such as the followings;
UNIQUE
PRIMARY
FOREIGN KEY
CHECK
10
CHAPTER 3: DDL COMMAND
Syntax:
create table table_name (column_name1 and datatype (size of the
column) constraints, column_name2 datatype (size of the column)
constraints, column_namen datatype (size of the column) constraints);
Example:
Syntax:
drop table table_name;
11
Example:
In the above figure, drop table is used which makes the earth data delete from the databases.
Syntax:
alter table table_name drop columnname
Example:
12
In the above figure, alter table command is used which can modify or delete a specific column
Data of age is deleted from the previous table.
Syntax:
alter table table_name drop column column_name
Example:
13
In this figure, drop column command is used. Empid is dropped from the previous table using
drop column command as shown in above figure.
Syntax:
alter table table_name add(column_name data type(length) constraint);
14
Example:
In this given figure, a new column is used. There is alter table command is used which add a
phone column in the previous table and modify the table by adding phone column.
Syntax:
alter table table_name add(column_name1
data_type(length)constrain,column_name2 datatype(length)contrain.
15
Example:
In the above figure, multiple column is used. There is alter table command which is used to add
caste, address, and phone2 column by using a comma. You can also modify or delete the specific
column through alter table statement.
Syntax:
rename table table_name to new name table_name;
16
Example:
In the above the table, rename table is used. From the previous table, employee and student,
employee is renamed as worker as you can see in the above table.
Syntax:
alter table name add(column name datatype(length)default default
value);
17
Example:
18
Syntax:
alter table table name modify column name datatype(length);
Example:
In the above figure, employee’s name is modify from the previous table as shown in
Table.
Syntax:
truncate table table_name;
Example:
19
In the above figure, truncate table command is used which delete the data of students from the
previous table.
2. Drop:
20
The “drop” command is used to completely remove a table or database from the database
management system.
The drop command permanently deletes a table and all its data, freeing up the space
allocated for it.
Once a table is dropped, it cannot be recovered, and all associated objects like indexes,
triggers, and constraints are also removed.
In summary, Truncate removes all rows from a table while keeping the table structure intact,
whereas DROP completely deletes the table along with its data and any associated objects. Both
commands are used for different purposes depending on the need for data retention or complete
Removal.
21
CHAPTER 4: SQL CONSTRAINTS
Syntax:
create table table_name (column_name data_type not null);
Example:
4.2. Unique
Unique constraints is used to ensure that all values in a column are different.
22
Syntax:
create table table_name(column1 data_type not null primary key,column2
data_type not null unique);
Example:
4.3. Default
The default constraint is used to set a default value for a column.
Syntax:
23
create table table_name (column_name data_type default default_value);
Example:
Syntax:
create table table_name(column1 data_type,column2 data_type, column3
data_type, not null,……, primary key(column1,column2,column3));
Example:
24
In the above figure, primary key is set to std_id and roll_no.
4.5. Foreign key
The foreign key constraint is used to prevent actions that would destroy links between tables.
Syntax:
create table table_name(column1 data_type1,…..,constraint
fk_constraint_name references referenced_table_name(referenced_column)
on update cascade);
Example:
In the above figure, stu_id is foreign key from the previous table which was primary key.
25
4.6. Check Constraints
The check constraint is used to limit the value range that can be placed in a column.
Syntax:
create table table_name(column1 datatype,column2 datatype,
……,constraint constraint_name check(condition));
Example:
26
CHAPTER 5: DML DEFINITION
DML stand for “Data Manipulation Language”. It is a sub set of SQL (Structured Query
Language) used to interact with databases for inserting, updating, deleting, and retrieving data.
Examples of DML content of your database efficiently, keeping the data up to date and relevant.
A popular data manipulation language is Structured Query Language or SQL, used to retrieve
and manipulate data in a relational database. With SQL, you can perform operations on the
database and also use it to create a database. SQL uses specific commands like Create, Drop,
Insert, etc., to carry out the required tasks.
27
CHAPTER 6: DML COMMAND
Syntax:
insert into table_name value(value1,value2,value3,…,);
Example:
In the above figure, data of stu_id, stu_fname, stu_lname, gender, phone, stu_dob is inserted.
Syntax:
insert into table_name value(value1, value2, value3, ...);
28
Example:
In the above figure, null value is insert for the column rohit.
Syntax
insert into table_name value(value_1, value_2,default,default,.......)
29
Example:
Syntax:
delete from table_name;
30
Example:
Syntax:
delete from your_table_name where id = 123;
31
Example:
Syntax:
update table_name set column1=value1 where primary key
attribute=value;
32
Example:
In the above figure, updating the record of column gender f to m of stu_id 12 from the previous
table.
Syntax:
update table_name set column name 1=value1, column_name2=value2,
column_name3=value3 where primary key attribute=value;
33
Example:
In the above figure, multiple column record is update like Jayprakesh shah is update as rishab
from previous table similarly, Bhagat and gender ‘m’ is updated as Bhagat and ‘f’ from the
previous one.
34
CHAPTER 7: SQL CLAUSES
Syntax:
select * from table_name;
Example:
Syntax:
Select * from table_name where column=value;
35
Example:
Syntax:
select column1, column2,…..from table_name;
36
Example:
In the table, specific record is display, the record of stu_fname, stu_lname and phone is display
from the previous table student.
37
CHAPTER 8: SQL SELECT CLAUSES
Syntax:
Select column1,column2,……from table_name where condition;
Example:
In the table, where clauses is used it display the record of id, name, gender etc. from the table
student where age is greater than 19.
38
8.2. Like clause (pattern matching)
Like clause is used to search for a specified pattern in a column.
Syntax:
select * from table_name where column_name like ‘%a’;
Example:
In the table, like clause is used to search address from the previous table.
Syntax:
39
desc table_name;
Example:
Syntax:
select column1, colum2 from table_name order by column1 asc, column2
desc;
40
Example:
In this figure, column name is sort as ascending order and nationality is sort as descending order
from the previous table.
Syntax:
select column_name, avg (column) from table_name group by column_name;
41
Example:
Syntax:
select distinct column _name from table_name;
42
Example
In this table, distinct clause is used and unique value of gender column is displayed.
Syntax:
select * from table_name where condition1 and condition2;
43
Example:
In this table, AND operator is used which retrieve all student from “Nepali” Nationality whose
fee is greater than 21000.
8.8. OR operator
Syntax:
select * from table_name where column1 = ‘value1’ or column2 > value2;
44
Example:
In this table, OR operator retrieve rows where the category is either ‘electronic’ or price > 20000.
45
CHAPTER 9: SQL VIEW
2. View
A view is a virtual table that is derived from one or more base tables or other views.
It does not store data itself; instead, it presents data from the base tables in a specific way.
Views are defined by SQL queries and can contain joins, filters, aggregations, and other
transformations.
Views provide a way to present a subset of the data or to simplify complex queries.
They can enhance security by allowing users to access specific columns of a table
without exposing the entire table.
Views do not consume additional storage space since they are just saved queries that are
executed on-the-fly when queried.
Syntax:
create view view_name as (select statement);
46
Example:
Syntax:
drop view view_name;
47
Example:
48