首页 > 数据库 >MySQL的基本语句

MySQL的基本语句

时间:2023-09-02 18:55:05浏览次数:50  
标签:语句 基本 account 查询 wbnyua username MySQL password id

目录

1.0数据库操作

# 创建数据库
create database test;

# 删除数据库
drop database test;

2.0 数据表操作

2.1 表的创建

# 创建数据表
create table account(
    id bigint primary key auto_increment comment '用户id',
    username varchar(20) unique comment '账号',
    `password` varchar(200) comment '密码',
    create_at timestamp default current_timestamp comment '创建时间',
    update_at timestamp on update current_timestamp comment '修改时间'
)comment '用户账号表';

2.2 表的修改

2.2.1 表中字段的添加

# 在 account 表中添加一个字段:role
alter table account add column role int comment '角色类型';

2.2.2 表中字段的修改

# 修改表 account 中 role 字段的类型
alter table account modify role varchar(10) comment '角色类型';

2.2.3 表中字段的删除

# 删除表 account 中的 role 字段
alter table account drop column role;

2.3 表的查询

2.3.1 查询数据库中所有的表

# 查询数据库 test 下的所有表
show tables from test;

2.3.2 查询表结构

# 方式一
show columns from account;
# 方式二
describe account;
# 方式三
desc account;

2.4 表的删除

drop table account;

2.5 表中数据的操作

2.5.1 表数据的查询

2.5.1.1 表中数据的简单查询
# 查询所有字段
select * from account;

# 查询所有字段
select id,username,password,create_at,update_at from account;

# 查询某个字段
select username from account;

# 查询某些字段
select id,username,password from account;
2.5.1.2 表中数据的条件查询
# 查询表 account 中 username 是 wbnyua 的用户
select id,username,password from account where username = 'wbnyua';

# 查询表 account 中 username 是 wbnyua 且 password 是 wbnyua123 的用户
select id,username,password from account where username = 'wbnyua' and password = 'wbnyua123';

# 查询表 account 中 username 不是 wbnyua 的用户
select id,username,password from account where username <> 'wbnyua';

# 查询表 account 中 username 前面6个字符是 wbnyua 的用户(包括用户名是 wbnyua)
select id,username,password from account where username like 'wbnyua%';

# 查询表 account 中 username 前面6个字符是 wbnyua ,后面个字符是其他任何字符的用户(不包括用户名是:wbnyua)
# (_ : 表示匹配任意字符)
select id,username,password from account where username like 'wbnyua_';

# 查询 id 在 1 到 3 之间的用户(不包括 id=1 和 id=3 )
select * from account where id > 1 and id < 3;

# 查询 id 在 1 到 3 之间的用户(包括 id=1 和 id=3 )
select * from account where id >= 1 and id <= 3;
# 这条查询语句等价与上面那条
select * from account where id between 1 and 3;

# 查询表 account 并根据 id 降序排序 (默认升序 asc)
select * from account order by id desc;

# 查询表 account 中 id=1 id=2 id=3 的用户
select * from account where id in (1,2,3);

# 查询语句还会更新

2.5.2 表数据的添加

# 添加一条数据
insert into account(username,password) value ('wbnyua','wbnyua123');

# 添加多条数据
insert into account(username,password) values ('wbnyua1','12345'),('wbnyua2','12345');

2.5.3 表数据的修改

# 修改语句的条件关键字的使用和查询语句差不多,就不展开写了

# 修改表 account 中 username 是 wbnyua 的 username 和 password
update account set username = 'wbnyua_admin' , password = '12345000' where username = 'wbnyua';

# 修改表 account 所有记录的 password 为 12345678
update account set password = '12345678';

2.5.3 表数据的删除

# 删除语句的条件关键字的使用和查询语句差不多,就不展开写了

# 删除表 account 中 username 是 wbnyua 的数据
delete from account where username = 'wbnyua';

# 清空数据(id 不会 从新开始递增)
delete from account;

# 截断表(id 会 从新开始递增)
truncate table account;

