本文整理自网络,侵删。
目录
- MySQL 基础常用命令
- 1. SQL语句
- 2. 建表
- 3.字段属性
- 4.修改表:alter table
- 5. 增删改查:字符串全部使用''包起来
- 5.1 增
- 5.2 删
- 5.3 改
- 5.4 查
- 6. 子句
- 7.limit分页
- 8.去重
- 9.聚合函数
- 10.拼接
- 11.日期函数
- 12. 数组计算
- 13.排序
- 14. group by 分组
MySQL 基础常用命令
注意:MySQL在centos中安装的是5.7版本的,编辑MySQL时会有个报错,需要执行:
set@@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
1. SQL语句
每个命令执行结束加分号结束
- 查询所有数据库:show databases;
- 切换数据库:use 库命名;
- 创建数据库:create database [IF NOT EXISTS] 库名;
- 删除数据库:drop database [IF EXISTS] 库名;
- 查询数据库创建:show 建库语句;
- 指定数据库采用的字符集:CHARACTER SET
- 修改数据库的编码集:alter database 数据库名 CHARACTER SET 编码集;
注意:不要修改mysql服务器的编码集,表的编码集默认和库一致
2. 建表
格式:
- create table [if not exists] 表名(
- 字段1 数据类型 字段属性,
- 字段2 数据类型 字段属性,...
- 字段N 数据类型 字段属性
- )engine=引擎 default charset=编码集;
- 查看当前数据库:select database();
- 查看建表语句:show create table 表名;
- 查看表结构:desc 表名;
- 删除:drop table [if exists] 表名;
3.字段属性
- not null:没给值数据为默认值(varchar默认值为空
- AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1
- PRIMARY KEY关键字用于定义列为主键,您可以使用多列来定义主键,列间以逗号分隔
- ENGINE 设置存储引擎,CHARSET 设置编码
- default null:没给值数据就是null
- default 值:设置字段的默认值
注意:主键不重复的列
这里我们建立一个student表:
create table if not EXISTS student ( id int auto_increment, `name` VARCHAR(32), age int, sex char(1), clazz VARCHAR(32)) charset utf8;
insert into student values (1001,'zs',18,'男','一班'); insert into student values (1002,'ls',19,'女','二班'); insert into student(`name`,age,sex,clazz) values ('ww',69,'男','一班'); insert into student(`name`,age,sex,clazz) values ('we',21,'女','二班'); insert into student(`name`,age,sex,clazz) values ('ld ',23,'男','一班'); insert into student(`name`,age,sex,clazz) values ('lq',45,'女','二班'); insert into student(`name`,age,sex,clazz) values ('lwq',23,'男','一班'); insert into student(`name`,age,sex,clazz) values ('ld',12,'女','二班');
4.修改表:alter table
修改表名:alter(rename) table 旧表名 to 新表名;
rename table student1 TO `student`;
添加字段:alter table 表名 add 字段 字段数据类型 属性;
alter table student add job varchar(32) default '没有工作' ; insert into student (job) VALUES('a'); insert into student (job) VALUES('b'); insert into student (job) VALUES('c'); insert into student (job) VALUES('a'); insert into student (job) VALUES('b');
修改字段:alter table 表名 change 旧字段 新字段 数据类型 属性;
alter table student change clazz clazz varchar(255); alter table student change age score double;
修改字段:alter table 表名 modify 字段 数据类型 属性;
alter table student MODIFY varchar(356); #这里不能比之前的空间小
注意:
- change:修改所有(字段名,数据类型,属性)
- modify:修改一部分(数据类型,属性)
- 修改数据类型时,varchar->int元数据会变为0
5. 增删改查:字符串全部使用''包起来
5.1 增
格式:
insert into 表名(字段) values(值),(值)...(值); insert into student values (1001,'zs',18,'男','一班'); insert into student values (1002,'ls',19,'女','二班'); insert into student(`name`,age,sex,clazz) values ('ww',69,'男','一班'); insert into student(`name`,age,sex,clazz) values ('we',21,'女','二班'); insert into student(`name`,age,sex,clazz) values ('ld ',23,'男','一班'); insert into student(`name`,age,sex,clazz) values ('lq',45,'女','二班'); insert into student(`name`,age,sex,clazz) values ('lwq',23,'男','一班'); 10 insert into student(`name`,age,sex,clazz) values ('ld',12,'女','二班');
5.2 删
-- 删除delete from 表名 where 子句; delete from student where job='c';
5.3 改
-- 改update 表名 set 字段1=值1,字段2=值2...字段N=值N where 子句; update student set job='b'where name ='ls';
5.4 查
-- 查select 字段 from 表名 where 子句; select * from student ; #查询全部 SELECT id as di,name,job,score from student where score>18; #特定查询,并且展示特定的表 as:表示改字段名称(原来的表不发生变化)
注意:表示所有字段
6. 子句
- > < <= >= = <> 大于、小于、大于(小于)等于、不等于
- between ...and... 显示在某一区间的值(含头含尾)
- in(set) 显示在in列表中的值,例:in(100,200)只能匹配100或200
- like '张_' 模糊查询 使用% 和 _(%表示匹配所有 _匹配一个)
- Is null 判断是否为空
- and 多个条件同时成立
- or 多个条件任一成立
- not 不成立,例:where not(expection>10000);
-- > < <= >= = != 大于、小于、大于(小于)等于、不等于 SELECT * from student WHERE id>1006; SELECT * from student WHERE id!=1006; --between ...and... 显示在某一区间的值(含头含尾) select id,name,job from student where id BETWEEN 1002 and 1005; select * from student where job BETWEEN 'a' and 'b'; -- in(set) 显示在in列表中的值,例:in(100,200)只能匹配100或200 select * from student where job in('a','b'); -- like '张_' 模糊查询 使用% 和 _(%表示匹配所有 _匹配一个) SELECT * from student where name like 'l%'; SELECT * from student where name like 'l_'; select * from student where name is not null;
7.limit分页
格式:
语句 limit 开始下标,长度;
-- limit分页 语句 limit 开始下标,长度;注意:没有where select * from student LIMIT 1,2; select * from student LIMIT 0,2; select * from student LIMIT 2;
注意:
如果数据量不够,显示全部
8.去重
格式:
DISTINCT 字段1,字段2...字段N
-- 去重 DISTINCT 字段1,字段2...字段N select DISTINCT name from student; select count(DISTINCT name) from student;
注意:
相关阅读 >>
sql server 分组统计并合计总数及with rollup应用
sql server中的xml数据进行insert、update、delete
asp连接sql和access数据代码(asp里的随机函数)
更多相关阅读请进入《sql》频道 >>

数据库系统概念 第6版
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » MySQL 基础常用命令总结
标签:sql
相关推荐
评论
管理员已关闭评论功能...