0% found this document useful (0 votes)
11 views49 pages

PDF Ayush

This lab report on MySQL exercises, prepared by Ayush Kumar Gupta, covers various SQL commands and concepts, including Data Definition Language (DDL) and Data Manipulation Language (DML). It details commands for creating, altering, and managing databases and tables, as well as constraints like primary and foreign keys. The document serves as a practical guide for students in the Database Management System course at Tribhuvan University.
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)
11 views49 pages

PDF Ayush

This lab report on MySQL exercises, prepared by Ayush Kumar Gupta, covers various SQL commands and concepts, including Data Definition Language (DDL) and Data Manipulation Language (DML). It details commands for creating, altering, and managing databases and tables, as well as constraints like primary and foreign keys. The document serves as a practical guide for students in the Database Management System course at Tribhuvan University.
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/ 49

LAB REPORT

ON

MYSQL EXERCISE

Prepared By:

Ayush Kumar Gupta

Roll no:79

Symbol no: 38218/23

BBA 2nd semester “C”

Patan Multiple Campus

Patan Dhoka, Lalitpur

Submitted To:

Mr. Binit Shrestha

Subject Faculty Member

In partial fulfillment of requirements for the course Database Management System


studied in BBA second semester Tribhuvan University.

September 2024
i

Table of Contents

CHAPTER 1: SQL BRIEF DISCRIPTION ..................................................................................... 1

1.1 Introduction To SQL .............................................................................................................. 1

1.2 Introductory Commands To SQL .......................................................................................... 1

1.2.1 Show Database ............................................................................................................... 1

1.2.2 Use Database .................................................................................................................. 2

1.2.3 Drop Database ................................................................................................................ 2

1.2.4 Show Tables ................................................................................................................... 3

1.2.5 Create A New Database .................................................................................................. 4

CHAPTER 2: DDL BASIC CONCEPT .......................................................................................... 5

CHAPTER 3: DDL COMMANDS.................................................................................................. 6

3.1 Create Table: .......................................................................................................................... 6

3.2 Drop Table ............................................................................................................................. 7

3.3 Alter Table ............................................................................................................................. 8

3.3.1 Adding A New Column .................................................................................................. 8

3.3.2 Dropping A Column ....................................................................................................... 9

3.3.3 Adding Multiple Columns in Existing Table ................................................................ 10

3.3.4 Adding A Column with Default Value .......................................................................... 11

3.3.5 Changing Datatype of An Existing Column ................................................................. 12

3.3.6 Renaming an Existing Table......................................................................................... 13

3.4 Truncate Table ..................................................................................................................... 15

3.5 Difference Between Drop Table and Truncate Table ........................................................... 16

CHAPTER 4: SQL CONSTRAINTS ............................................................................................ 17

4.1 Not Null ......................................................................................................................... 17

4.2 Unique ............................................................................................................................ 18

4.3 Default ........................................................................................................................... 18


ii

4.4 Primary Key ................................................................................................................... 19

4.5 Foreign Key ................................................................................................................... 20

CHAPTER 5: DML DEFINITION ................................................................................................ 23

CHAPTER 6: DML COMMANDS ............................................................................................... 24

6.1 Inserting A New Record in Table......................................................................................... 24

6.2 Inserting A Null Value in Table............................................................................................ 25

6.3 Inserting A Default Value in Table ....................................................................................... 26

6.4 Delete All Records of a Table .............................................................................................. 27

6.5 Delete A Particular Record Using Where Clause ................................................................ 27

6.6 Updating Single Column Value in A Record ....................................................................... 28

6.7 Updating Multiple Column Value in A Record.................................................................... 29

CHAPTER 7: SQL SELECT CLAUSE ......................................................................................... 31

7.1 Display All Records of a Table ............................................................................................ 31

7.2 Displaying Certain Records Based on Condition ................................................................ 31

7.3 Displaying Specific Columns from A Record ..................................................................... 32

CHAPTER 8: SQL CLAUSES ...................................................................................................... 34

8.1 Where Clause....................................................................................................................... 34

8.2 Like Clause .......................................................................................................................... 35

8.3 Describe Clause ................................................................................................................... 35

8.4 Order by Clause ................................................................................................................... 36

8.5 Group by Clause .................................................................................................................. 38

8.6 Distinct Clause..................................................................... Error! Bookmark not defined.

8.7 And Operator for Where Clause .......................................................................................... 40

8.8 Or Operator for Where Clause ............................................................................................. 41

CHAPTER 9: SQL VIEWS ........................................................................................................... 43

