MySQL study Note 3: basic table operations _ MySQL
Source: Internet
Author: User
MySQL study Note 3: basic table operations introduction bitsCN.com to operate a table, you must first select the database because the table exists in the database
Select database
Mysql> use school;
Database changed
After selecting the database, we can create tables 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 used to create a table, followed by the table name
The field names and types are written in brackets, separated by commas (,). Note that varchar is a variable-length string.
The five lines can also be written as one line. The reason for this writing is to define the definition.
Display Table
Mysql> show tables;
+ ------------------ +
| Tables_in_school |
+ ------------------ +
| Student |
+ ------------------ +
Row in set (0.00 sec)
Show tables: displays all tables in the current database.
View basic table structure
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)
The field, data type, whether it is null, primary foreign key, default value, and additional information are displayed.
Describe can also be abbreviated as desc
In fact, most SQL statements can be abbreviated to four characters.
Note that the newly written sex is of the boolean type and will be automatically converted to the tinyint type.
View detailed table structure
Show create table displays the detailed information when creating a table.
The/G behind the end is to show more beautiful
Tips: especially valid at the end of/G when long information is displayed.
Delete table
Mysql> drop table student;
Query OK, 0 rows affected (0.02 sec)
The operation for deleting a table is similar to that for deleting a database.
Use the drop command. after deletion, you can use show tables to view the remaining table bitsCN.com.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service