Preface: The following is the most basic database of the most commonly used commands, especially for beginners to practice, I hope that through the practice of these commands to skillfully operate and consolidate the foundation, because only continuous practice to the knowledge can truly become their own things.
Quickly view the following:
Operation |
Command |
Create a database |
CREATE database name; |
Specify the database to manipulate |
Use database name; |
Create a data table |
CREATE table data table name; |
View Data Sheet |
SHOW CREATE table data table name; |
Viewing a data table using the describe statement |
DESCRIBE data table name; |
Renaming a data table |
ALTER table data table name RENAME new table name; |
Modify field names |
ALTER table data table name change old field name new data type for new field name; |
modifying field data types |
ALTER table Data table name MODIFY field name data type; |
Add Field |
ALTER table Data table name ADD field name data type; |
Delete a field |
ALTER table name DROP field name; |
Modify the arrangement position of a field |
ALTER table Data table name MODIFY field name 1 data type First | After field name 2 |
Delete a data table |
DROP table data table name; |
1. Create a database
Command: CREATE database name;
Example: CREATE DATABASE student;
2 . Specify the database to be manipulated
Command: Use database name;
Example: use student;
3, create the data table (if you have specified a database to operate, the command reference the previous bar)
Command: CREATE table data table name
(
Field name 1 data type [integrity constraint],
Field Name 2 data type [integrity constraint],
...
Field Name 2 data type [integrity constraint]
);
Example: CREATE TABLE Grade
(
ID INT (11),
Name VARCHAR (20),
Grade FLOAT
);
To verify that the data table was created successfully, you need to view it using the show TABLES statement: Show TABLES
The result is as follows (I'm using phpMyAdmin):
4. View Data Sheet
Command: SHOW CREATE table data table name;
Example: SHOW CREATE TABLE grade;
The results of the implementation are as follows:
5. Use the describe statement to view the data table
Command: DESCRIBE data table name, or DESC data table name;
Example: DESC grade;
The results of the implementation are as follows:
6. Renaming a data table
Command: ALTER table data table name RENAME [to] new name;
Note: The contents of [] appear in the command indicate the selectivity condition, not required;
Example: ALTER TABLE grade RENAME Newgrade;
7. Modify Field names
Command: ALTER table data table name change old field name new data type for new field name
Note: The new data type cannot be empty, even if you do not change the data type to write the original data type
Example: ALTER table grade change name newname varchar (20) (Changing the Name field in the Grade table to newname and the data type unchanged)
8. Modify field Data type
Command: ALTER table data table name MODIFY field name data type;
Example: ALTER TABLE grade MODIFY ID INT (20); (Change the data type of the field name ID to int (20))
9. Add fields
Command: ALTER table data table name ADD new field name data type [constraint] [first | After existing field name];
Note: "|" Indicates either that the arguments on either side are selectable, where first indicates that the field is set to a field in the table, and after means that the new field is inserted after the specified "existing field name"
Example: ALTER TABLE grade ADD Age INT (10);
10. Delete Fields
Command: ALTER table name DROP field name;
Example: ALTER TABLE grade DROP grade;
11. Modify the position of the field arrangement
Command: ALTER table name MODIFY field name 1 data type First | After field name 2
Example 1:alter table grade MODIFY newname VARCHAR first; (Inserts a field newname into the table at the beginning)
Example 2:alter TABLE grade MODIFY ID INT (one) after grade; (Insert field ID after field grade)
12. Delete Data Sheet
Command: DROP table data table name;
Example: DROP TABLE grade;
MySQL most basic command