Operating database: CRUD
1. C(Create): Create
- Create a database:
* create database database name; - Create a database, determine that it does not exist, and then create:
* create database if not exists database name; - Create a database, and specify the character set
* create database database name character set character set name;
Exercise: Create a db4 database, determine whether it exists, and set the character set to gbk
* create database if not exists db4 character set gbk;
2. R(Retrieve): query
- Query the names of all databases:
* show databases; - Query the character set of a database: query the creation statement of a database
* show create database database name;
example:
Quietly Mimi spit out, this capitalization is enough!
3. U(Update): modify
- Modify the character set of the
database * alter database database name character set character set name;
**4. D(Delete):删除**
- Delete database
* drop database database name; - Determine that the database exists, and then delete it
if it exists* drop database if exists database name;
5. Use the database
-
Query the name of the database currently in use
* select database(); -
Use database
* use database name;
Operation table
1. C(Create): Create
- grammar:
create table 表名(
列名1 数据类型1,
列名2 数据类型2,
....
列名n 数据类型n
);
-
Note: There is no need to add a comma (,) in the last column
-
Database type:
1. int: integer type
* age int,
2. double: decimal type
* score double(5,2)
3. date: date, only contains year, month and day, yyyy-MM-dd
4. datetime: date, Contains year, month, day, hour, minute, and second yyyy-MM-dd HH:mm:ss
5. timestamp: Time error type includes year, month, day, hour, minute, and second yyyy-MM-ddHH:mm:ss
* If you don’t assign a value to this field in the future, or assign a value null, the current system time is used by default to automatically assign the value
6. varchar: string
* name varchar(20): name maximum 20 characters
* zhangsan 8 characters Zhang San 2 characters- Create table
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
- Copy table:
* create table table name like the name of the table being copied;
2. R(Retrieve): query
- Query all the table names in a database
* show tables;
* Query table structure
* desc table name;
3. U(Update): modify
-
Modify the table name
alter table table name rename to the new table name;
-
Modify the character set of the
table alter table table name character set character set name;
-
Add a column of
alter table table name add column name data type;
-
Modify column name type
alter table table name change column name new column name new data type;
alter table table name modify column name new data type;
-
Delete column
alter table table name drop column name;
**4. D(Delete):删除**
drop table 表名;
drop table if exists 表名 ;