博客
关于我
MySQL-【4】基本操作
阅读量:795 次
发布时间:2023-02-11

本文共 2762 字,大约阅读时间需要 9 分钟。

SQL 数据库操作指南

1. 创建数据库

create database 数据库名;

数据库是存储数据的主要容器,使用适当的名称来组织你的数据。

2. 显示数据库

show databases;

这将列出你当前正在使用的所有数据库。

3. 删除数据库

drop database 数据库名;

谨慎操作,删除数据库会永久丢失所有数据。

4. 使用指定数据库

use 数据库名;

切换到指定数据库后,你的后续操作将针对该数据库进行。

5. 创建数据表

create table 表名(字段名1 类型1 [, ... 字段名n 类型n]);
  • 示例:
create table myTable(id int(4) not null primary key auto_increment,name char(255),score float(8,2));

6. 删除数据表

drop table 表名;

删除表会移除其所有数据和结构。

7. 插入数据

insert into 表名[(字段名1[, ... 字段名n])] values (值1[, ... 值n]);
  • 示例:
insert into myTable (id,name,score) values(1,"tom",93.65);

8. 查询数据表数据

select 字段1, 字段2, ... from 表名 where 表达式;
  • 示例:
select * from myTable;

9. 查询前两行数据

select * from myTable order by 字段名 limit 数量,偏移量;
  • 示例:
select * from myTable order by id limit 0,2;

10. 查询大于60分的数据

select * from 表名 where 字段名 > 条件值;
  • 示例:
select * from myTable where score > 60;

11. 创建另一个表

create table 表名(字段名1 类型1 [, ... 字段名n 类型n]);
  • 示例:
create table myTable2(id int(4) not null primary key,name char(255),grade double(8,2));

12. 查询两个表分数大于60

select * from 表名1, 表名2 where (字段名1 > 60 and 字段名2 > 60);
  • 示例:
select * from myTable,myTable2 where (score > 60 and grade > 60);

13. 删除表中的数据

delete from 表名 where 条件表达式;
  • 示例:
delete from myTable2 where id=4;

14. 修改表中的数据

update 表名 set 字段名 = 新值 where 条件表达式;
  • 示例:
update myTable2 set name="小白" where id=3;

15. 增加表中字段

alter table 表名 add 字段名 类型 [默认值];
  • 示例:
alter table myTable2 add brother int(4) default 0;

16. 修改表名

rename table 原表名 to 新表名;
  • 示例:
rename table myTable to myNewTable;

17. 查询两个表字段重复的情况

select ah_day_table.id as ah_day_table_id, GROUP_CONCAT('系统订单',ah_order_table.id,'',ah_order_table.orderStatus) as ah_order_table_need_datafrom ah_day_table join ah_order_table on ah_day_table.orderPersonName = ah_order_table.orderPersonNamegroup by ah_day_table.id;

18. 批量更新数据

update 表名 set 字段名 = 表达式 when 条件1 then 值1 when 条件2 then 值2 ... end where 条件表达式;
  • 示例:
update ah_sf_table setah_sf_table.lastLogisticsStatus = casewhen  billCode='运单号A'  then '退回签收'when  billCode='运单号B'  then '退回签收'ENDWHERE billCode IN ('运单号A','运单号B');

19. 批量插入数据

insert into 表名 (字段名1[, ... 字段名n]) values (值1[, ... 值n]);
  • 示例:
insert into ah_day_table(orderPersonName,orderPhone,orderTime,orderCode,orderCount,orderContent,orderMark,orderAddress,orderIP,orderPrice) values(    '小王', '137xxxxxxxx', '2021-05-31 17:03:29','xxxx', '1','xxxx', 'xxxx','xxxx','xxxxx','19.0'),    ('小张', '137xxxxxxxx', '2021-05-31 17:03:29','xxxx', '2','xxxx', 'xxxx','xxxx', 'xxxxx','19.0'),    ('小李', '137xxxxxxxx', '2021-05-31 17:03:29','xxxx', '3','xxxx', 'xxxx','xxxx', 'xxxxx','19.0'),    ('小孙', '137xxxxxxxx', '2021-05-31 17:03:29','xxxx', '4','xxxx', 'xxxx','xxxx', 'xxxxx','19.0');

20. 批量删除数据

delete from 表名 where 字段名 in (值1, 值2, ...);
  • 示例:
delete from ah_day_table where id in (20,21,22,23);

以上操作可以帮助你高效管理数据库,熟练掌握这些命令将显著提升你的数据库操作能力。

转载地址:http://ocbfk.baihongyu.com/

你可能感兴趣的文章
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>