Lab Lecture Note 2 (SQL Database & Table)
Lab Lecture Note 2 (SQL Database & Table)
In SQL, the 'Create Database' statement is a first step for storing the structured
data in the database. The database developers and the users use this statement in
SQL for creating the new database in the database systems. It creates the
database with the name which has been specified in the Create Database
statement. Syntax of Create Database statement in MySQL is:
In this syntax, Database_Name specifies the name of the database which we want
to create in the system. We have to type the database name in query just after the
'Create Database' keyword. Following are the most important points which are
required to learn while creating a database:
• The database we want to create should be a simple and unique name, which
can be easily identified.
• Database name should be no more than 128 characters.
Example 1: This example creates the Student database. To create the Student
database, you have to type the following command in Structured Query Language:
CREATE DATABASE Student;
When this query is executed successfully, then it will show the following output:
You can also verify that your database is created in SQL or not by using the
following query:
SHOW DATABASES;
SQL does not allow developers to create the database with the existing database
name. Suppose if you want to create another Student database in the same
database system, then the Create Database statement will show the following error
in the output:
So, firstly you have to delete the existing database by using the Drop Statement
before creating a same name database.
1|Page
You can also replace the existing database with the help of Replace keyword. If
you want to replace the existing Student database, then you have to type the
following SQL query:
When this query is executed successfully, then it will show the following output:
You can also check that your database is created in SQL by typing the following
query:
SHOW DATABASES;
2|Page
Syntax of Drop Database Statement in MySQL is:
In this SQL syntax, we have to specify the name of that database which we want
to delete permanently from the database system.
Example1: Suppose, we want to delete the Student database with all its data from
the database system. Firstly, we have to check that the Student database exists in
the system or not by using the following statement:
SHOW DATABASES;
If the Student database is shown in the output, then we have to type the following
query in SQL for removing the Student database:
If the Student database does not exist in the database system and we run the above
query in SQL, then the query will show the following output:
Here, the DatabaseName is the name of the database that you want to delete.
Example: Let us try to delete an existing database student in the database system
using the following SQL statement:
3|Page
When we execute the above SQL statement, the output is obtained as follows:
Now, let us try again to delete the database student that doesn't exist in the
database system using the following SQL statement:
When we execute the above SQL statement, the output is obtained as follows:
USE database_name;
Example 1: Suppose, you want to work with the Hospital database. For this firstly,
you have to check that if the Hospital database exists on the current database
server or not by using the following query:
SHOW DATABASES;
If the Hospital database is shown in the output, then you have to execute the
following query to select the Hospital database:
USE Hospital;
Example 2: Suppose, you want to work with another College database in SQL. For
this firstly, you have to check that the College database exists on the current
database server or not by using the following query:
4|Page
SHOW DATABASES;
If the College database is shown in the result, then you have to execute the
following query to select the College database:
USE College;
SQL Table
5|Page
Let's see the simple syntax to create the table.
The data type of the columns may vary from one database to another. For
example, NUMBER is supported in Oracle database for integer value whereas INT
is supported in MySQL.
You can verify it, if you have created the table successfully by using DESC
command as follows:
DESC STUDENTS;
The output will be:
FIELD TYPE NULL KEY DEFAULT EXTRA
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
6|Page
Now you have the STUDENTS table available in your database and you can use to
store required information related to students.
Example 02: Let's see the command to create an Employee table in MySQL
database.
You can verify it, if you have created the table successfully by using DESC
command as follows:
DESC STUDENTS;
The output will be:
7|Page
The basic syntax for creating a table with the other table is:
Example 01: The following SQL creates a copy of the employee table including
all columns.
You can verify it, if you have created the table successfully by using DESC
command as follows:
DESC EmployeeCopy;
Example 02: The following SQL creates a copy of the employee table including
some columns.
You can verify it, if you have created the table successfully by using DESC
command as follows:
DESC EmployeeCopy2;
8|Page
INSERT INTO table_name
(
column_name1, column_name2, .…, column_nameN)
VALUES
(value_1, value_2, ..…, value_N);
Make sure to use single quotation marks around the string values (e.g., 'Ishtiaq',
'Ahammad', '[email protected]', 'Airport', 'Dhaka').
9|Page
SQL DROP TABLE
• A SQL DROP TABLE statement is used to delete a table definition and all
data from a table.
• This is very important to know that once a table is deleted all the information
available in the table is lost forever, so we have to be very careful when
using this command.
Let's see the syntax to drop the table from the database.
Example 01: First, we verify STUDENTS table and then we would delete it from
the database.
DESC STUDENTS;
FIELD TYPE NULL KEY DEFAULT EXTRA
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
This shows that STUDENTS table is available in the database, so we can drop it
as follows:
Now, use the following command to check whether table exists or not.
DESC STUDENTS;
10 | P a g e
SQL DELETE TABLE
The DELETE statement is used to delete rows from a table. If you want to
remove a specific row from a table you should use WHERE condition. The
Syntax for this is:
Example 01: Let us consider, we want to Delete a single record from our employee
table. Then the query will be:
Example 03: Let us consider, we want to Delete all rows from our employee table.
Then the query will be:
The TRUNCATE statement is used to delete all the rows from the table and free
the containing space.
11 | P a g e
The Example Syntax for truncate a table is:
Truncate table is faster and uses lesser resources than DELETE TABLE
command.
When you use the drop statement it deletes the table's row together with the
table's definition so all the relationships of that table with other tables will no
longer be valid.
On the other hand, when we TRUNCATE a table, the table structure remains the
same, so you will not face any of the above problems. Drop table command delete
complete table along with table structure too. TRUNCATE TABLE doesn't
delete the structure of the table.
Example 1: Suppose, you want to change the name of the “employeecopy” using
ALTER TABLE statement. For this, you have to type the following query in SQL.
12 | P a g e
After this statement, the table "employeecopy" will be changed into the table
name "Bikes_Details".
Example 2: Suppose, you want to change the name of the “studentscopy” using
ALTER TABLE statement. For this, you have to type the following query in SQL.
After this statement, the table "studentscopy" will be changed into the table name
"Students_Details".
• If you want to copy the data (row value) of one SQL table into another SQL
table in the same MySQL server, then it is possible by using the INSERT
INTO statement in SQL.
• The INSERT INTO statement in SQL copies the content from one existing
table into the new table.
• But you have to remember that the tables should have the same name and
same number of attributes (i.e., columns).
Example 1: Suppose, you want to insert data into the “employeecopy” table from
“employee” table using INSERT INTO statement. For this, you have to type the
following query in SQL.
Example 2: Suppose, you want to insert data into the “studentscopy” table from
“students” table using INSERT INTO statement. For this, you have to type the
following query in SQL.
13 | P a g e
Copy Data from Another Table with WHERE Clause
Example 1: Suppose, you want to insert data only some row data (not all) into
the “employeecopy” table from “employee” table using INSERT INTO statement.
For this, you have to type the following query in SQL.
This query will insert only one row data into the “employeecopy” table from
“employee” table.
Example 2: Suppose, you want to insert data only some row data (not all) into
the “employeecopy” table from “employee” table using INSERT INTO statement.
For this, you have to type the following query in SQL.
This query will insert two rows of data into the “employeecopy” table from
“employee” table.
14 | P a g e
The above syntax only allows you to add a single column to the existing table. If
you want to add more than one column to the table in a single SQL statement,
then use the following syntax.
This statement will add Last_Name and City columns to the Employee table.
The MODIFY keyword is used for changing the column definition of the existing
table.
This syntax only allows you to modify a single column of the existing table. If you
want to modify more than one column of the table in a single SQL statement, then
use the following syntax.
15 | P a g e
Examples of ALTER TABLE MODIFY Column statement in SQL
This statement will Modify Last_Name and City columns to the Employee table.
In many situations, you may require to delete the columns from the existing table.
Instead of deleting the whole table or database you can use DROP keyword for
deleting the columns. To delete multiple columns, you have to write separate
command for each column.
Example 1: Suppose, you want to delete the Last_Name and City column from
the above Employeecopy2 table. For this, you have to type the following two
different queries in the SQL.
16 | P a g e
Examples of ALTER TABLE CHANGE Column Name statement in SQL
This statement will change the name of the First_Name column to Last_Name.
17 | P a g e