sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysqlclient-dev
mysql -h [主机名或ip] -u [用户] -p
mysql -u root -p
root@ubuntu:/home/lng# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 17Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)
mysql> create database csdn;Query OK, 1 row affected (0.01 sec)mysql> show databases;+--------------------+| Database |+--------------------+| csdn || information_schema || mysql || performance_schema || sys | +--------------------+5 rows in set (0.00 sec)mysql>
mysql> drop database csdn;Query OK, 0 rows affected (0.01 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)mysql>
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql>
介绍数据表的操作之前先介绍下mysql数据库的数据类型
数值型
类型 | 字节 | 说明 |
---|---|---|
TINYINT | 1 | 整数型 |
SAMLLINT | 2 | 整数型 |
INT | 4 | 整数型 |
BIGINT | 8 | 整数型 |
FLOAT | 4 | 浮点型 |
DOUBLE | 8 | 浮点型 |
字符串型
类型 | 字节 | 说明 |
---|---|---|
CHAR | 0 ~ 255 | 定长字符串 |
VARCHAR | 0 ~ 255 | 不定长字符串 |
时间日期型
类型 | 字节 | 范围 | 格式 | 说明 |
---|---|---|---|---|
DATA | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | 日期型 |
TIME | 3 | -838:59:59/838:59:59 | HH:MM:SS | 时间值或持续时间 |
YEAR | 1 | 1901/2155 | YYYY | 年份值 |
DATETIME | 8 | 1000-01-01 00:00:00/9999-12-21 23:59:59 | YYYY-MM-DDHH:MM:SS | 日期和时间值 |
约束是对插入数据库中的数据进行限定,这么做的目的是为了保证数据的有效性和完整性。
约束关键字 | 含义 |
---|---|
NOT NULL | 约束字段值不能为空 |
DEFAULT | 设置字段默认值 |
UNIQUE KEY | 设置字段的值是唯一的 |
PRIMARY KEY | 设置字段为表的主键 |
AUTO_INCREMENT | 设置字段值为自动增加 |
FOREIGN KEY | 设置字段为表的外键 |
create table 表名(字段名1 数据类型 约束条件,字段名2 数据类型 约束条件,...)
mysql> create database CSDN;Query OK, 1 row affected (0.01 sec)mysql> show databases;+--------------------+| Database |+--------------------+| CSDN || information_schema || mysql || performance_schema || sys |+--------------------+5 rows in set (0.00 sec)mysql> use CSDN;Database changedmysql> create table student(id INT PRIMARY KEY,name VARCHAR(30),age INT,score FLOAT);Query OK, 0 rows affected (0.02 sec)
describe/desc 表名
mysql> desc student;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || age | int | YES | | NULL | || score | float | YES | | NULL | |+-------+-------------+------+-----+---------+-------+4 rows in set (0.01 sec)mysql>
drop table 表名
alter table 旧表名 rename 新表名
alter table 表名 add 字段名 字段数据类型
mysql> alter table students add class INT;Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || age | int | YES | | NULL | || score | float | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+5 rows in set (0.01 sec)mysql>
alter table 表名 add 字段名 字段数据类型 first
mysql> alter table students add code INT first;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| code | int | YES | | NULL | || id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || age | int | YES | | NULL | || score | float | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+6 rows in set (0.00 sec)mysql>
alter table 表名 add 字段名 字段数据类型 after 字段名
mysql> alter table students add sex VARCHAR(10) after name;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| code | int | YES | | NULL | || id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || sex | varchar(10) | YES | | NULL | || age | int | YES | | NULL | || score | float | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+7 rows in set (0.01 sec)mysql>
alter table 表名 drop 字段名
alter table 表名 modify 字段名 字段新的数据类型
mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| code | int | YES | | NULL | || id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || sex | varchar(10) | YES | | NULL | || age | int | YES | | NULL | || score | float | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+7 rows in set (0.01 sec)mysql> alter table students modify score DOUBLE;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| code | int | YES | | NULL | || id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || sex | varchar(10) | YES | | NULL | || age | int | YES | | NULL | || score | double | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+7 rows in set (0.00 sec)mysql>
alter table 表名 change 旧字段名 新字段名 旧字段类型
mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| code | int | YES | | NULL | || id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || sex | varchar(10) | YES | | NULL | || age | int | YES | | NULL | || score | double | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+7 rows in set (0.00 sec)mysql> alter table students change code lcode INT;Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc students;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| lcode | int | YES | | NULL | || id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || sex | varchar(10) | YES | | NULL | || age | int | YES | | NULL | || score | double | YES | | NULL | || class | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+7 rows in set (0.00 sec)mysql>
alter table 表名 change 旧字段名 新字段名 新字段类型
alter table 表名 modify 字段名 字段类型 first
alter table 表名 modify 字段名1 字段类型 after 字段名2
insert into 表名(字段1, 字段2, 字段3, ...) values(值1, 值2, 值3, ...)
mysql> insert into students(id,name,age,score) values(1001,'TOM',22,90.5);Query OK, 1 row affected (0.00 sec)mysql> select * from students;+------+------+------+-------+| id | name | age | score |+------+------+------+-------+| 1001 | TOM | 22 | 90.5 |+------+------+------+-------+1 row in set (0.00 sec)mysql>
insert into 表名(字段1, 字段2, 字段3, ...) values(值11, 值21, 值31, ...),(值12, 值22, 值32, ...),...
mysql> insert into students(id,name,age,score) values(1002,'JACK',18,80.0),(1003,'LUCY',20,98.5);Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> select * from students;+------+------+------+-------+| id | name | age | score |+------+------+------+-------+| 1001 | TOM | 22 | 90.5 || 1002 | JACK | 18 | 80 || 1003 | LUCY | 20 | 98.5 |+------+------+------+-------+3 rows in set (0.00 sec)mysql>
updata 表名 set 字段1=值1, 字段2=值2, ..., where 条件
mysql> select * from students;+------+--------+------+-------+| id | name | age | score |+------+--------+------+-------+| 1001 | TOM | 22 | 90.5 || 1002 | JACK | 18 | 80 || 1003 | LUCY | 16 | 100 || 1004 | 曹操 | 18 | 80 |+------+--------+------+-------+4 rows in set (0.00 sec)mysql> update students set age=20,score=99.0 where id=1003;Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from students;+------+--------+------+-------+| id | name | age | score |+------+--------+------+-------+| 1001 | TOM | 22 | 90.5 || 1002 | JACK | 18 | 80 || 1003 | LUCY | 20 | 99 || 1004 | 曹操 | 18 | 80 |+------+--------+------+-------+4 rows in set (0.00 sec)mysql>
update 表名 set 字段1=值1, 字段2=值2, ...
mysql> select * from students;+------+--------+------+-------+| id | name | age | score |+------+--------+------+-------+| 1001 | TOM | 22 | 90.5 || 1002 | JACK | 18 | 80 || 1003 | LUCY | 20 | 99 || 1004 | 曹操 | 18 | 80 |+------+--------+------+-------+4 rows in set (0.00 sec)mysql> update students set age=20,score=80;Query OK, 4 rows affected (0.00 sec)Rows matched: 4 Changed: 4 Warnings: 0mysql> select * from students;+------+--------+------+-------+| id | name | age | score |+------+--------+------+-------+| 1001 | TOM | 20 | 80 || 1002 | JACK | 20 | 80 || 1003 | LUCY | 20 | 80 || 1004 | 曹操 | 20 | 80 |+------+--------+------+-------+4 rows in set (0.00 sec)mysql>
delete from 表名 where 条件
mysql> select * from students;+------+--------+------+-------+| id | name | age | score |+------+--------+------+-------+| 1001 | TOM | 20 | 80 || 1002 | JACK | 20 | 80 || 1003 | LUCY | 20 | 80 || 1004 | 曹操 | 20 | 80 |+------+--------+------+-------+4 rows in set (0.00 sec)mysql> delete from students where name='曹操';Query OK, 1 row affected (0.00 sec)mysql> select * from students;+------+------+------+-------+| id | name | age | score |+------+------+------+-------+| 1001 | TOM | 20 | 80 || 1002 | JACK | 20 | 80 || 1003 | LUCY | 20 | 80 |+------+------+------+-------+3 rows in set (0.00 sec)mysql>
delete from 表名
+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1001 | TOM | boy | 18 | 80 | 1 || 1002 | JACK | boy | 20 | 85.5 | 1 || 1003 | LUCY | girl | 20 | 10.5 | 2 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1005 | BOB | boy | 19 | 86.9 | 2 || 1006 | AMY | girl | 18 | 70.5 | 1 || 1007 | EMMY | girl | 19 | 99.5 | 2 || 1008 | DAWN | boy | 21 | 95.5 | 2 |+------+------+------+------+-------+-------+
select * from 表名
mysql> select * from students;+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1001 | TOM | boy | 18 | 80 | 1 || 1002 | JACK | boy | 20 | 85.5 | 1 || 1003 | LUCY | girl | 20 | 10.5 | 2 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1005 | BOB | boy | 19 | 86.9 | 2 || 1006 | AMY | girl | 18 | 70.5 | 1 || 1007 | EMMY | girl | 19 | 99.5 | 2 || 1008 | DAWN | boy | 21 | 95.5 | 2 |+------+------+------+------+-------+-------+8 rows in set (0.00 sec)mysql>
select 字段1,字段2,... from 表名
mysql> select id,name,class from students;+------+------+-------+| id | name | class |+------+------+-------+| 1001 | TOM | 1 || 1002 | JACK | 1 || 1003 | LUCY | 2 || 1004 | ANDY | 1 || 1005 | BOB | 2 || 1006 | AMY | 1 || 1007 | EMMY | 2 || 1008 | DAWN | 2 |+------+------+-------+8 rows in set (0.00 sec)mysql>
select distinct 字段1,字段2,... from 表名
mysql> select distinct class from students;+-------+| class |+-------+| 1 || 2 |+-------+2 rows in set (0.00 sec)mysql>
select 字段1 运算符 运算符操作数, 字段2 运算符 运算符操作数, ... from 表名
mysql> select id + 1, name from students;+--------+------+| id + 1 | name |+--------+------+| 1002 | TOM || 1003 | JACK || 1004 | LUCY || 1005 | ANDY || 1006 | BOB || 1007 | AMY || 1008 | EMMY || 1009 | DAWN |+--------+------+8 rows in set (0.00 sec)mysql>
select 字段1 as 字符串, 字段2 as 字符串, ... from 表名
mysql> select name as '姓名', sex as '性别', age as '年龄', score as '分数', class as '班级' from students;+--------+--------+--------+--------+--------+| 姓名 | 性别 | 年龄 | 分数 | 班级 |+--------+--------+--------+--------+--------+| TOM | boy | 18 | 80 | 1 || JACK | boy | 20 | 85.5 | 1 || LUCY | girl | 20 | 10.5 | 2 || ANDY | girl | 20 | 82.2 | 1 || BOB | boy | 19 | 86.9 | 2 || AMY | girl | 18 | 70.5 | 1 || EMMY | girl | 19 | 99.5 | 2 || DAWN | boy | 21 | 95.5 | 2 |+--------+--------+--------+--------+--------+8 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 条件
select 字段 from 表名 where 条件
mysql> select * from students where class=1;+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1001 | TOM | boy | 18 | 80 | 1 || 1002 | JACK | boy | 20 | 85.5 | 1 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1006 | AMY | girl | 18 | 70.5 | 1 |+------+------+------+------+-------+-------+4 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 条件1 and 条件2
mysql> select * from students where class=1 and sex='girl';+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1004 | ANDY | girl | 20 | 82.2 | 1 || 1006 | AMY | girl | 18 | 70.5 | 1 |+------+------+------+------+-------+-------+2 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 字段名 between 值1 and 值2
mysql> select * from students where score between 80 and 90;+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1001 | TOM | boy | 18 | 80 | 1 || 1002 | JACK | boy | 20 | 85.5 | 1 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1005 | BOB | boy | 19 | 86.9 | 2 |+------+------+------+------+-------+-------+4 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 字段名 not between 值1 and 值2
mysql> select * from students where score not between 80 and 90;+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1003 | LUCY | girl | 20 | 10.5 | 2 || 1006 | AMY | girl | 18 | 70.5 | 1 || 1007 | EMMY | girl | 19 | 99.5 | 2 || 1008 | DAWN | boy | 21 | 95.5 | 2 |+------+------+------+------+-------+-------+4 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 字段名 is null
mysql> select * from students where id is null;Empty set (0.00 sec)
select 字段 from 表名 where 字段名 is not null
mysql> select * from students where id is not null;+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1001 | TOM | boy | 18 | 80 | 1 || 1002 | JACK | boy | 20 | 85.5 | 1 || 1003 | LUCY | girl | 20 | 10.5 | 2 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1005 | BOB | boy | 19 | 86.9 | 2 || 1006 | AMY | girl | 18 | 70.5 | 1 || 1007 | EMMY | girl | 19 | 99.5 | 2 || 1008 | DAWN | boy | 21 | 95.5 | 2 |+------+------+------+------+-------+-------+8 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 字段名 in(值1, 值2, ...)
mysql> select * from students where name in('TOM','JACK');+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1001 | TOM | boy | 18 | 80 | 1 || 1002 | JACK | boy | 20 | 85.5 | 1 |+------+------+------+------+-------+-------+2 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 字段名 not in(值1, 值2, ...)
mysql> select * from students where name not in('TOM','JACK');+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1003 | LUCY | girl | 20 | 10.5 | 2 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1005 | BOB | boy | 19 | 86.9 | 2 || 1006 | AMY | girl | 18 | 70.5 | 1 || 1007 | EMMY | girl | 19 | 99.5 | 2 || 1008 | DAWN | boy | 21 | 95.5 | 2 |+------+------+------+------+-------+-------+6 rows in set (0.00 sec)mysql>
select 字段 from 表名 where 字段名 like 值
mysql> select * from students where name like 'A%';+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1004 | ANDY | girl | 20 | 82.2 | 1 || 1006 | AMY | girl | 18 | 70.5 | 1 |+------+------+------+------+-------+-------+2 rows in set (0.00 sec)mysql> select * from students where name like 'A__';+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1006 | AMY | girl | 18 | 70.5 | 1 |+------+------+------+------+-------+-------+1 row in set (0.00 sec)mysql>
select 字段名 from 表名 order by 字段名 asc/desc
mysql> select * from students order by score desc;+------+------+------+------+-------+-------+| id | name | sex | age | score | class |+------+------+------+------+-------+-------+| 1007 | EMMY | girl | 19 | 99.5 | 2 || 1008 | DAWN | boy | 21 | 95.5 | 2 || 1005 | BOB | boy | 19 | 86.9 | 2 || 1002 | JACK | boy | 20 | 85.5 | 1 || 1004 | ANDY | girl | 20 | 82.2 | 1 || 1001 | TOM | boy | 18 | 80 | 1 || 1006 | AMY | girl | 18 | 70.5 | 1 || 1003 | LUCY | girl | 20 | 10.5 | 2 |+------+------+------+------+-------+-------+8 rows in set (0.00 sec)mysql>
select 字段名 from 表名 group by 字段名
select 字段名 from 表名 group by 字段名 having 条件
mysql> select class from students group by class;+-------+| class |+-------+| 1 || 2 |+-------+2 rows in set (0.00 sec)mysql>
mysql> select class,count(class) from students group by class;+-------+--------------+| class | count(class) |+-------+--------------+| 1 | 4 || 2 | 4 |+-------+--------------+2 rows in set (0.00 sec)mysql>
mysql> select class,avg(score) from students group by class;+-------+------------+| class | avg(score) |+-------+------------+| 1 | 79.55 || 2 | 73.1 |+-------+------------+2 rows in set (0.00 sec)mysql>
mysql> select class,avg(score) from students group by class having class=1;+-------+------------+| class | avg(score) |+-------+------------+| 1 | 79.55 |+-------+------------+1 row in set (0.00 sec)mysql>