博客
关于我
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中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>