sql
sql
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
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
12 DESC test1 ;
13 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
14 | Field | Type | Null | Key | Default | Extra |
1
15 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
16 | x | int | YES | | NULL | |
17 | y | int | YES | | NULL | |
18 + -- - - - - -+ - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
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 + -- - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - -+ - - - - -+ - - - - - - - - -+ - - - - - - -+
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
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.