Use of MySQL database (2) - adding, deleting, modifying and checking tables

The basic concept of database for beginners,
the use of MySQL database (1) - addition, deletion, modification and query of database

foreword

Today, I will mainly learn how to add, delete, modify and check the content of database tables. There are a lot of statements used. Let’s see how to use them together.


1. Using database tables

The use of any type of technology is inseparable from addition, deletion, modification and query, and 表(table)since this is the case, let's start with entering the database and how to use the SQL statement of addition, deletion, modification and query.

When you first enter the database, you can use show databases; to display the contents of the database

show database;
create database book;
use book;

example:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| book               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> use book;
Database changed

Input use book;, output shows that Database changed means that it has entered the book database.


If you enter show tables;to check what is in the table at this time, it will display Empty set, indicating that the current table is empty.

mysql> show tables;
Empty set (0.00 sec)

If you don't know which database you are in now, you can use this statement to view the current location;

mysql> select database();
+------------+
| database() |
+------------+
| book       |
+------------+
1 row in set (0.00 sec)

Then we can create some content in the table and use createthe statement.

2. Create a table (create)

2.1 Create a table

When creating a table, you need to set the columns and types of the table, just like creating a list in Excel needs to give him the field name, so that you know what information should be written in each column; and there are field types in the database, and there are many types of fields. More on that later.
insert image description here
Format:

create table 表名(字段名 类型(设置的长度));
mysql> create table book(id int(10),bname varchar(30));
Query OK, 0 rows affected, 1 warning (0.12 sec)

The front is its fixed format create table table name, the content in the brackets is the field name and data type, and the number in the brackets next to the data type is the length of the type.
Int is a numerical type, that is, numbers can be input, and varchar is a string type, which can fill in Chinese characters, English, characters and other information.

2.2 Add the field type of the table

If you need to continue adding the fields and types of the table, you can use this statement;

alter table 表名 add 字段 类型;

example:

mysql> alter table book add price int(20);
Query OK, 0 rows affected, 1 warning (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> desc book;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| bid   | int         | YES  |     | NULL    |       |
| bname | varchar(30) | YES  |     | NULL    |       |
| price | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

In fact, you can also choose to add it on the first line, or add it under a certain line, and use this statement;

#在表中的第一行添加字段和类型
alter table 表名 add 字段 类型 first;  
#添加字段到某一行的后面
alter table 表名 add 字段 类型 after 字段;

example:

mysql> alter table book add b_date date first;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table book add press varchar(30) after bname;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

3. View table (show)

Check which tables under the database can use this statement to view;

mysql> show tables;
+-----------------+
| Tables_in_books |
+-----------------+
| book            |
+-----------------+
1 row in set (0.00 sec)

3.1 View the structure of the table

Let's take a look at the structure of the table we just created;

mysql> desc book;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| b_date | date        | YES  |     | NULL    |       |
| bid    | int         | YES  |     | NULL    |       |
| bname  | varchar(30) | YES  |     | NULL    |       |
| press  | varchar(30) | YES  |     | NULL    |       |
| price  | int         | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Use desc table name; it will display the structure of the table,
Field represents the field, and the meaning of the column
type is the data type
. Null indicates whether the content can be null. The default is yes to support null values, and not null is not to support null values.
key is an index type (more content) – in the subsequent introduction,
default means that the unfilled data is null by default, and of course it can also be set to other display content.
Extra indicates that it is additional information, which will be described in detail later--(buried pit)

A method of viewing the table structure is written above, but there are actually three ways to view the table structure;

show columns from 数据库名,表名;
explain 表名;
show fields from 表名;

example:

mysql> explain book;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| b_date | date        | YES  |     | NULL    |       |
| bid    | int         | YES  |     | NULL    |       |
| bname  | varchar(30) | YES  |     | NULL    |       |
| press  | varchar(30) | YES  |     | NULL    |       |
| price  | int         | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> show fields from book;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| b_date | date        | YES  |     | NULL    |       |
| bid    | int         | YES  |     | NULL    |       |
| bname  | varchar(30) | YES  |     | NULL    |       |
| press  | varchar(30) | YES  |     | NULL    |       |
| price  | int         | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> show columns from books.book;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| b_date | date        | YES  |     | NULL    |       |
| bid    | int         | YES  |     | NULL    |       |
| bname  | varchar(30) | YES  |     | NULL    |       |
| press  | varchar(30) | YES  |     | NULL    |       |
| price  | int         | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

4. Modify the table (alter)

If you create a table name and find that the table name is wrong, don’t panic at this time, it can still be modified, let’s take a look at using this statement;

alter table 原表名 rename 新表名

example:

mysql> alter table book rename book_table;
Query OK, 0 rows affected (0.04 sec)

In fact, the field and type can also be modified, first look at only modifying the type of the field;

alter table 表名 modify 字段名 新类型

example:

mysql> alter table book_table modify id tinyint(4);
Query OK, 0 rows affected, 1 warning (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 1

Statements that modify fields and types together;

alter table 表名 change 原字段名 新字段名 新类型

example:

mysql> alter table book change id bid int(20);
Query OK, 0 rows affected, 1 warning (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> desc book;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| bid   | int         | YES  |     | NULL    |       |
| bname | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

5. Delete the table (drop)

No matter what you delete, you must think twice and confirm that it can be deleted before performing this operation.
The statement to delete the table;

drop table 表;

example:

mysql> drop table book;
Query OK, 0 rows affected (0.02 sec)

Summarize

The addition, deletion, modification and query of database tables are rich in content, and can be browsed repeatedly. If you think the content is good, you can bookmark it and like it!

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129557567