Addition, deletion, modification and query of the library
Create database:create database 数据库名字
Specify characters and create a database:CREATE DATABASE oldgirl CHARACTER SET utf8;
Delete database:drop database 库名
Change the character set of the database:alter database oldgirl character set utf8mb4;
View the mysql library:show databases
2. Manage tables in the library
Addition, deletion, modification and query of tables
create table syntax
createtable<表名>(<字段名1><类型1>,
…
<字段名n><类型n>);
Field name description: The content can be numbers, letters, underscores, and numbers cannot be used at the beginning
Description of field type (also called option constraint)
Data type (Type)
Integer: number-integer
Tiny integer (tinyint): 1byte=8bit=2^8=256
Large integer (int): 4byte=2^32=(4 billion+)
Super large integer (bigint): 8byte=2^64 (4 billion*4 billion=160 billion)
Characters: all symbols are characters, including integers
Variable-length character type (varchar): Create a string with a specified space size but do not allocate initial space for it, only allocate an upper limit for it
Fixed-length character type (char): Create a specified space size and allocate initial space for it
Whether it is empty (Null): YES or NO
In addition, there are many constraints that will not be listed here.
drop table:drop table 表名
Change table name:rename table 原名字 to 新名字
Check out the tables in the library:show tables
View the table creation syntax:show create table stu1\G
Create and enter the library:create database oldboy;use oldboy;
build table
createtable stu1(
id int(10)notnull,
name varchar(20)notnull,
age tinyint(2)NOTNULLdefault'0',
dept varchar(16)defaultNULL);
View table:show tables;
View table structure: desc stu1;
View the statement to create the table: show create table stu1\G
Modify the table name:rename table
drop table:drop table <表名>
3. Manage fields (columns) in tables
add field
The last line is added:alter table stu1 add 字段名 varchar(类型大小) not null(是否为空) comment 'address'(注释);
Specified position increase:alter table stu1 add 字段名 varchar(类型大小) after 某个字段的名字;
Add in the first line:alter table stu1 add number varchar(11) first;
Before and after comparison
+-------+-------------+------+-----+---------+-------+| Field |Type|Null|Key|Default| Extra |+-------+-------------+------+-----+---------+-------+| id |int(10)|NO||NULL||| name |varchar(20)|NO||NULL||| age |tinyint(2)|NO||0||| dept |varchar(16)| YES ||NULL||+-------+-------------+------+-----+---------+-------+4rowsinset(0.01 sec)
+--------+--------------+------+-----+---------+-------+| Field |Type|Null|Key|Default| Extra |+--------+--------------+------+-----+---------+-------+| number |varchar(11)| YES ||NULL||| id |int(10)|NO||NULL||| name |varchar(20)|NO||NULL||| qq |varchar(25)| YES ||NULL||| age |tinyint(2)|NO||0||| dept |varchar(16)| YES ||NULL||| addr |varchar(256)|NO||NULL||+--------+--------------+------+-----+---------+-------+7rowsinset(0.00 sec)
Delete field:alter table 表名 drop 字段名字;
Change field:alter table 表名 change name sname varchar(128);
View the contents of the table:select * for 表名
4. Manage the data (rows) in the table
rebuild a table
createtable stu1(
id int(10)PRIMARYKEYnotnullAUTO_INCREMENT,
name varchar(20)notnull,
age tinyint(2)NOTNULLdefault'0',
dept varchar(16)defaultNULL);
MariaDB [oldboy]>desc stu1;+-------+-------------+------+-----+---------+----------------+| Field |Type|Null|Key|Default| Extra |+-------+-------------+------+-----+---------+----------------+| id |int(10)|NO| PRI |NULL|auto_increment|| name |varchar(20)|NO||NULL||| age |tinyint(2)|NO||0||| dept |varchar(16)| YES ||NULL||+-------+-------------+------+-----+---------+----------------+
insert data
Method 1 (specified column): insert into stu1(id,name,age,dept) values(1,'oldboy',35,'net sec');(Note: character columns should be enclosed in quotation marks, and numeric columns should not be enclosed in quotation marks)
Method 2 (omit columns):insert into stu1 values(2,'oldgirl',25,'linux');
Method 3: (insert multiple rows at the same time):insert into stu1 values(3,'littlegirl',5,'net sec'),(4,'littleboy',2,'Linux');
Delete data in the table
Order:delete from 表名 where 表达式
example:delete from stu1 where id=6;
modify the data in the table
grammar:update 表名 set 字段=新值 where 条件
example:update stu1 set name='zhangsan' where id=6;
query the data in the table
Basic grammar:select <字段1,字段2,...> from <表名> [WHERE 条件]
example:SELECT * FROM city WHERE countrycode='CHN';
5. Database data backup and recovery
Backup principle: Export data in the form of SQL statements