千家信息网

常用的一些mysql语句及用法

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,本文主要给大家简单讲讲常用的一些mysql语句及用法,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望常用的一些mysql语句及用法这篇文章可以给大家带来
千家信息网最后更新 2025年02月07日常用的一些mysql语句及用法

本文主要给大家简单讲讲常用的一些mysql语句及用法,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望常用的一些mysql语句及用法这篇文章可以给大家带来一些实际帮助。

1.开启mysql数据库服务
格式: net start mysql

如: net start mysql57

2.关闭mysql数据库服务
格式: net stop mysql
如: net stop mysql57

3.登陆mysql服务
格式: mysql -u root -p
如: mysql -u root -p 执行成功后,在输入密码,

4. 查看当前数据库的版本xinxi
格式: select version();

5.查看当前时间
格式: select now();

6.退出,

quit,exit

2.数据库database的操作

1.查看当前有哪些数据库

格式: show databases;

2.创建一个数据库

格式: create database 数据库名 charset=utf8;

例: create database wen charset=utf8;

3.删除一个数据库

格式: drop database 数据库名;

例: drop database wen;

4.使用数据库

格式:use 数据库名;

例: use wen;

5.查看当前使用数据库

格式: select database();

3.表table的操作

1.查看当前数据库有哪些表

格式: show tables;

2.创建一个表

格式: create table 表名(字段名及类型);

例: create table student(id int auto_increment primary key,name varchar(20) not null,age int not null,address varchar(20),gender bit default 1);

3.查看表的结构

格式: desc 表名;

例: desc student;

4.查看创建表的语句

格式: show create table 表名;

5.删除表

格式: drop table 表名;

例: drop table student;

6.修改表

1.修改表的名字

格式: rename table 旧表名 to 新表名;

例: rename table student to students;

2.修改表的结构, add | drop | change

1.add 添加一个新的字段

格式: alter table 表名 add 字段名 类型;

例: alter table student add phone int;

2.change 修改

格式: alter table 表名 change 旧字段名 新字段名 新字段类型;

例: alter table student change phone number varchar(20);

3.drop 删除

格式: alter table 表名 drop 字段名;

例: alter table student drop number;

4.数据的操作

1.插入数据

1.插入全列数据

格式: insert into 表名 values(值1,值2,....)

注意:值得顺序需要与表的结构对应, 主健可以写0  

例: insert into student values(0,"小明",36,"北京朝阳区",1);

  2.缺省插入

   格式:insert into 表名(字段名1,字段名2,....) values(值1,值2,....) 

     例: insert into student(name,age,address) values("小东",48,"深圳南山区");

3.插入多条数据

格式: insert into 表名 values(值1,值2,....),(值1,值2,....),(值1,值2,..)

例: insert into student values(0,"小云",45,"杭州",0),(0,"小雨",105,"俄罗斯",0),(0,"小清",99,"日本",0);

2.删除数据

格式: delete from 表名 where 条件

例: delete from student where age = 105;

delete from student

不加条件表示全部删除, 请谨慎使用

3.修改数据

格式:update 表名 set 字段名1=值,字段名2=值,... where 条件;

update student set gender = 1 where name = "小青";

update student set age = 52,address="深圳" where name = "小东";

4.查看所有数据

格式: select * from 表名;

例: select * from student;

5.查询数据

1.查询格式

1.查询所有

格式: select * from 表名;

2.按条件查询

格式: select 字段名1,字段名2,... from 表名 where 条件;

1.select后跟的是查询结果需要显示的字段名

2. * 表示所有的字段名都要显示

3. from 后跟的是表名,表示从那个表查

4. where 后跟的是筛选条件

5. 可以使用 字段名 as 别名 来给字段名取个别名

例: 显示 name和age

select name,age from student;

select name as "姓名" from student;

2.条件中的运算

1.比较运算符

> 大于

< 小于

>= 大于等于

<= 小于等于

= 等于

!= 不等于

格式: select 字段名1,字段名2,... from 表名 where 字段名 比较运算符 值;

