MySQL string and operation command notes

1. MySQL login

1: Environment setup (path, character set, restart mysql)

2: Connect to the database
Format: mysql -h host address-u user name-p user password

2. String type and constraints

1: String type
Numerical data types (INTEGER, SMALLINT, DECIMAL, and NUMERIC), and approximate numeric data types (FLOAT, REAL, and DOUBLE PRECISION)

2: Constraints
Primary key primary key: the order of physical storage

Not null: this field is not allowed to fill in empty values

Unique: The value of this field cannot be repeated

Default default: When this value is not filled in, the default value will be used, if it is filled in, it shall prevail

Foreign key foreign key: Constraint on the relationship field. When filling in the relationship field, it will
check whether the value exists in the associated table . If it exists, it will fill in successfully. If it does not exist, it will fail to fill in and throw an exception.

Note: Although the foreign key constraint can guarantee the validity of the data, it will reduce the performance of the database when the data is crud (add, modify, delete, query), so it is not recommended to use, then the validity of the data requires us to The logic layer controls.

Three. MYSQL command library operation

Database operations
1. Create a database
Command: create database <database name>
For example: create a database named xhkdb
mysql> create database xhkdb;

2. Show all databases
Command: show databases (note: there is an s at the end)
mysql> show databases;

3. Delete the database
Command: drop database <database name>
For example: delete the database named xhkdb
mysql> drop database xhkdb;

4. Connect to the database
Command: use <database name>
For example: if the xhkdb database exists, try to access it:
mysql> use xhkdb;
screen prompt: Database changed

5. View the currently used database
mysql> select database();

6. Table information contained in the current database:
mysql> show tables; (Note: there is an s at the end)

Four. Table operation "should connect to a database before operation"

1. Create table
command: create table <table name> (<field name> <type> [,...<field name n> <type n>]);

2. Get the table structure
Command: desc table name, or show columns from table name

3. Delete table
command: drop table <table name>

4. Insert data
Command: insert into <table name> [( <field name>[,…<field name n> ])] values ​​(value)[, (value n )]

5. Query the data in the table

1), query all rows
Command: select <field, field,...> from <table name> where <expression>
For example: view all data in the table MyClass
mysql> select * from MyClass;

2), query the first few rows of data
For example: view the previous data in the table MyClass
mysql> select * from MyClass order by id limit 0,2;
or:
mysql> select * from MyClass limit 0,2;

6. Delete data in the table
Command: delete from table name where expression

7. Modify the data in the table: update table name set field = new value, ... where conditions

8. Add fields to the
table : Command: alter table table name add field type other

9. Change the table name:
Command: rename table original table name to new table name;

10. Update field content
Command: update table name set field name = new content

Guess you like

Origin blog.csdn.net/weixin_48660253/article/details/109403650