MYSQL database - basic operations of database & data table

Table of contents

Log in to MySQL

1. Create a database

View database

Modify database

Delete database               

2. Create a data table

View datasheet

Modify data table

Delete data table:


Preface: Review MySQL knowledge

The punctuation marks of all SQL statements are half-width symbols.

The contents in [ ] are optional parameters

Experimental environment: win10


Log in to MySQL

Start the MySQL server

Open the win10 command prompt (win+R) and enter cmd

Then enter mysql -uroot -p

Enter your password to log in to the MySQL server

1.Create database

Create database m1

①CREATE DATABASE `m1`;

②CREATE DATABASE IF NOT EXISTS `m1`;

(When using ①, if the database m1 already exists in the database, an error will be reported (error message:  Can't create database 'm1' ; database exists))

(The meaning of the statement in ② is: if the database m1 does not exist in MySQL, create the m1 database; if already exists in the database m1 in MySQL, ignore this statement , no more database will be created)

2. Create an m2 database using the utf8 character set and with proofreading rules.

CREATE DATABASE `m1` CHARACTER SET 'utf8' COLLATE 'utf8_bin';

View database

View all databases SHOW DATABASES;

View the specified database SHOW CREATE DATABASE database name;

Select (use) database USE database name;

View the currently used database SELECT DATABASE();

Modify database

Modify the encoding method of the database (this statement cannot modify the database name):

    ALTER DATABASE database name DEFAULT CHARACTER SET encoding method COLLATE encoding method_bin;

Example: Modify the encoding method of m1 to gbk

    ALTER DATABASE `m1` CHARACTER SET gbk;

Delete database               

    ①DROP DATABASE database name;

    ②DROP DATABASE IF EXISTS database name;

Example: Delete database m1

    DROP DATABASE IF EXISTS `m1`;

(The meaning of the statement in ② is: if the database m1 exists in MySQL, delete the database; if the database m1 does not exist in MySQL, ignore the statement and do not delete the database)

2. Create a data table

CREATE TABLE [IF NOT EXISTS] table name
(
field name field type integrity constraints,
Field name data type integrity constraints,
Field name data type integrity constraints
);

View datasheet

(DESC is the abbreviation of DESCRIBE)

View all tables in the database SHOW TABLES;

View the creation statement of the data table SHOW CREATE TABLE data table name;

View data table field information DESC data table name;

View the data table structure SHOW [FULL] COLUMNS FROM database name.data table name;

View data table data information SELECT*FROM data table name;

Modify data table

Modify the data table name:

    ①ALTER TABLE old table name RENAME [TO/AS] new table name;

    ②RENAME TABLE old table name 1 TO new table name 1 [, old table name 2 TO new table name 2];

Modify field name:

    ALTER TABLE table name CHANGE old field name new field name new field type [attribute];

Modify field data type:

    ALTER TABLE table name MODIFY field name data type;

Modify the field arrangement position:

    ①Adjust field 1 to the first column using FIRST

    ALTER TABLE table name MODIFY field name 1 data type FIRST;

    ②Adjust field 1 to a position other than the first column, use AFTER field name 2

    ALTER TABLE table name MODIFY field name 1 data type AFTER field name 2;

New fields:

    ①Add a field

     ALTER TABLE table name ADD field name data type [constraints];

    ②Add multiple fields at one time (for example, add 2 fields)

     ALTER TABLE table name ADD field name data type [constraints], ADD field name data type [constraints];

    ③Add a new field before or after a specified field

    ALTER TABLE table name ADD field name data type [constraints] [BEFORE/AFTER existing field name];

Delete fields:

    ALTER TABLE table name DROP field name;

Delete data table:

   ①DROP TABLE table name 1[, table name 2];

   ②DROP TABLE IF EXISTS table name 1[, table name 2];

Guess you like

Origin blog.csdn.net/m0_62239233/article/details/130199217