DDL operates databases and tables
-
Operation database: CRUD
-
C: (Create) to Create
- Create database:
- Create database Specifies the database name.
- Create a database and check whether it does not exist.
- Create database if not exists Database name;
-
Create the database and specify the character set
- Create database Database name character set name;
-
Exercise: create db4 database, determine whether exists, and specify character set GBK
- create database if not exists db4 character set gbk;
- Create database:
- R (Retrieve) : query
- Query all database names:
- show databases;
- Query the character set of a database: Query the creation statement of a database
- Show create database Database name;
- Query all database names:
- U (Update) : change
- Modify the character set of the database
- Alter DATABASE alter database name character set name;
- Modify the character set of the database
- D (Delete) : Delete
- Deleting a Database
- Drop database Specifies the database name.
- Check whether the database exists and then delete it
- Drop database if EXISTS Indicates the database name.
- Deleting a Database
- Using a database
- Query the name of the database currently in use
- select database();
- Using a database
- Use Database name;
- Query the name of the database currently in use
-
-
The operating table
-
C: (Create) to Create
-
Syntax: create table Table name (column name 1 Data type 1, column name 2 Data type 2,…. Column name n Data type n);
- Note: The last column does not need a comma (,).
-
Database type:
- Int: indicates an integer
- age int,
- Double: indicates the decimal type
- Score double (5, 2)
- Date: indicates the date, including year, month and day, YYYY-MM-DD
- Datetime: indicates the date, including year month day hour minute second YYYY-MM-DD HH: MM :ss
-
Timestamp: Time error type Includes year month day minute second YYYY-MM-DD HH: MM :ss
- If this field is not assigned in the future, or is assigned to null, the current system time is used by default for automatic assignment
-
Varchar: string
- Name vARCHar (20): The name contains a maximum of 20 characters
- A zhangsan contains 8 characters. A zhangsan contains 2 characters
- Int: indicates an integer
-
Create table student(id int, name varchar(32), age int, score double(4,1), birthday date, insert_time timestamp);
- The replication table:
- Create table name like the name of the table to be copied;
-
- R (Retrieve) : query
- Query all table names in a database
- show tables;
- Query the table structure
- Desc table name;
- Query all table names in a database
- U (Update) : change
- Alter table rename to rename a table;
- Alter table table name character set character set name;
- Alter table table_name add column name datatype;
- Alter table table name change column name New column type new data type; Alter table table name modify column name New data type;
- Alter table table_name drop table_name;
- D (Delete) : Delete
- Drop table table name;
- Drop table if exists Specifies the name of the table.
-
DML: Add, delete, or modify data in a table
- Add data:
- Grammar:
- Insert into table name (1, 2,… N) values(1, 2… The value of n);
- Note:
- Column names and values should correspond one to one.
- Insert into values(values 1, 2… The value of n);
- Except for numeric types, all types need to be quoted (odd or even)
- Grammar:
- Delete data:
- Grammar:
- Delete from table name [where condition]
- Note:
- If no condition is added, all records in the table are deleted.
- If you want to delete all records
- Delete from table name; — Not recommended. Delete operations are performed as many times as there are records
- TRUNCATE TABLE name; Drop the table and create the same table.
- Grammar:
-
Modify data:
-
Grammar:
- Update table_name set table_name 1 = 1, table_name 2 = 2,… [] the where condition;
-
Note:
- If no conditions are added, all records in the table are modified.
-
DQL: queries records in a table
-
Select * from table_name;
-
Select list of columns from table names WHERE list of conditions group by having conditions after a group ORDER by sort LIMIT Paging limit
-
Based on the query
- Multiple fields of query 1, the select field name field name 2… From the name of the table;
- Note:
- If all fields are queried, you can use * instead of the list of fields.
- Note:
- To remove repetition:
- distinct
- Computed columns
- In general, you can use four operations to compute the values of some columns. (Generally only numerical calculations are performed)
- Ifnull (expression 1, expression 2) : the calculation that null participates in is null
- Expression 1: Which field needs to be checked for null
- If the field is the replacement value after null.
- Names:
- As: As can also be omitted
- Multiple fields of query 1, the select field name field name 2… From the name of the table;
-
Conditions of the query
- The WHERE clause is followed by a condition
-
The operator
- > 、< 、<= 、>= 、= 、<>
- BETWEEN… AND
- IN (set)
- LIKE: fuzzy query
- Placeholder:
- _: a single arbitrary character
- % : multiple arbitrary characters
- Placeholder:
- IS NULL
- And or &&
- The or or | |
-
Or not!
Query age > 20
SELECT * FROM student WHERE age > 20;
SELECT * FROM student WHERE age >= 20;
SELECT * FROM student WHERE age = 20;
SELECT * FROM student WHERE age = 20 SELECT * FROM student WHERE age = 20 = 20; SELECT * FROM student WHERE age <> 20;
The query age is greater than or equal to 20 and less than or equal to 30
SELECT * FROM student WHERE age >= 20 && age <=30; SELECT * FROM student WHERE age >= 20 AND age <=30; SELECT * FROM student WHERE age BETWEEN 20 AND 30;
Select * from user where age 22, age 18, SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25 SELECT * FROM student WHERE age = 22 OR age = 25;
SELECT * FROM student WHERE English = null; — No. Null values cannot be used with = (! =) judgment
SELECT * FROM student WHERE english IS NULL;
SELECT * FROM student WHERE English IS NOT null;
Select * from horse; Like SELECT * FROM student WHERE NAME like ‘%’; Select * from person where the second word is personified
SELECT * FROM student WHERE NAME LIKE “_化%”;
SELECT * FROM student WHERE NAME = ‘___’;
SELECT * FROM student WHERE NAME LIKE ‘% de %’;