9.1 Difference Between View and Table ................................................................................... 43


iii

9.2 Creating View ...................................................................................................................... 43

9.3 Deleting A View/Drop View ................................................................................................ 44


1

CHAPTER 1: SQL BRIEF DISCRIPTION

1.1 Introduction To SQL

SQL stands for Structured Query Language. SQL is used to create, remove, alter the
databases and database objects in a database management system and to store, retrieve,
update the data in a database. SQL is a standard language for creating, accessing,
manipulating database management system. SQL works for all modern relational database
management system like, SQL Server, Oracle, MYSQL, etc.

1.2 Introductory Commands To SQL

1.2.1 Show Database

This command helps to show all the available databases in the MySQL server.

Syntax:

show databases;

Example:

In the above example, we have used the command ‘show databases’ to view all the
databases that are available in the MySQL server.
2

1.2.2 Use Database

This command helps to select a particular database in which various commands are
executed.

Syntax:

use database_name;

Example:

In the above figure, the database ‘Ayush’ is selected.

1.2.3 Drop Database

This command is used to delete a particular database from MySQL server.


3

Syntax:

drop database database name;

Example:

In the above example, the ‘test’ database is deleted from MySQL server.

1.2.4 Show Tables

This command is used to show the list of tables which are created inside the database.

Syntax:

Show tables;

Example:
4

In the above example, we have shown all the tables that are in database ‘Ayush’.

1.2.5 Create A New Database

This command is used to create a new database that is not already present in the server.

Syntax:

create database database name;

In the above example, we have created a new database ‘test’.


5

CHAPTER 2: DDL BASIC CONCEPT

DDL stands for Data Definition Language. It is a subset of SQL (Structured Query
Language) used to define and manage the structure of a database, such as creating, altering,
and dropping tables, indexes, and views. DDL statements are essential for defining the
blueprint of a database, ensuring data integrity, and managing the database structure
efficiently. They are typically used by database administrators and developers during the
initial setup of a database or when modifications to the schema are required to meet
changing business requirements.
6

CHAPTER 3: DDL COMMANDS

3.1 Create Table:

This command is used to create an empty table in database. The table thus created is
defined with its datatype, column name, length, and constraints, if they exist.

Syntax:

create table table_name(column1 name datatype(length) constraint,


column2 name datatype(length) constraint,…… column_n name
datatype(length) constraint);

Example:

In the above example we have created a table named authors with two columns.
7

3.2 Drop Table

This command is used to delete an existing table in a database. It completely removes the
table structure and associated index, statistics, permissions, triggers, and constraints.

Syntax:

drop table table_name;

Example:

In the above example, we have dropped the previously created table ‘authors’. We can see
that table ‘authors’ no longer exist.
8

3.3 Alter Table

This command is used to add, delete, or modify columns in an existing table. This is also
used to add and drop various constraints on an existing table.

3.3.1 Adding A New Column

We can add a new column using alter table command.

Syntax:

alter table table_name(new column_name datatype(length)


constraints);

Example:

In the above example, we have added a new column ‘nationality’ in the table ‘book’.
9

3.3.2 Dropping A Column

We can also delete an existing column along with its data from the table using alter table
command.

Syntax:

alter table table_name drop column_name;

Example:

In the above example, we have deleted an existing column ‘pages’.


10

3.3.3 Adding Multiple Columns in Existing Table

We can add multiple columns in the table using alter table command.

Syntax:

alter table table_name add(column1_name datatype(length)


constraints, column2_name datatype(length) constraints);

Example:

In the above example, we have added two columns ‘nationality’ and ‘involved _since’.
11

3.3.4 Adding A Column with Default Value

We can add a column which shows a default value using alter table.

Syntax:

alter table table_name add(column_name datatype(length) default


‘default_value’);

Example:

In the above example, we have added a column ‘address’ which has a default value
‘Balkumari’.
12

3.3.5 Changing Datatype of An Existing Column

We can change the datatype of an existing column by using the alter table command.

Syntax:

alter table table_name modify column_name datatype(length);

Example:

In the above example, we have changed the datatype of ‘involved_since’ from ‘date’ to
‘int(15)’.
13

3.3.6 Renaming an Existing Table

We can change the name of a table using the alter table command.

Syntax:

alter table table_name rename new table_name;

Or,

rename table_name to new table_name;

Example:
14

In the above example, we have changed the name of table ‘book’ to ‘publication’ and vice
versa.
15

3.4 Truncate Table

This command is used to delete all the date which are in the table without deleting the
structure of the table.

Syntax:

