MySQL Database Creation and Table Management
Overview
This guide walks through the process of creating and managing databases and tables in MySQL. It
includes creating databases, creating tables, inserting data, and committing transactions.
Database Creation
To start, we need to create a new database and use it for our tables.
1 SHOW databases ;
2 + -- - - - - - - - - - - - - - - - - - -+
3 | Database |
4 + -- - - - - - - - - - - - - - - - - - -+
5 | imsdb |
6 | in fo rm ation_schema |
7 | mysql |
8 | pe rf or mance_schema |
9 | psa |
10 | regform |
11 | sys |
12 + -- - - - - - - - - - - - - - - - - - -+
13
14 CREATE database mydb ;
15
16 USE mydb ;
17
18 SHOW tables ;
19 Empty set (0.01 sec )
The above commands show the existing databases, create a new database named mydb, and switch
to using mydb.
Table Creation
Creating Simple Tables
Let’s create a couple of simple tables named test and test1.
1 CREATE TABLE test ( x int ) ;
2
3 DESC test ;
4 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
5 | Field | Type | Null | Key | Default | Extra |
6 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
7 | x | int | YES | | NULL | |
8 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
9
10 CREATE TABLE test1 ( x int , y int ) ;
11
12 DESC test1 ;
13 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
14 | Field | Type | Null | Key | Default | Extra |
1
15 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
16 | x | int | YES | | NULL | |
17 | y | int | YES | | NULL | |
18 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
Creating the Customer Table
Next, we create a more detailed table named customer with various columns to store customer informa-
tion.
1 CREATE TABLE customer (
2 customer_id INT ,
3 customer_name VARCHAR (20) ,
4 customer_address VARCHAR (100) ,
5 customer_contact INT ,
6 customer_mailid VARCHAR (20) ,
7 customer_gender VARCHAR (20)
8 );
9
10 DESC customer ;
11 + -- - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
12 | Field | Type | Null | Key | Default | Extra |
13 + -- - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
14 | customer_id | int | YES | | NULL | |
15 | customer_name | varchar (20) | YES | | NULL | |
16 | customer_address | varchar (100) | YES | | NULL | |
17 | customer_contact | int | YES | | NULL | |
18 | customer_mailid | varchar (20) | YES | | NULL | |
19 | customer_gender | varchar (20) | YES | | NULL | |
20 + -- - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
Inserting Data into Customer Table
We can insert a new record into the customer table with the following SQL statement.
1 INSERT INTO customer
2 VALUES (
3 101 ,
4 ’ scott ’ ,
5 ’2b , park avenue , chennai ’ ,
6 1234567890 ,
7 ’ scott@gmail . com ’ ,
8 ’m ’
9 );
10
11 SELECT * FROM customer ;
12 + --
- - - - - - - - - - -+ - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - -
13 | customer_id | customer_name | customer_address | customer_contact |
customer_mailid | customer_gender |
14 + --
- - - - - - - - - - -+ - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - -
15 | 101 | scott | 2b , park avenue , chennai | 1234567890 |
scott@gmail . com | m |
16 + --
- - - - - - - - - - -+ - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - -
17
18 COMMIT ;
2
Creating and Populating the dept Table
1 CREATE TABLE dept (
2 deptno DECIMAL (2) ,
3 dname VARCHAR (14) ,
4 loc VARCHAR (13)
5 );
6
7 DESC dept ;
8 + -- - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
9 | Field | Type | Null | Key | Default | Extra |
10 + -- - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
11 | deptno | decimal (2 ,0) | YES | | NULL | |
12 | dname | varchar (14) | YES | | NULL | |
13 | loc | varchar (13) | YES | | NULL | |
14 + -- - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
15
16 INSERT INTO dept ( deptno , dname , loc ) VALUES
17 (10 , ’ ACCOUNTING ’ , ’ NEW YORK ’) ,
18 (20 , ’ RESEARCH ’ , ’ DALLAS ’) ,
19 (30 , ’ SALES ’ , ’ CHICAGO ’) ,
20 (40 , ’ OPERATIONS ’ , ’ BOSTON ’) ;
Creating and Populating the emp Table
Finally, we create the emp table and insert records into it.
1 CREATE TABLE emp (
2 empno DECIMAL (4) ,
3 ename VARCHAR (10) ,
4 job VARCHAR (9) ,
5 mgr DECIMAL (4) ,
6 hiredate DATE ,
7 sal DECIMAL (7 ,2) ,
8 comm DECIMAL (7 ,2) ,
9 deptno DECIMAL (2 ,0)
10 );
11
12 DESC emp ;
13 + -- - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
14 | Field | Type | Null | Key | Default | Extra |
15 + -- - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
16 | empno | decimal (4 ,0) | YES | | NULL | |
17 | ename | varchar (10) | YES | | NULL | |
18 | job | varchar (9) | YES | | NULL | |
19 | mgr | decimal (4 ,0) | YES | | NULL | |
20 | hiredate | date | YES | | NULL | |
21 | sal | decimal (7 ,2) | YES | | NULL | |
22 | comm | decimal (7 ,2) | YES | | NULL | |
23 | deptno | decimal (2 ,0) | YES | | NULL | |
24 + -- - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
By following these steps, you can create and manage databases and tables, insert data, and commit
transactions in MySQL.