mysql常用命令写法

1.mysql常用命令

显示当前mysql服务器上的所有数据库 show databases; 选择要使用的数据库 use 数据库名; 显示当前数据库中的所有表 show tables; 显示当前使用的数据库 select database(); 查看表结构 desc 表名; 查看表中所有数据 select * from 表名;

2.数据库操作命令

创建数据库同时指定默认的字符集 create database 数据库名 default CHARACTER set utf8; 删除数据库 drop database 数据库名;

3.mysql常用数据类型

整数: int bigint 小数 decimal 长度 精度 decimal(18,2) 字符 char(长度) varchar(长度) char(8) 固定保存8位长度的字符串 varchar(8) 最多保存长度为8的字符串 日期 date time datetime 二进制 blob 枚举 enum 从固定取值范围的数据中取一个 比如性别 集合 set 从固定取值范围的数据中取一个或多个 比如不定项选择题的答案

4.表操作命令

建表 create table 表名 列名 数据类型, 列名 数据类型, ....... 列名 数据类型 删除表 drop table 表名;

5.数据操作命令

5.1 增加数据

给表中指定的列赋值 insert into 表名(列名,列名,列名) values(值,值,值); 给表中的所有列赋值 insert into 表名 values(值,值,值,值,值);

5.2 删除数据

delete from 表名 where 条件 例子: 删除所有答案是A的记录 delete from question where answer='A' 删除question表中的所有记录 delete from question; 删除答案带A的记录 delete from question where ansswer like '%A%' 删除答案不带A的记录 delete from question where ansswer not like '%A%'

5.3 运算符

关系运算符 > < >= <= = != <> 逻辑运算符 and or not like 模糊匹配 % 任意字符 '张%' 所有姓张的 _ 一个任意字符 ‘张_’ 姓张的姓名只有两个字的

5.4修改数据

update 表名 set 列名=,列名=值 where 条件 修改题目位3+2=?A选项和B选项 update question set optiona='aaaaaa',optionb='bbbbb' where title='3+2=?'

5.4 查询数据

1 简单筛选

select 列名,列名,列名 from 表名 where 条件

2.排序

select 列名,列名....  from 表名 where 条件   order by 列名 asc/desc;

3.模糊匹配

like in select * from employees where emp_no in(499999,499572,499525); select * from employees where emp_no=499999 or emp_no=499572 or emp_no=499525; between and 查询出生日期在1960-1-11960-7-1 之间的员工信息 select * from employees where birth_date between '1960-1-1' and '1960-7-1'

4.聚合函数

max 最大值 max(列名) max(表达式) min 最小值 sum 总和 avg 平均值 count 计数 select max(列名) from 表名 where 条件 error: select * from 表名 where max(emp_no); error: select * from 表名 where emp_no=max(emp_no) select * from employees where emp_no = ( select max(emp_no) from employees )

5. 分组

select count(*),gender from employees group by gender;

6.复杂查询(嵌套)

select .... from ..... where ..... 在分组之前对数据进行筛选(不能使用聚合函数) group by .... having ..... 对分组后的数据进行筛选(可以使用聚合函数) order by.......

7.联合查询

1. 内部联结 A select first_name,last_name,dept_name,from_date,to_date from employees,departments,dept_emp where employees.emp_no =dept_emp.emp_no and dept_emp.dept_no=departments.dept_no and employees.emp_no=10003 and now() between from_date and to_date; B select first_name,last_name,dept_name,from_date,to_date from employees inner join dept_emp on employees.emp_no=dept_emp.emp_no inner join departments on departments.dept_no=dept_emp.dept_no where employees.emp_no=10003 and now() between from_date and to_date; 2.左外部联结 left outer join 3.右外部联结 right outer join 4 完整外部连接 full join
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''

mysql常用命令写法 - HelloWorld