truncate table table_name;

Example:

In the above example, using truncate command has deleted the values inside of the table
‘author’ without deleting the structure of the table.
16

3.5 Difference Between Drop Table and Truncate Table

Drop Truncate

SQL command that destroys the tables’ SQL command that helps to remove
structure and the data stored in it. the records that are in the table.

Helps to remove the records of the Helps to remove only the values that
table, table structure and to remove the are filled in the table.
database from the system.

drop table table_name; truncate table table_name;


17

CHAPTER 4: SQL CONSTRAINTS

4.1 Not Null

Not null in SQL is a constraint that ensures a column doesn’t contain a null value. It ensures
that an attribute contain some value and not be empty. We can use this constraint while

a. Creating table

Syntax: create table table_name(column_name datatype(length) not


null);

b. Adding column

Syntax: alter table table_name add(column_name datatype(length) not


null);

c. Modifying column

Syntax: alter table table_name modify column_name datatype(length)


not null;

Example:

In the above example, we can see that when constraint not null is used the value insert
cannot be null.
18

4.2 Unique

Unique is a constraint that can be applied to a column in a database table. It ensures that
each value in the column is unique, meaning no two rows can have same values.

Syntax:

create table table_name(column1_name datatype(length) unique);

Example:

In the above figure, the attribute ‘address’ having unique constraint does not take any
duplicate value.

4.3 Default

Default is a constraint that can be applied to column in a database table. It specifies a default
value for the column, which is used when an attribute requires a particular value.

Syntax:
19

alter table table_name add(column1_name datatype(length) default


‘default_value’);

Example:

In the above example, the attribute ‘address’ is set to default with value ‘null’.

4.4 Primary Key

A primary key is a constraint that can be applied to a column or a group of columns in a


database table. It uniquely identifies each row in the table and ensures that no two rows
have same values for primary key column.
20

Syntax:

create table table_name(column1 name datatype(length) primary


key);

Example:

In the above example, we have set the attribute ‘p_id’ as primary key because of that the
attribute cannot be null and cannot repeat the values.

4.5 Foreign Key

Syntax:
21

alter table child table_name add foreign key(colunm_name of child


table) references parent table_name(primary_key attribute of
parent table);

Examples:

In the above example, we have made a specific attribute into a foreign key. Now the
attribute supplied_by of child table will act as a foreign key for the parent table .

In the above example, we are not able to drop the ‘supplied_by’ attribute because it acts as
a foreign key.
22

In the above example, we are bgine unable to add a row in the child table because the value
1234 does not exist the the primary key column of the parent table supplier. Hence, the
foreign key constraint fails.

In the above example we are not able to declare ‘supplied_by’ as a foreign key because the
attribute ‘name’ is not the primary key of the parent table.

In the above example, we can see the attribute ‘supplied_by’ act as a foreign key for the
weak table ‘items’ to connect with the parent table ‘supplier’.
23

CHAPTER 5: DML DEFINITION

Introduction Of DML

The SQL commands that deal with the manipulation of data present in the database belong
to DML or Data Manipulation Language and this includes most of the SQL statements. It
is the component of the SQL statement that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements. It plays with rows. It doesn’t
change or create or alter scheme at any cost. DML is a programming language used for
adding(inserting), deleting, and modifying(update) data in a database.

Types of DML

1. Procedural: the user specifies what data is needed and how to get it.

2. Nonprocedural: the user only specifies what data is needed


24

CHAPTER 6: DML COMMANDS

6.1 Inserting A New Record in Table

This command is used to insert data into a row of a table. The order in which the data is
entered must match the order of the column in the schema of the table. Single inverted
comma (‘’) is used to insert varchar type data.

Syntax:

insert into table_name values(value1, value2,…,value_n);

Example:

In the above example, we have inserted different value in the row of the table.
25

6.2 Inserting A Null Value in Table

This command is used to insert a null value into the field that has no data to input.

Syntax:

insert into table values(value1, null, value3,..,value_n);

Example:

In the above example, we have inserted a null value in a row of the table.
26

6.3 Inserting A Default Value in Table

This command is used to insert a default value in the field into the row of the table. The
column in which a default value is being inserted must be previously programmed with a
default value from DDL.

Syntax:

insert into table_name values(value1, value2, default, …,


value_n);

Example:

In the above figure, we have inserted a default value ‘kathmandu’ which was previously
declared using DDL.
27

6.4 Delete All Records of a Table

This record is used to delete various records from a given table. The records will be
completely deleted and the command cannot be undone.

Syntax:

delete from table_name;

