CREATE SCHEMA <模式名> AUTHORIZATION <用户名>

定义模式实际上定义了一个命名空间,在这个空间可以进一步定义该模式包含的数据库对象,例如基本表,视图,索引

DROP SCHEMA <模式名>  <CASCADE|RESTRICT>

选择了CASCADE 表示删除模式的同时把该模式下的数据库对象全部一起删除;

选择了RESTRICT表示如果在该模式下定义了数据库对象,则拒绝该删除语句的执行;

Mysql,sql语句赏析

  1. DROP TABLE IF EXISTS dl_user;
  2.  
  3. CREATE TABLE dl_user(
  4. id int(12) NOT NULL auto_increment,
  5. email varchar(50) NOT NULL,
  6. nick_name varchar(30) default NULL,
  7. password varchar(50) NOT NULL,
  8. user_integral int(12) not null default '',
  9. is_email_verify char(3),
  10. email_verify_code varchar(50) default NULL,
  11. last_login_time bigint default NULL,
  12. last_login_ip varchar(15),
  13. PRIMARY KEY (id),
  14. UNIQUE KEY email(email)
  15. )ENGINE=InnoDB DEFAULT CHARSET=UTF8;
  16.  
  17. insert into dl_user(email,nick_name,password,
  18. user_integral,is_email_verify,email_verify_code,
  19. last_login_time,last_login_ip)
  20. values("jdbc@126.com","jdbc","1234",10,"Y","ABSDFWE",345123421345,"192.168.3.1");
  21.  
  22. select * from dl_user;
  23. insert into dl_user(email,nick_name,password,user_integral,is_email_verify,email_verify_code,last_login_time,last_login_ip)
  24. values("jdbc22@126.com","jdbc","1234",10,"Y","ABSDFWE",345123421345,"192.168.3.1")

注意:

1

当我们要想数据库中插入数据时,

行列一定要对应;

数字不必加单引号,字符串必须加单引号

  1. insert into user(name,num,password) values('sufeng',7,'')

2

更新操作(包括删除),在选定表,选定行列的基础上再去修改,否则会酿成错误,

  1. update dl_user set email= '1814545@qq.com' where id=7

3 查询操作

列-》变量

where是判断条件

常用的查询语句

  1. # 查询主键为32的商品
  2. select * from goods where good_id=32
  1. # 选择不属于第三栏目的商品
  2. select good_id,cat_id,good_name from goods where cat_id != 3
  1. # 不高于三千元的商品
  2. select good_id,good_name form goods where shop_price<=3000
  1. # 取出第七栏或者第十栏的商品
  2. select good_id,good_name,cat_id from goods where cat_id=7 or cat_id=10
    select good_id,good_name,cat_id from goods where cat_id in <7,11>
  1. #查询价格在100到500之间的所有商品
  2. select good_id,good_name.shop_price from goods where shop_price>=100 and shop_price <= 500;
    select good_id,good_name,shop_price from goods where shop_price between 100 and 500;
  1. #取出不属于第三栏目且不属于第四栏目的商品
  2. select good_id,good_name,cat_id from goods where cat_id not in <4,5>;
  1. #取出第三个栏目下边价格在1000到3000,并且点击率大于5的系列商品
  2. select good_id,good_name,cat_id,shop_price,click_cout from where cat_id=3 and (shop_price<1000 or shop_price>3000) and click_count>5;
  1. # 模糊查询 %通配任意字符
  2. select good_id ,cat_id,good_name from goods where goods_name like 'windows%';
  1. #模糊查询 "_"通配一个字符
  2. select good_id,cat_id,good_name,from goods where goods_name like 'window__'
  1. # 将10到20内数字记为10,将20到30内数字记为20
  2. update userinfo set age=floor(age/10)*10;
  1. # 替换字符串
  2. select goods_id,concat('htc',substring(good_name,4)) from goods where good_name like '诺基亚%';
  1. # 统计函数 平均值函数
  2. select avg(good_price) from goods;
  1. # 统计函数 统计个数
  2. select count(*) from goods;
  1. # 按照商品类别分组进行统计
  2. select cat_id,avg(good_price) from goods group by cat_id;
  1. # where是对数据本身的判断,having 是对结果集的判断,两者虽然都能起筛选的作用,但有着本质的区别
  2. select good_id,(mark_price-show_price) as pro from goods where 1 having pro >200;
  1. #....
  2. select name,sum(score<60) as gk,avg(score) from grades group by name having gk>=2;
  1.  
  1. #....order by排序 asc(默认)升序 desc降序 limit 开始位置 条目数
  1. select good_id,good_name.cat_id,show_price from goods order by cat_id asc,show_price desc limit 2,3;
  1. #5种 子句 顺序 where , group by, having ,order by, limit
  1. # where 型子查询
  2. select good_id,good_name,show price,cat_id from goods where good_id=(select max(good_id) from goods);
  1. # where 型子查询
  2. select good_id,good_name,show price,cat_id from goods where good_id in (select max(good_id) from goods group by cat_id);
  1. # from型 子查询
  2. select * from (select good_id,good_name,cat_id,show_price from goods order by cat_id asc,good_id_desc) group by cat_id;
  1. #exist 子查询
  2. select * from category where exists(select * from goods where good.cat_id=category.cat_id);
  1. # 内连接查询
    select xx from
  2. table1 inner join table2 on table1.xx =table2.xx;
  1. #左连接 查询 左表为主,右表为辅
  2. select xx from
  3. table1 left join table2 on table1.xx =table2.xx;
  1. # 比赛详情示例1
    select m.*,t1.tname as hteam,t2.tname as gteam
  2. from
  3. m inner join t as t1 on m.hid=t1.tid inner join as t2 on m.git =t2.tid ;
  1. # 比赛详情示例2
  2. select mid,t1.tname as hteam,t2.tname as gteam
  3. from
  4. m inner join t as t1 on m.hid=t1.tid inner join t as t2 on m.gid=t2.tid
  5. where matme between '2006-06-01' and '2006-07-01';
  1. # union操作
    select uid,name from user
  2. union
  3. select id,name from tmp;

