当前位置: 代码网 > it编程>数据库>Mysql > Mysql常见的SQL语句格式及实用技巧

Mysql常见的SQL语句格式及实用技巧

2025年06月24日 Mysql 我要评论
一、常用语法汇总数据库(database)表(table)记录增create database [if not exists] database_name;create table [if not e

一、常用语法汇总

数据库(database)表(table)记录
create database [if not exists] database_name;create table [if not exists] table_name (
  column1 datatype [constraint],
  column2 datatype [constraint],
  ...
  [primary key (column_list)]
);

-- 插入记录(单条)
insert into table_name (column1, column2, ...)
values (value1, value2, ...);

-- 插入多条记录
insert into table_name (column1, column2, ...)
values (value1_1, value1_2, ...),
       (value2_1, value2_2, ...);

drop database [if exists] database_name;drop table [if exists] table_name;delete from table_name where condition;
-- 切换数据库
use database_name;
-- 修改表结构(示例:添加列)
alter table table_name add column_name datatype;
update table_name
set column1 = value1, column2 = value2, ...
where condition;
show databases;

-- 查看所有表
show tables;

-- 查看表结构
describe table_name;

-- 查询记录(全量)
select * from table_name;

-- 查询特定字段
select column1, column2 from table_name;

-- 条件查询
select * from table_name where condition;

-- 排序查询
select * from table_name order by column [asc|desc];

-- 分页查询(limit 从 0 开始)
select * from table_name limit offset, count;

常用约束与修饰符
-- 主键约束(唯一标识)
primary key (column)
-- 唯一约束(值不能重复)
unique (column)
-- 非空约束
not null
-- 默认值
default value
-- 自增(整数类型)
auto_increment

二、示例

1.数据库操作

-- 创建数据库(存在则跳过)
create database if not exists db_name;
-- 删除数据库(谨慎使用)
drop database if exists db_name;
-- 查看所有数据库
show databases;
-- 切换数据库
use db_name;

2.表操作

(1)创建表

create table if not exists users (
  id int primary key auto_increment,
  name varchar(50) not null,
  age int default 18,
  email varchar(100) unique,
  created_at timestamp default current_timestamp
);

(2)修改表结构

-- 添加列
alter table users add column phone varchar(20);
-- 修改列类型
alter table users modify column age tinyint;
-- 重命名列
alter table users change column phone mobile varchar(20);
-- 删除列
alter table users drop column mobile;

(3)删除表

drop table if exists users;

(4)查看表信息

-- 查看表结构
describe users;
-- 查看建表语句
show create table users;

3.记录操作 

(1)插入数据

-- 单条插入
insert into users (name, age, email)
values ('alice', 25, 'alice@example.com');
-- 批量插入
insert into users (name, age)
values ('bob', 30), ('charlie', 22);
-- 插入查询结果
insert into users_backup (name, age)
select name, age from users where age > 25;

(2)查询数据

-- 基础查询
select * from users;
-- 条件查询
select name, age from users where age > 25 and name like 'a%';
-- 去重查询
select distinct age from users;
-- 排序与分页
select * from users order by age desc limit 10 offset 20;
-- 聚合函数
select count(*), avg(age), max(age) from users;
-- 分组查询
select age, count(*) from users group by age having count(*) > 1;

(3)更新数据

-- 单条件更新
update users set age = age + 1 where name = 'alice';
-- 多字段更新
update users set age = 30, email = 'bob@new.com' where id = 2;
-- 批量更新(基于子查询)
update users u
join user_scores s on u.id = s.user_id
set u.age = s.age + 5;

(4)删除数据

-- 按条件删除
delete from users where age < 18;
-- 清空表(保留结构)
truncate table users;

4.高级查询

(1)多表关联

-- 内连接
select u.name, o.order_id
from users u
join orders o on u.id = o.user_id;
-- 左连接
select u.name, o.order_id
from users u
left join orders o on u.id = o.user_id;

(2)子查询

select * from users
where age > (select avg(age) from users);

(3)联合查询

select name from users
union
select product_name from products;

三、实用技巧

1.避免全表扫描:为查询字段添加索引

create index idx_age on users (age)

2.事务处理:保证数据一致性

start transaction;
update accounts set balance = balance - 100 where id = 1;
update accounts set balance = balance + 100 where id = 2;
commit; -- 或 rollback

3.临时表:提升查询效率与隔离性,会话内有效,会话关闭自动删除。

-- 创建临时表并填充数据
create temporary table temp_orders as
select order_id, user_id, amount
from orders
where create_time > '2023-01-01';
-- 使用临时表进行后续查询
select user_id, sum(amount)
from temp_orders
group by user_id;

到此这篇关于mysql常见的sql语句格式的文章就介绍到这了,更多相关mysql sql语句格式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com