Example:

In the above example, we have deleted the entire values from the table.

6.5 Delete A Particular Record Using Where Clause

This command is used to delete a particular row from the table using the where clause.
28

Syntax:

delete from table_name where column_name=column_value;

Example:

In the above example, we have deleted an entire row where the address was set to banepa.

6.6 Updating Single Column Value in A Record

This command is used to update the data that is present in an existing table. This command
works on specific column using the where clause.
29

Syntax:

update table table_name set column_name= new column_value where


primary key column=primary key value;

Example:

In the above example, we have changed the value of address column from Bhaktapur to
Kanpur.

6.7 Updating Multiple Column Value in A Record

This command is also used to update the records of existing table. However, with this
command we can change the value of more than one column at a time.

Syntax:
30

update table table_name set column1_name= new column1_value,


column2_name= new column2_value where primary key column=primary
key value;

Example:

In the above example, we have changed the value of two columns i.e. address and p_name.
31

CHAPTER 7: SQL SELECT CLAUSE

7.1 Display All Records of a Table

This command is used to view all the records that are in a table.

Syntax:

select * from table_name;

Example:

In the above example, we have displayed all the records of table author.

7.2 Displaying Certain Records Based on Condition

Syntax:

select * from table_name where ‘condition’;


32

Example:

In the above example, we have selected using a particular condition.

7.3 Displaying Specific Columns from A Record

The select command can also be used to retrieve a specific column’s records from a table.

Syntax:

select column1_name, column2_name from table_name;

Example:
33

In the above example, we have selected a single column as well as multiple columns to
view the record from.
34

CHAPTER 8: SQL CLAUSES

8.1 Where Clause

This clause shows that the record using the where condition. The condition may be different
accordingly with the situation.

Syntax:

Select * from table_name where ‘condition’;

Example:

In the above example we have used where clause to select all the values where age is less
than 25.
35

8.2 Like Clause

This clause shows that the particular record when a certain type of pattern matches. the
pattern may be forward or backward.

Syntax:

Select * from table name where column name like ‘%pattern’;

Select * from table name where column name like ‘pattern%’;

Example 1;

In the above example, we have use like pattern where the pattern is forward and backward.

8.3 Describe Clause

It simply describes the table schema with the help of their clause.
36

Syntax:

Desc table_name;

Example:

In the above example, we have described the schema of table that we have created.

8.4 Order by Clause

This column selects and show the records given by order either in ascending or descending
way.

Syntax:

Select * from table_name order by column_name asc;

Select * from table_name order by column_name desc;


37

Example 1:

In the above example we have shown the records according to the age in ascending order.

Example 2:
38

In the above example we shows the record according to descending order of the level.

8.5 Group by Clause

This clause is used to show the aggregate function of the column grouped by the attribute.

Syntax:

Select column_name, aggregate function from table_name group by


column_name.

Example :
39

In the above example, we have shown the aggregate function i.e avg age which us grouped
by level.

8.6 Distinct Clause

This clause selects and shown the single value for every repeating values.

Syntax:

Select distinct column_name from table_name;

Example:
40

In the above example, we have shown the single value of the column for every repeating
value of male and female.

8.7 AND Operator for Where Clause

This clause shows the multiple operation at a time using the AND clause.

Syntax:

Select * from table_name where column_name1=’column_value’ AND


column_name2=’column_value’;

Example:
41

In the above example, we have shown records that fulfills the condition of being female
and age less than 25.

8.8 OR Operator for Where Clause

This clause shows the multiple operation at a time using the OR clause. The records listed
in this must match either of at least one of the conditions mentioned in the OR operator.

Syntax:

Select * from table_name where column_name1=’column_value’ OR


column_name2=’column_value’;

Example:
42

In this above example, we have been shown records that fulfills the condition of being
female or age less than 25.
43

CHAPTER 9: SQL VIEWS

9.1 Difference Between View and Table

view table

View has minimum information. Table has overall information.

View is the subset of table. Table is the main set for view.

For creating view, table must exist. For creating table, view is not required.

9.2 Creating View

A view is the minimized version of table. View is the subset of table for which a table must
exist.

Syntax:

Create view view_name as select * from table_name where


column_name=’column_value’;

Example:
44

In the above example, we have created a view table named ram as selected from table
student.

9.3 Deleting A View/Drop View

This command is used to delete the created view table.

Syntax:

Drop view view_name;

Example:
45

In the above example, we have dropped the view table that we had created previously.

In the above example, it shows error because student is not a view. So we know that to
delete a view it must be the view not table.

You might also like