union的语句必须满足一个条件,各语句取出的列数相同,列名称未必要一致,列名称会使用第一条sql语句的列。

  1. # union 操作
  2. select id,sum(num) from
  3. (select * from a
  4. union all
  5. select * from b) as tmp
  6. group by id;

使用union时 ,完全相等的行将会被合并;因此,这里直接采取union all

create  table 表名(

列1    列类型   【列属性】

列2   列类型    【列属性】

);

engine=存储引擎

charset=字符集

整型列

bigint         int           mediumint       smallint       tinyint

  1. # 增加无符号列
  2. alert table t2 add unum tinyint unsigned;
  1. #0填充,固定长度4
  2. alert table t2 add sn tinyint(4) zerofill;
  1. # 查看表结构
  2. desc t2;

浮点型

float/double 有精度损失

(M,D) M时总位数,D是精确度

decimal定点型 更精确

字符型

char  定常

varchar 变长

text

blog

enum

  1. #enum操作
  2. create table t7(
  3. gender enum('男','女')
  4. );

时间日期类型

  1. #创建年月日时间表
  2. create table t8(
  3. ya year,
  4. dt date,
  5. tm time,
  6. dttm datetime);
  7.  
  8. #创建年
  9. insert into t8 (ya) values(1996);
  10.  
  11. #创建日期
  12. insert into t8(dt) values('1990-12-23');
  13.  
  14. #创建时间
  15. insert into t8 (tm) values('18:23:45');
  16.  
  17. #详细时间
  18. insert into t8 (dttm) values('1996-01-07 23:34:56');

timestamp时间戳类型

定义该属性 该列自动填充系统时间

  1. #建表 不允许空 带默认值
  2. create table t10(
  3. id int not null default 0,
  4. name char(10) not null default ' '
  5. );
  1. #声明表的主键
  2. create table t11(
  3. id int primary key,
  4. name char(2)
  5. );
  6.  
  7. create table t12(
  8. id int,
  9. name char(2),
  10. primary key (id)
  11. );

primary key此列不重复 可以做到区分元组

数值型primary key 一般与auto_increament联用

建表时注意定常与变长,常用与不常用的结合,时刻考虑查询速度及空间浪费。

改表名字

  1. rename table regist3 to reg3;

增加列

  1. alter table reg3 add height tinyint unsigned not null default 0;

删除列

  1. alter table reg3 drop column height;

在指定列后添加

  1. alter table reg3 add height tinyint unsigned after weight;

修改列

按照商品类别分组 计算每种类别售价均值

  1. create view v3 as select cat_id,avg(show_price) from goods group by cat_id;

视图的引入

视图又被叫做虚拟表,时sql语句的查询结果,

