PDF Ayush
PDF Ayush
ON
MYSQL EXERCISE
Prepared By:
Roll no:79
Submitted To:
September 2024
i
Table of Contents
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.
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
This command helps to select a particular database in which various commands are
executed.
Syntax:
use database_name;
Example:
Syntax:
Example:
In the above example, the ‘test’ database is deleted from MySQL server.
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’.
This command is used to create a new database that is not already present in the server.
Syntax:
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
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:
Example:
In the above example we have created a table named authors with two columns.
7
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:
Example:
In the above example, we have dropped the previously created table ‘authors’. We can see
that table ‘authors’ no longer exist.
8
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.
Syntax:
Example:
In the above example, we have added a new column ‘nationality’ in the table ‘book’.
9
We can also delete an existing column along with its data from the table using alter table
command.
Syntax:
Example:
We can add multiple columns in the table using alter table command.
Syntax:
Example:
In the above example, we have added two columns ‘nationality’ and ‘involved _since’.
11
We can add a column which shows a default value using alter table.
Syntax:
Example:
In the above example, we have added a column ‘address’ which has a default value
‘Balkumari’.
12
We can change the datatype of an existing column by using the alter table command.
Syntax:
Example:
In the above example, we have changed the datatype of ‘involved_since’ from ‘date’ to
‘int(15)’.
13
We can change the name of a table using the alter table command.
Syntax:
Or,
Example:
14
In the above example, we have changed the name of table ‘book’ to ‘publication’ and vice
versa.
15
This command is used to delete all the date which are in the table without deleting the
structure of the table.
Syntax:
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
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.
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
b. Adding column
c. Modifying column
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:
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
Example:
In the above example, the attribute ‘address’ is set to default with value ‘null’.
Syntax:
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.
Syntax:
21
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
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.
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:
Example:
In the above example, we have inserted different value in the row of the table.
25
This command is used to insert a null value into the field that has no data to input.
Syntax:
Example:
In the above example, we have inserted a null value in a row of the table.
26
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:
Example:
In the above figure, we have inserted a default value ‘kathmandu’ which was previously
declared using DDL.
27
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:
Example:
In the above example, we have deleted the entire values from the table.
This command is used to delete a particular row from the table using the where clause.
28
Syntax:
Example:
In the above example, we have deleted an entire row where the address was set to banepa.
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:
Example:
In the above example, we have changed the value of address column from Bhaktapur to
Kanpur.
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
Example:
In the above example, we have changed the value of two columns i.e. address and p_name.
31
This command is used to view all the records that are in a table.
Syntax:
Example:
In the above example, we have displayed all the records of table author.
Syntax:
Example:
The select command can also be used to retrieve a specific column’s records from a table.
Syntax:
Example:
33
In the above example, we have selected a single column as well as multiple columns to
view the record from.
34
This clause shows that the record using the where condition. The condition may be different
accordingly with the situation.
Syntax:
Example:
In the above example we have used where clause to select all the values where age is less
than 25.
35
This clause shows that the particular record when a certain type of pattern matches. the
pattern may be forward or backward.
Syntax:
Example 1;
In the above example, we have use like pattern where the pattern is forward and backward.
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.
This column selects and show the records given by order either in ascending or descending
way.
Syntax:
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.
This clause is used to show the aggregate function of the column grouped by the attribute.
Syntax:
Example :
39
In the above example, we have shown the aggregate function i.e avg age which us grouped
by level.
This clause selects and shown the single value for every repeating values.
Syntax:
Example:
40
In the above example, we have shown the single value of the column for every repeating
value of male and female.
This clause shows the multiple operation at a time using the AND clause.
Syntax:
Example:
41
In the above example, we have shown records that fulfills the condition of being female
and age less than 25.
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:
Example:
42
In this above example, we have been shown records that fulfills the condition of being
female or age less than 25.
43
view table
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.
A view is the minimized version of table. View is the subset of table for which a table must
exist.
Syntax:
Example:
44
In the above example, we have created a view table named ram as selected from table
student.
Syntax:
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.