标签:语句,基本,account,查询,wbnyua,username,MySQL,password,id
From: https://www.cnblogs.com/wbnyua/p/17674043.html

相关文章

  • CentOS6.5安装mysql 远程登录
    第1步、yum安装mysql[[email protected]]#yum-yinstallmysql-server直到出现结果: 第2步、设置开机启动[[email protected]]#chkconfigmysqldon这步没提示第3步、启动mysql服务[[email protected]]#servicemysqldstartshell提示: 第4步、修改r......
  • mysql索引(转)
    转载:https://www.php.cn/faq/493277.html一、数据结构区分1.1.B+tree索引根据存储方式,mysql可以分为B+tree索引和哈希索引B+tree索引可以进行全键值、键值范围和键值前缀查询1.2.哈希索引哈希索引也称为散列索引或 HASH索引。MySQL目前仅有MEMORY存储引擎和HEAP存......
  • mysql decode()
    mysqldecode()    举例:    oracle: select  decode(pay_name, ' aaaa ' , ' bbb ' ,pay_name), sum (comm_order), sum (suc_order), sum (suc_amount)  From   payment.order_tab   group   by  decode(pay_name, ' aaaaa ' , ' bb......
  • 12.mysql数据查询
    下面是一些MySQL数据库中的数据查询操作示例,包括单表查询和多表查询,以及相应的示例数据表。单表查询:假设我们有一个名为products的表,用于存储产品信息:CREATETABLEproducts(product_idINTPRIMARYKEY,product_nameVARCHAR(255),categoryVARCHAR(50),......
  • 9.mysql 高可用性和故障恢复
    当考虑在MySQL数据库中实现高可用性和故障恢复时,以下是更详细的步骤和策略:主从复制(Master-SlaveReplication):配置一个主数据库和多个从数据库。启用二进制日志(binarylog)和从数据库的复制功能。设置适当的复制方式(异步复制通常用于高可用性,但可能会有些许延迟)。使......
  • mysql监控和维护
    对MySQL进行监控和维护是确保数据库性能和稳定性的关键部分。以下是一些常见的MySQL监控和维护任务:1.监控工具和服务:MySQLWorkbench:这是MySQL官方提供的一款图形化管理工具,提供性能监控和诊断工具。PerconaToolkit:包括各种有用的工具,如pt-query-digest用于分析慢查询、p......
  • 【Mysql | 空值处理 】
    MySQL中,空值通常用于表示缺失或未定义的值。处理空值的关键在于理解空值与其他值之间的关系,以及如何使用不同的SQL函数来处理和转换空值。(空值处理)NULLValues(空值)MySQL使用SQLSELECT命令及WHERE子句来读取数据表中的数据,但是当提供的查询条件字段为NULL时,该......
  • H1H1Wifi模块-ESP-01s使用的基本操作
    H1H1Wifi模块-ESP-01s蓝牙,ESP-01s,Zigbee,NB-Iot等通信模块都是基于AT指令的设计AT指令简介AT指令集是从终端设备(TerminalEquipment,TE)或数据终端设备(DataTerminalEquipment,DTE)向终端适配器(TerminalAdapter,TA)或数据电路终端设备(DataCircuitTerminalEquipment,DCE)发......
  • centos查看mysql默认密码和修改密码
    1、查看mysql默认密码:grep‘temporarypassword’/var/log/mysqld.logroot@localhost:b_1sZou9FZrtb_1sZou9FZrt就是2、修改mysql密码:ALTERUSER‘root’@‘localhost’IDENTIFIEDBY‘newpassword’;‘newpassword’替换成你要设置的密码,注意:密码设置必须要大小写字母数......
  • mysql周week函数
    WEEK(date[,mode])WEEK()函数会返回一个日期的周数,第2个参数mode可以指定一周是从周日开始还是周一开始,以及返回值的范围是[0,53]还是[1,53],如果第2个参数缺失了,则使用系统变量default_week_format的值ModeFirstdayofweekRangeWeek1isthefirstweek…......