例: select * from student where age < 50;

2. 逻辑运算符

and 且

or 或者

not 非

格式: select 字段名1,字段名2,... from 表名 where 字段名 比较运算符 值 逻辑运算符 字段名 比较运算符 值 ...;

例: select * from student where age < 50 and age > 40;

select * from student where not (age < 50 and age > 40);

3.模糊运算符 like

% 表示多个任意字符

_ 表示一个任意字符

需求: 匹配所有姓小的人
格式: select * from student where name like "小%";
格式: select * from student where name like "小_";
需求: 匹配名字中有 "小"字的
select * from student where name like "%小%";

4.范围查询
in (多个值 ) 判断一个值 是否是多个值中的一个
between 值1(包含) and 值2(包含) 判断一个值是否在 值1与值2之间
格式: select 字段名1,字段名2,... from 表名 where 字段名 范围运算符 值;
例: 找出 25或者45或者80或者 90
select * from student where age in (25,45,80,90);
需求:找出25 到 45之间的数据
select * from student where age between 25 and 45;
5. 空判断
is null 为空
is not null 不为空
格式: select 字段名1,字段名2,... from 表名 where 字段名 is null;
插入数据: insert into student(name,age) values("小·超",60);
例: select * from student where address is null;
select * from student where address is not null;
6.去除重复的值
格式: select distinct 字段名 from 表名 where 条件;
例: select distinct gender from student;
7.聚合函数
count(*) 求当前结果总共有多少条数据
sum(列名) 求列名对应列的 和
avg(列名) 求当前列的平均值
max(列名) 求当前列的最大值
min(列名) 求当前列的最小值
例: 求当前表总共有多少条数据?
select count(*) from student;
求年龄最小的?
select min(age) from student;

8.分组 group by
格式: select 字段名... from 表名 where 条件 group by 字段名

查看有多少种性别

例: select gender from student group by gender;

需求:统计 男生 和 女生 各有多少个
select gender,count(*) from student group by gender;
需求: 统计所有女生的个数?
例: select gender,count(*) from student group by gender having gender = 1;
where 查询条件, 是先执行的查询条件
having 查询条件 是在查询结果的基础上在查询
9. 排序
格式: select 字段名... from 表名 where 条件 order by 字段名1,字段名2...
例: 年龄小到大
select * from student order by age;
默认是从小到大排列
asc 从小到大
desc 从大到小
select * from student order by age asc

10. 分页

格式: select 字段名... from 表名 where 条件 limit 起始值,多少条数据
起始值可以从 0 开始
例: select * from student limit 0,3;

常用的一些mysql语句及用法就先给大家讲到这里,对于其它相关问题大家想要了解的可以持续关注我们的行业资讯。我们的板块内容每天都会捕捉一些行业新闻及专业知识分享给大家的。

格式 字段 数据 条件 数据库 查询 运算 运算符 需求 语句 常用 前列 后跟 多个 类型 结构 结果 服务 最小 从小到大 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 火车列车时刻数据库 手机能不能学软件开发 松原市网络安全等级保护测评 怎么看电脑服务器和数据库名称 手术室软件开发 国电通网络技术股份有限公司 美版我的世界起床战争服务器 北京现代化软件开发特点 微信发小孩照片的网络安全吗 江西局域网存储服务器 软件开发培训班哪家好排行榜 第八届网络安全最新新闻 电脑键入网络安全密钥不匹配 银屑病医疗保健数据库 中国计算机及网络安全中心 淘宝云服务器能赚钱吗 甲骨文云服务器流量查询 新浪手机邮箱服务器手机怎么设置 静安区银联网络技术管理方法 软件开发的资源风险 医药公司网络安全事件 网络安全师怎么翻译 做数据库处理 装了安全狗服务器密码不好改 重庆库存备件管理软件开发 网络安全教育系列视频下载 手机怎么设置云服务器地址 数据库运维报价 浦东新区信息软件开发产品介绍 华为信号服务器
0