保证了数据表的安全,权限的控制(体现在对部分列的开放和关闭,),以及简化可sql语句

  1. create view v3 as select cat_id,avg(shop_price) as pj from goods group by cat_id;
  2. select * from v3 order by pj limit 0,3;

视图的更新 删除,分情况,

如果视图的每一行是与物理表一一对应的,则可以,

view的行是由物理表多行计算得到的结果,则不可以。

视图的algorthm

对于简单查询形成的view,再对view查询,如where,order 等等,可以把建视图的语句和查视图的语句

合并成差五里的语句,这种视图的算法叫merge算法;

对于复杂查询生成的视图,结果集形成临时表temp 表,再对临时表进行操作;

  1. create algorithm=merge view v7 as select goods_id,goods_name from goods;

表管理语句

  1. #选择数据库
  2. use test;
  3.  
  4. #显示表
  5. show tables;
  6. (#视图也会被显示)
  7.  
  8. #显示表结构
  9. desc t12;
  10.  
  11. #删除表
  12. drop table tmp;
  13. #删除视图
  14. drop view vuser;
  15.  
  16. #查看表详细信息
  17. show table status \G
  18. #(\G竖着显示)
  19. show table status where name='goods' \G;
  20.  
  21. #改表名
  22. rename table oldName to Newname;
  23.  
  24. #清空表数据
  25. truncate t12;

frm存储表结构

myd数据信息

myi索引

存储引擎 Myisam InnoDB Memory

客户端提交的字符集  character_set_client = gbk;

客户端返回的字符集 character_set_results = gbk   (/utf8);

服务器端的字符集 character_set_connection = gbk;

简写 为 set names gbk;

校对集 排序规则

索引是数据的目录,能快速定位数据的位置,

提高了查询速度,增加了增删改的麻烦,

一般在查询频率较高,重复度较低的列上加;

primary key();

key key1( key11)

unique key key2(key22)

可以设定索引的长度,多列索引以及索引修饰

  1. create table t18(
  2. id int,
  3. name char(10),
  4. email char(20),
  5. primary key(id),
  6. key name(name),
  7. unique key email(email(10))
    key xx(name,email)
  8. );
  1. show index from t19 /G;
  1. explain select * from t19 where xing='朱' and ming='院长' \G
  1. #删除索引
  2. alter table t20 drop index m;
  1. create table t16(
  2. email char(30);
  3. )
  4.  
  5. insert into t16 values('abc@163.c0m','121313@163.com');
  6.  
  7. select left (email,3) from t16;
  8.  
  9. select position ('@' in email ) from t16;
  10.  
  11. select left (email,position('@' in email)-1) from t16;

时间日期

  1. select date_formate(now(),'%Y%m');

事务

  1. update account set money=money+500;
  1. start transaction;
  2.  
  3. commit;
  4.  
  5. rollback;

模糊查询   % 一个或多个字符

  1. select * from studentinfo where sname like '张%'

any

  1. select sname as 补考学生
  2. from studentinfo
  3. where sno=any(select sno from elective where score<60);

in

  1. select sname as 考试不及格的学生
  2. from studentinfo
  3. where sno in (select sno from elective where score < 60) ;

not exist

  1. select tname ,tpro
  2. from teacher
  3. where not exists (select * from teacher
  4. where tpro='教授');

存储过程

  1. #结束符定义
  2. delimter //
  3.  
  4. #创建存储过程
  5. create procedure p_jiaoshi()
  6.  
  7. begin
  8.  
  9. select * from teacher where tedu='硕士研究生'
  10.  
  11. end
  12.  
  13. //
  14.  
  15. # 调用存储过程
  16. call p_jiaoshi();

存储过程与存储函数都哦是由sql语句和过程式所组成的代码片段,并且可以被应用程序和其他的sql语句调用,他们之间的区别在于

(1)存储函数不能有输出参数,因为存储函数本身就是输出参数;而存储过程可以拥有输出参数。

(2)可以直接对存储函数进行调用,而不需要使用call与语句:对存储过程的调用,需要使用call语句。

(3) 存储函数中必须包含一条return语句,而这条特殊的sql语句不允许包含于存储过程中。

  1. # 创建过程函数
  2. create function addTwoNumber(x small unsigned, y smallint unsigned)
  3. return smallint unsigned
  4. begin
  5. declare a,b smallint usigned default 10:
  6. set a=x, b=y;
  7. return a+b;
  8. end
  9.  
  10. # 函数调用
  11. set @num1=10;
  12. set @num2=20;
  13. set result = addTwoNumber(@num1,@num2);
  14. select @ result;

事务,原子性, 一致性, 隔离性, 持久性

  1. # 开启事务
  2. start transation;
  3. insert into teacher values('t006''张君瑞','男');
  4. insert into teacher values('t007','赵楠','女');
  5.  
  6. # 在事务处理过程中,为了使执行的修改操作保存在数据库中,事务处理结束必须由用户提交
  7. # 确定,使用commit语句
  8. commit;
  9. select * from teacher;

在进行事务处理过程中,如果事务尚未提交时发现某些操作不合理,可以通过事务的回滚来取消当前事务,把数据库恢复到事务处理之前的状态。

  1. rollback;

Sql语句内功心法的更多相关文章

  1. mysql学习之 sql语句的技巧及优化

    一.sql中使用正则表达式 select name,email from user where email Regexp "@163[.,]com$"; sql语句中使用Regex ...

  2. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  3. LINQ to SQL语句(7)之Exists/In/Any/All/Contains

    适用场景:用于判断集合中元素,进一步缩小范围. Any 说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).有2种形式,分别为简单形式 ...

  4. Oracle ------ SQLDeveloper中SQL语句格式化快捷键

    Oracle SQL Developer中SQL语句格式化快捷键: 每次sql复制到SQL Developer面板的时候,格式老不对,而且看起来很不舒服,所有的sql都挤在一行完成. 这时我们可以全选 ...

  5. SQL语句优化

    (1)      选择最有效率的表名顺序 ( 只在基于规则的优化器中有效 ) : ORACLE 的解析器按照从右到左的顺序处理 FROM 子句中的表名, FROM 子句中写在最后的表 ( 基础表dri ...

  6. LinqToDB 源码分析——生成与执行SQL语句

    生成SQL语句的功能可以算是LinqToDB框架的最后一步.从上一章中我们可以知道处理完表达式树之后,相关生成SQL信息会被保存在一个叫SelectQuery类的实例.有了这个实例我们就可以生成对应的 ...

  7. 年终巨献 史上最全 ——LINQ to SQL语句

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  8. LINQ to SQL语句(19)之ADO.NET与LINQ to SQL

    它基于由 ADO.NET 提供程序模型提供的服务.因此,我们可以将 LINQ to SQL 代码与现有的 ADO.Net 应用程序混合在一起,将当前 ADO.NET 解决方案迁移到 LINQ to S ...

  9. LINQ to SQL语句(17)之对象加载

    对象加载 延迟加载 在查询某对象时,实际上你只查询该对象.不会同时自动获取这个对象.这就是延迟加载. 例如,您可能需要查看客户数据和订单数据.你最初不一定需要检索与每个客户有关的所有订单数据.其优点是 ...

随机推荐

  1. C# 杀掉Windows中所有Excel进程

    Process[] procs = Process.GetProcessesByName("excel"); foreach (Process pro in procs) { pr ...

  2. (TIP 2018)Technology details of FFDNet

    前言 论文地址:见researchgate, 方法继续更新. 解决的问题: 1.discriminative learning methods 用于denoising 任务学习到的是一个对于每种 噪声 ...

  3. AI 基础

    what AI ? 人工智能(Artificial Intelligence),英文缩写为AI. 人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的 ...

  4. 获取JS数组中所有重复元素

    //获取数组内所有重复元素,并以数组返回 //例:入参数组['1','2','4','7','1','2','2'] 返回数组:['1','2'] function GetRepeatFwxmmc(a ...

  5. js实现打印

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. Shell 常见理论问答

    (1)shell脚本中,怎么可以把某一行注释掉? 答:“#”. (2)如何执行一个shell脚本呢? 答:“sh x.sh”,“加执行./x.sh”,“bash x.sh”. (3)为了方便管理我们约 ...

  7. linux shell中如何删除指定后缀名的文件?

    答: find . -name '*.txt' -delete 这条命令含义如下: 从当前目录开始查找以txt为后缀名的文件并删除掉

  8. .Net中json序列化与反序列化

    NuGet中下载Newtonsoft.Json插件. public class JsonHelper<T>{ public static string ModelToJsonString( ...

  9. Executors创建线程池的几种方式以及使用

    Java通过Executors提供四种线程池,分别为:   1.newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.   ...

  10. mysql57 centos7 使用

    ####### yum repository install #######mysql yum repo http://repo.mysql.com/wget http://repo.mysql.co ...