当前位置:网站首页>MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables

MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables

2022-06-13 03:23:00 koping_ wu

1、 Create and manage databases

1.1 Create database

Determine whether the database already exists , If it does not exist, create the database :

CREATE DATABASE IF NOT EXISTS  Database name ;

1.2 Using a database

View all current databases :

SHOW DATABASES; # There is one S, Representing multiple databases 

View the database currently in use :

SELECT DATABASE();  # One used  mysql  Global functions in 

View all tables under the specified library :

SHOW TABLES FROM  Database name ;

View database creation information :

SHOW CREATE DATABASE  Database name ;

Use / Switch database :

USE  Database name ;

1.3 modify the database

Change the database character set

ALTER DATABASE  Database name  CHARACTER SET  Character set ;  # such as :gbk、utf8 etc. 

1.4 Delete database

Delete the specified database :

DROP DATABASE IF EXISTS  Database name ;

2、 Create table

2.1 How it was created 1: Custom creation

Must have :

  • CREATE TABLE jurisdiction
  • Storage space
    The syntax is as follows :
CREATE TABLE [IF NOT EXISTS]  Table name (
 Field 1,  data type  [ constraint condition ] [ The default value is ],
 Field 2,  data type  [ constraint condition ] [ The default value is ],
 Field 3,  data type  [ constraint condition ] [ The default value is ],
......
[ Table constraints ]
);

2.2 How it was created 2: Create using an existing table

  • Use AS subquery Options , Combine creating tables with inserting data
  • The specified column should correspond to the column in the subquery one by one
  • Define columns by column names and default values

eg: Put the employee list employees Make a backup :

CREATE TABLE employees_backup AS SELECT * FROM employees;

2.3 Look at the data table structure

stay MySQL After creating the data table in , You can view the structure of the data table .MySQL Support use DESCRIBE/DESC Statement to view the data table structure , Also supports the use of SHOW CREATE TABLE Statement to view the data table structure .

SHOW CREATE TABLE  Table name \G

Use SHOW CREATE TABLE Statement can not only view the detailed statement when the table is created , You can also view the storage engine and character encoding .

3、 Modify table

3.1 Append a column

The syntax is as follows :

ALTER TABLE  Table name  ADDCOLUMN】  Field name   Field type  【FIRST|AFTER  Field name 】;

3.2 Modify a column

You can modify the data type of the column , length 、 Default values and locations , The syntax is as follows :

ALTER TABLE  Table name  MODIFYCOLUMN】  Field name 1  Field type  【DEFAULT  The default value is 】【FIRST|AFTER  Field name 
2;

3.3 Rename a column

ALTER TABLE  Table name  CHANGE 【column】  Name   New column names   New data types ;

3.4 Delete a column

ALTER TABLE  Table name  DROPCOLUMN】 Field name 

4、 rename table

Mode one : Use RENAME

RENAME TABLE emp
TO myemp;

Mode two :

ALTER table dept
RENAME [TO] detail_dept;  -- [TO] It can be omitted 

5、 Delete table

DROP TABLE Statement cannot be rolled back

DROP TABLE [IF EXISTS]  Data sheet 1 [,  Data sheet 2, ...,  Data sheet n];

6、 Clear the table

TRUNCATE TABLE sentence : Delete all data in the table + Free the storage space of the table .

TRUNCATE TABLE detail_dept;

TRUNCATE Statement cannot be rolled back , While using DELETE Statement delete data , You can roll back .

SET autocommit = FALSE;  # Close auto submit first 
 
DELETE FROM emp2;  # Use delete Clear the table 
 
SELECT * FROM emp2;  # The data should be empty at this time 
 
ROLLBACK;  # Roll back 
 
SELECT * FROM emp2;  # The data will be restored 

Ali Development Specification :
【 Reference resources 】TRUNCATE TABLE Than DELETE Fast , It also uses less system and transaction log resources , but TRUNCATE No transaction and no trigger TRIGGER, Possible accidents , It is not recommended to use this statement in development code .
explain :TRUNCATE TABLE In function and without WHERE Clause DELETE Same statement .

原网站

版权声明
本文为[koping_ wu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280530399804.html