linux下使用mysql(上)

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 表名;
显示创建表的sqlshow 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=ba等于b
<>或!=a<>b或a!=ba不等于b
<a<ba小于b
<=a<=ba小于等于b
>a>ba大于b
>=a>=ba大于等于b
BETWEEN … AND…a BETWEEN b and cb<=a<=c
NOT BETWEEN … AND…a BETWEEN b and ca>c或a <b
IS NULLa IS NULLa是空值
IS NOT NULLa IS NOT NULLa不是空值
LIKEa 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            |
+------------------+------------------+------------------+-------------------------+-------------------+

日期处理函数date_format

 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                          |
+----------------------------------------------------------------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值