To manipulate the table, you first need to select the database because the table exists in the database
Select Database
Mysql> Use school;
Database changed
After selecting the database, we can create the table in this database.
Create a table
Mysql> CREATE TABLE Student (
-> ID int,
-> name varchar (20),
-> Sex Boolean
->);
Query OK, 0 rows affected (0.11 sec)
Create table is for creating tables, followed by table names
The field name and type are written in parentheses, separated by commas, which indicate that varchar is a variable-length string
The 5 lines here can also be written in one line, so it's for clarity.
Show Table
Copy Code code as follows:
Mysql> Show tables;
+------------------+
| Tables_in_school |
+------------------+
| Student |
+------------------+
Row in Set (0.00 sec)
Show tables to display all tables in the current database
View Table Basic structure
Copy Code code as follows:
Mysql> describe student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | Int (11) | YES | | NULL | |
| name | varchar (20) | YES | | NULL | |
| sex | tinyint (1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
Rows in Set (0.00 sec)
field, data type, NULL, primary foreign key, default value, and extra information are shown here
Describe can also be abbreviated as DESC
In fact, most SQL statements can be abbreviated to four-character form
Note that the sex you just wrote is a Boolean type and will be automatically converted to the tinyint type
View Table Detail structure
Copy Code code as follows:
Mysql> Show CREATE TABLE Student\g
1. Row ***************************
Table:student
Create table:create Table ' student ' (
' id ' int (one) DEFAULT NULL,
' Name ' varchar DEFAULT NULL
) Engine=innodb DEFAULT charset=latin1
Row in Set (0.00 sec)
Show CREATE table shows the details when creating a table
After the end of the \g is to show more beautiful
Tips: \g endings are especially effective when displaying longer messages
Delete Table
mysql> drop table student;
Query OK, 0 rows affected (0.02 sec)
Deleting a table and deleting a database is similar to the operation
Using the drop command, you can use show tables to view the remaining tables when the deletion is complete