本文谈谈MySQL的开发必会的sql语句

创建数据库

create database db1;

删除数据库

drop database db1;

创建数据表

create table tb1用户表(

id int not null auto_increment primary key,

name char(10),

department_id int,

p_id int,

)engine=innodb default charset=utf8;

主键(primary key)一个表只能有一个主键,主键可以由一列或者多列组成

外键的创建

CREATE TABLE t5 (

nid int(11) NOT NULL AUTO_INCREMENT,

pid int(11) not NULL,

num int(11),

primary key(nid,pid) --这里就是把两列设置成了主键

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table t6(

id int auto_increment primary key,

name char(10),

id1 int,

id2 int,

CONSTRAINT fk_t5_t6 foreign key (id1,id2) REFERENCES t1(nid,pid) --这里是设置外键

)engine=innodb default charset=utf8;

数据行的操作

数据的插入

insert into tb1(name,age) values('ax',8);

insert into tb12(name,age) select name,age from tb11;

表中的数据的删除

delete from t1;

truncate table t1;

drop table t1

delete from tb1 where id > 10

delete from tb12 where id >=2 or name='alex'

数据的更新

update tb1 set name='root' where id > 10

数据的查询

select from tb;

select id,name from tb;

表结构的查看

show create table t1;

desc t1;

其他

select from tb12 where id != 1

select from tb12 where id in (1,5,12);

select from tb12 where id not in (1,5,12);

select from tb12 where id in (select id from tb11)

select from tb12 where id between 5 and 12;

通配符

select from tb12 where name like "a%"

select from tb12 where name like "a_"

分页

select from tb12 limit 10;

select from tb12 limit 0,10;

select from tb12 limit 10,10;

select from tb12 limit 20,10;

select * from tb12 limit 10 offset 20;

page = input('请输入要查看的页码')

page = int(page)

(page-1) * 10

select * from tb12 limit 0,10; 1

select * from tb12 limit 10,10;2

排序

select from tb12 order by id desc; 大到小

select from tb12 order by id asc; 小到大

select * from tb12 order by age desc,id desc;

取后10条数据

select * from tb12 order by id desc limit 10;

分组

select count(id),max(id),part_id from userinfo5 group by part_id;

聚合函数有下面几个:

count

max

min

sum

avg

如果对于聚合函数结果进行二次筛选时?必须使用having

select count(id),part_id from userinfo5 group by part_id having count(id) > 1;

select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;

自增值设置

表自增值的设置

alter table t1 auto_increment=20;

-- 这个就表示从开始20开始算,用上面的show create table t1\G 就可以看到当前的值是多少。

基于会话级别

-- 查看当前的会话值

show session variables like 'auto_incre%'

-- 设置会话步长

set session auto_increment_increment=2;

-- 设置起始值

set session auto_increment_offset=10;

基于全局设置

-- 查看全局的设置值

show global variables like 'auto_inc%';

-- 设置全局步长值

set global auto_increment_increment=3;

-- 设置起始值

set global auto_increment_offset=11;

sql server 是在创建表的时候就可以自己设置,灵活度很高REATE TABLE t5 (nid int(11) NOT NULL AUTO_INCREMENT,pid int(11) NOT NULL,num int(11) DEFAULT NULL,PRIMARY KEY (nid,pid)) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET=utf8

CREATE TABLE t6 (

nid int(11) NOT NULL AUTO_INCREMENT,pid int(11) NOT NULL,num int(11) DEFAULT NULL,PRIMARY KEY (nid,pid)) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=20 DEFAULT CHARSET=utf8

唯一索引

create table t1(

id int,

num int,

xx int,

unique qu1 (num ,xx) -- 意思就是这两列在一行上面数据不能相同,例如都是1,1,就不行

);

唯一索引:约束不能重复(可以为空)主键索引:约束不能重复(不可以为空)他们的特点都是加速查询

外键一对一

create table userinfo1(

id int auto_increment primary key,

name char(10),

gender char(10),

email varchar(64)

)engine=innodb default charset=utf8;

create table admin(

id int not null auto_increment primary key,

username varchar(64) not null,

password VARCHAR(64) not null,

user_id int not null,

unique uq_u1 (user_id),

CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)

)engine=innodb default charset=utf8;

外键多对多

示例1:

用户表

相亲表

示例2:

用户表

主机表

用户主机关系表

===》多对多

create table userinfo2(

id int auto_increment primary key,

name char(10),

gender char(10),

email varchar(64)

)engine=innodb default charset=utf8;

create table host(

id int auto_increment primary key,

hostname char(64)

)engine=innodb default charset=utf8;

create table user2host(

id int auto_increment primary key,

userid int not null,

hostid int not null,

unique uq_user_host (userid,hostid),

CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),

CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)

)engine=innodb default charset=utf8;

连表操作

select * from userinfo5,department5

select * from userinfo5,department5 where userinfo5.part_id = department5.id

select * from userinfo5 left join department5 on userinfo5.part_id = department5.id

select * from department5 left join userinfo5 on userinfo5.part_id = department5.id

# userinfo5左边全部显示

# select * from userinfo5 right join department5 on userinfo5.part_id = department5.id

