1、安装mysql
apt search mysql-server
...
#查询结果
mysql-server-8.0/focal-security,focal-updates 8.0.41-0ubuntu0.20.04.1 amd64
MySQL database server binaries and system database setup
...
apt install mysql-server-8.0
...
2、连接数据库
mysql -u 用户名 -h mysql服务器地址 -P 端口 -p
#之后会提示输入密码
#登录成功后提示符会变成 mysql>
表的基本操作
功能 | 格式 |
---|---|
显示数据库 | show databases; |
显示数据库中的表 | show tables from 数据库; |
显示表结构 | describe 表名; |
显示创建表的sql | show create table 表名; |
重命名表 | alter table 旧表名 rename to 新表名; |
批量重命名表 | rename table 旧表名1 to 新表名1,旧表名2 to 新表名2 |
增加列 | alter table 表名 add column 列名 类型 [属性] |
删除列 | alter table 表名 drop column 列名 |
修改列信息 | alter table 表名 MODIFY 列名 新类型 [新属性] |
列的属性
delimiter $
create table t1
(
id int auto_increment,
student_no int not null,
name varchar(10) not null,
age int default(0),
primary key(id),
unique(student_no)
);
delimiter $
create table t2
(
id int auto_increment,
student_no int ,
score int default(0),
primary key(id),
foreign key(student_no) references t1(student_no)
);
$
delimiter ;
关键字 | 描述 |
---|---|
primary key | 主键:声明为主键的字段(或字段组合)不能出现重复,且不能为空 |
unique | 唯一键:声明为唯一键的字段(或字段组合)不能出现重复 |
not null | 非空属性 |
default | 默认值:插入数据时如果没有指定该字段,则设置为默认值 |
auto_increment | 自增:插入数据时不需要指定该字段 |
简单查询
#全表查询
select * from 表名
#查询部分字段
select 字段1,字段2 ... from 表名
带搜索条件的查询
select ... from 表名 where 条件表达式
#查找t1表中学号是202501的同学信息
mysql> select * from t1 where student_no = 202501;
+----+------------+------+------+
| id | student_no | name | age |
+----+------------+------+------+
| 3 | 202501 | ll | 10 |
+----+------------+------+------+
运算符 | 示例 | 描述 |
---|---|---|
= | a=b | a等于b |
<>或!= | a<>b或a!=b | a不等于b |
< | a<b | a小于b |
<= | a<=b | a小于等于b |
> | a>b | a大于b |
>= | a>=b | a大于等于b |
BETWEEN … AND… | a BETWEEN b and c | b<=a<=c |
NOT BETWEEN … AND… | a BETWEEN b and c | a>c或a <b |
IS NULL | a IS NULL | a是空值 |
IS NOT NULL | a IS NOT NULL | a不是空值 |
LIKE | a like 2025% | a匹配以2015开头的值,%匹配0或多个任意字符,_匹配一个任意字符字符 |
表达式之间可以用 AND和OR连接,AND的优先级大于OR.
表达式和函数
表达式中的NULL
- NULL作为算数运算的操作数时,结果总是NULL
- 除IS NULL 和 IS NOT NULL和<=>意外,NULL作为比较操作符的操作数时,结果总是NULL
- <=>被称为空值安全运算符,当两个操作数都不是NULL时,它和 = 相同.当一个操作数为NULL时返回0,两个都为NULL时返回1.
mysql> select left('abc123',3),right('abc123',3),upper('abc'),lower('ABC');
+------------------+-------------------+--------------+--------------+
| left('abc123',3) | right('abc123',3) | upper('abc') | lower('ABC') |
+------------------+-------------------+--------------+--------------+
| abc | 123 | ABC | abc |
+------------------+-------------------+--------------+--------------+
select length('abc123'),ltrim(' abc '),rtrim(' abc '),substring('abc123',2,3),concat(123,'abc');
+------------------+------------------+------------------+-------------------------+-------------------+
| length('abc123') | ltrim(' abc ') | rtrim(' abc ') | substring('abc123',2,3) | concat(123,'abc') |
+------------------+------------------+------------------+-------------------------+-------------------+
| 6 | abc | abc | bc1 | 123abc |
+------------------+------------------+------------------+-------------------------+-------------------+
select date_format(now(),'年:%Y,月:%m,日:%d 时:%H,分:%m,秒:%s');
+----------------------------------------------------------------+
| date_format(now(),'年:%Y,月:%m,日:%d 时:%H,分:%m,秒:%s') |
+----------------------------------------------------------------+
| 年:2025,月:02,日:12 时:22,分:02,秒:10 |
+----------------------------------------------------------------+