# department5右边全部显示

select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id

将出现null时一行隐藏

select * from

department5

left join userinfo5 on userinfo5.part_id = department5.id

left join userinfo6 on userinfo5.part_id = department5.id

select

score.sid,

student.sid

from

score

left join student on score.student_id = student.sid

left join course on score.course_id = course.cid

left join class on student.class_id = class.cid

left join teacher on course.teacher_id=teacher.ti

select count(id) from userinfo5;

mysql语句engin_MySQL必会的SQL语句相关推荐

  1. MySQL查询本年的数据的sql语句

    MySQL查询本年的数据的sql语句: SELECT * FROM 表名 WHERE YEAR(时间字段)=YEAR(NOW());

  2. 解析MySQL基础架构及一条SQL语句的执行流程和流转

    前言 本篇文章分析SQL语句在MySQL中的执行流程,包括SQL的查询在MySQL内部会怎么流转,SQL语句的更新是怎么完成的.在分析之前我们一起看看MySQL的基础架构,知道了 MySQL由那些组件 ...

  3. 如何查询mysql中执行效率低的sql语句

    一些小技巧 1. 如何查出效率低的语句? 在MySQL下,在启动参数中设置 --log-slow-queries=[文件名],就可以在指定的日志文件中记录执行时间超过long_query_time(缺 ...

  4. Mysql中文乱码以及导出为sql语句和Excel问题解决

    Mysql中文乱码以及导出为sql语句和Excel问题解决 参考文章: (1)Mysql中文乱码以及导出为sql语句和Excel问题解决 (2)https://www.cnblogs.com/zhao ...

  5. mysql 硬解析 软解析_ORACLE sql语句的硬解析与软解析(收藏)

    我们都知道在Oracle中每条SQL语句在执行之前都需要经过解析,这里面又分为软解析和硬解析.那么这两种解析有何不同之处呢?它们又分别是如何进行解析呢?Oracle内部解析的步骤又是如何进行的呢?下面 ...

  6. 将mysql语句转换为sql_数据库-转换sql语句

    文章描述:主要说明转换成SQL语句的过程.----必要信息(数据库名,表名,条件)转换成SQL语句 一些界面上数据增删改查的操作往往只需要输入一数据库名,表名,加条件就可以进行数据查询了,在这背后是怎 ...

  7. linux系统sql语句报错_linux之SQL语句简明教程---CREATE VIEW

    视观表 (View) 可以被当作是虚拟表格.它跟表格的不同是,表格中有实际储存资料,而视观表是建立在表格之上的一个架构,它本身并不实际储存资料. 建立一个视观表的语法如下: CREATE VIEW & ...

  8. 简易HQL语句转换为可执行的SQL语句

    主要利用Hibernate中封装的一个转换器实现类:QueryTranslatorImpl, hql语句中无参数时,用下面的方法: /** * 将hql语句转换为sql语句,无参数 * * @para ...

  9. oracle字段换名语句,修改表名的sql语句

    sql语句 能不能修改表名 可以. SQL自带了一系列的系统存储过程.其中sp_rename就具有修改表名和列名的功能.对于sp_rename是这样定义的:更改当前数据库中用户创建对象(如表.列或用户 ...

最新文章

  1. viewer.js插件的应用
  2. JavaScript 开发的40个经典技巧
  3. 二分查找非递归方式实现
  4. 程序员在群询问破解软件
  5. Google Guava,牛逼的脚手架
  6. 搭建迁移训练Slim框架环境
  7. 翻译: TensorFlow 2.0 中的符号和命令式 API 是什么?
  8. 怎样下载最新的SCI/SSCI/EI目录期刊列表?
  9. IDEA自动生成serialVersionUID的设置
  10. 计算机术语CPI是什么意思,鼠标上的cpi什么意思?鼠标知识科普
  11. 精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过
  12. 单表置换加密matlab,单表置换密码
  13. 知云文献翻译打不开_【小虎聊干货】八大翻译软件大揭秘,告诉你科研翻译的正确打开方式...
  14. Can not set java.util.Date field *** to java.time.LocalDateTime解决办法
  15. 程序员工资一般多少钱一个月?【推荐】
  16. CXF报错及解决方案
  17. Android 自定义View 实例2_Clipping Canvas
  18. 使用yarn add安装依赖时报错‘error An unexpected error occurred: “EINVAL: invalid argument, unlink ’
  19. python3 模拟 ajax post请求
  20. 【Pandas数据处理100例】(三十一):Pandas读取Excel(xlsx)表格文件

热门文章

  1. NodeJs取参的四种方法
  2. [ES6] 细化ES6之 -- 字符串的扩展
  3. Scrapy_CSS选择器
  4. MySQL_存储引擎
  5. javascript表单处理相关的知识总结(一)
  6. LeetCode--5.最长回文子串(滑动窗口)
  7. 临界区,互斥量,信号量,事件的区别
  8. Android学习笔记之(一)开发环境搭建
  9. php版谁是卧底,机器人陪你玩“谁是卧底quot;游戏
  10. mariadb mysql 配置文件_Mariadb配置文件优化参数(仅供参考)