登陆mysql: mysql -hlocalhost -uroot -proot

创建数据库:create database dbname charset utf8

查看数据库:show databases;

选择数据库:use dbname;

删除数据库:drop database dbname;

创建数据表: create table tbname (

id int(10) primary key auto_increment,

name char(10) not null defaule '',

.......

)engine myisam/innodb charset utf8/gbk

设定环境编码的命令:set names gbk;

查看数据表:show tables;

查看建表语句:show create table tbname;

查看表结构:desc tbname;

修改表(增加列):alter table tbname add 列名称 列类型

修改表(修改列):alter table tbname change 旧列名 新列名 列类型

修改表(删除列):alter table tbname drop 列名称

索引

  1. 提高查询速度,但是降低了增删改的速度,所以使用索引时,要综合考虑

  2. 索引不是越多越好,一般我们在常出现于条件表达式中的列加索引

  3. 值越分散的列,索引的效果越好

索引类型

  1. primary key 主键索引

  2. index 普通索引

  3. unique index 唯一性索引

  4. fulltext index 全文索引

修改表(增加主键索引):alter table tbname add primary key(主键所在列名)

修改表(删除主键索引):alter table tbname drop primary key;

修改表(增加唯一索引):alter table tbname add unique index 索引名(列名);

修改表(增加全文索引):alter table tbname add fulltext index 索引名(列名);

修改表(删除普通索引):alter table tbname drop unique index 索引名;

清空数据表内容:truncate tbname;

删除表:drop table admin;

插入数据: insert into tbname (列1,列2,列...) values(val1,val2,val...) 指定值与列一一对应

insert into tbname values(val1,val2,val3...) 全部列都插入值

insert into tbname values(val1,val2,val3...),(val1,val2,val3...),(val1,val2,val3...) 插入多行数据

删除数据: delete from tbname where 列名=列值;

修改数据: update tbname set 列名1=新列值1,列名2=新列值2 where 条件;

查询数据: select 列1,列2,列3 from tbname;

数据类型: 整型 tinyint (0-255/-128-127) smallint (0-65536/-32768-32767) mediumint () int bigint 共5种类型

      unsigned 无符号(不能为负) zerofill (0 填充) M 填充后的宽度

      举例: tinyint unsigned; tinyint(6) zerofill;

数值型: 浮点型: float double

      格式: float(M,D) D 表示小数位数 unsinged zerofill;

字符型: char(M) 定长 varchar(M) 变长 text

      列             实存字符    实占空间

      char(M)       0<=i<=M           M

      varchar(M)    0<=i<=M           i+1

            year            YYYY 范围: 1901~2155. 可输入值2位和4位(如98,2012)

            date            YYYY-MM-DD 如: 2010-03-04

日期时间类型: time HH:MM:SS 如: 19:26:32

            datetime        YYYY-MM-DD HH:MM:SS 如: 2010-03-14 19:26:32

            timestamp       YYYY-MM-DD HH:MM:SS 特性:不用赋值,该列会为自己赋当前的具体时间

查询详解: (1) 条件查询 where a. select 列1,列2,列3.. from tbname where 列x=列x值;

                            b. 比较运算符 select 列1,列2,列3.. from tbname where 列x[=|!=|<|>|<=|>=]列x值;

                            c. like,not like ('%'匹配任意多个字符,'_'匹配任意单个字符)

                            d. in,not in,between and

                            e. is null,is not null

      (2) 分组        group by  配合5个聚合函数使用 max(最大),min(最小),sum(求和),avg(求平均),count(统计)

      (3) 筛选        having

      (4) 排序        order by

      (5) 限制        limit

连接查询:

左连接: 例句 select 列1,列2,列3... from tbnameA left join tbnameB on tbnameA.列1 = tbnameB.列2;

右连接: 例句 select 列1,列2,列3... from tbnameB right join tbnameA on tbnameA.列1 = tbnameB.列2;

左右连接都是以在左边的表的数据结构为准,沿着左表查询。

内连接是以两张表都有的共同部分数据为准,也就是左右连接的数据之交集。

子查询:

where 型子查询:内层sql的返回值在where后作为条件表达式的一部分

例句: select * from tableA where colA = (select colB from tableB where ...);

from 型子查询:内层sql查询结果,作为一张表,供外层的sql语句再次查询

例句:select * from (select * from ...) as tableName where ...

字符集

客服端sql编码 character_set_client

服务器转化后的sql编码 character_set_connection

服务器返回给客户端的结果集编码 character_set_results

快速把以上3个变量设为相同值: set names 字符集

存储引擎 engine:Myisam/Innodb

  1. Myisam 速度快 不支持事务 回滚

  2. Innodb 速度慢 支持事务 回滚

事务执行顺序:

a. 开启事务 start transaction

b. 运行sql

c. 提交,同时生效\回滚 commit\rollback

触发器: trigger

监视地点: 表

监视行为: 增 删 改

触发时间:a fter\before

触发事件: 增 删 改

创建触发器语法:

create trigger tgName

after/before insert/delete/update

on tableName

for each row

sql 触发语句

删除触发器: drop trigger tgName;

综合练习题:

  1. 连接数据库 (以本地机为例)

答: mysql -hlocalhost -uroot -proot

  1. 建立一个gbk编码的数据库

答: create database test charset utf8;

  1. 建立商品表和栏目表

答: #商品表

create table goods(

goods_id int primary key auto_increment,

goods_name varchar(20) not null default '',

cat_id int not null default 0,

brand_id int not null default 0,

goods_sn char(12) not null default '',

shop_price float(6,2) not null default 0.00,

goods_desc text

)engine MyISAM charset utf8;

栏目表

create table category(

cat_id int primary key auto_increment,

cate_name varchar(20) not null default '',

parent_id int not null default 0

)engine myisam charset utf8;

  1. 删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段

答: alter table goods drop goods_desc;

alter table goods drop brand_id;

alter table goods add click_count int not null default 0;
  1. 在 goods_name 列上加唯一性索引

答: alter table goods add unique index goods_name(goods_name);

  1. 在 shop_price 列上加普通索引

答: alter table goods add index shop_price(shop_price);

  1. 在 click_count 上增加普通索引,然后再删除

答: alter table goods add index click_count(click_count);

alter table goods drop index click_count;

基础查询练习一(where条件查询)

  1. 查询主键为32的商品

答: select goods_id,goods_name,shop_price from goods where goods_id=32;

  1. 查询不属于第三个栏目的所有商品

答: select goods_id,goods_name,shop_price from goods where cat_id!=3;

  1. 查询本店价格高于3000的商品

答: select goods_name,goods_id,shop_price from goods where shop_price>3000;

  1. 查询本店商品价格低于或等于100元的商品

答: select goods_id,goods_name,shop_price from goods where shop_price <=100;

  1. 取出第4个栏目或第11个栏目的商品(不允许用 or )

答: select goods_id,goods_name,shop_price from goods where cat_id in (4,11);

  1. 取出本店价格大于100小于500的商品(不允许用 and )

答: select goods_id,goods_name,shop_price from goods where shop_price between 100 and 500;

  1. 取出名字以”诺基亚“开头的商品

答: select goods_id,goods_name,shop_price from goods where goods_name like '诺基亚%';

  1. 取出名字为”诺基亚Nxxx“的商品

答: select goods_id,goods_name,shop_price from goods where goods_name like '诺基亚N___';

  1. 取出不在第3个栏目和不在第11个栏目的商品

答: select goods_id,goods_name,shop_price from goods where cat_id!=3 and cat_id!=11;

select goods_id,goods_name,shop_price from goods where cat_id not in (3,11);
  1. 取出名字不以”诺基亚“开头的商品

答: select goods_id,goods_name,shop_price from goods where goods_name not like '诺基亚%';

  1. 取出价格大于100且小于300,或者大于4000且小于5000的商品

答:select goods_id,goods_name,shop_price from goods where shop_price>100 and shop_price<300 or shop_price>4000 and shop_price<5000;

  1. 取出第3个栏目下面价格在1000到3000之间,并且点击量大于5,以”诺基亚“开头的系列商品

答: select goods_id,goods_name,shop_price from goods where cat_id=3 and shop_price>1000 and shop_price<3000 and click_count>5 and goods_name like '诺基亚%';

select goods_id,goods_name,shop_price from goods where cat_id=3 and shop_price between 1000 and 3000 and click_count>5 and goods_name like '诺基亚%';

  1. 取出第3个栏目下面价格小于1000或价格大于3000,并且点击量大于5的系列商品

答: select goods_id,goods_name,shop_price from goods where cat_id=3 and (shop_price<1000 or shop_price>3000) and click_count>5;

  1. 取出第1个栏目下面的商品(注意 1号栏目下面没有商品,但其子栏目2,3,4,5有商品)

答: select goods_id,goods_name,shop_price from goods where cat_id in (2,3,4,5);

基础查询练习二(group by 分组查询和聚合函数的使用)

  1. 查出最贵的商品

答: select goods_id,goods_name,shop_price from goods group by shop_price desc limit 1;

select max(shop_price) from goods;
  1. 查出最大(最新)的商品的编号

答: select max(goods_id) from goods;

select goods_id from goods group by goods_id desc limit 1;
  1. 查出最便宜商品的价格

答: select min(shop_price) from goods;

select goods_id,goods_name,shop_price from goods group by shop_price asc limit 1;
  1. 取出最小(最旧)的商品编号

答: select min(goods_id) fomr goods;

select goods_id,goods_name,shop_price from goods group by goods_id asc limit 1;
  1. 查出本店所有商品的库存量

答: select sum(goods_num) from goods;

  1. 查出本店所有商品的平均价格

答: select avg(shop_price) from goods;

  1. 查出本店共有多少种商品

答: select count(*) from goods;

基础查询练习三(having 与 group by 综合运用)

  1. 查出本店商品价格比市场价格的差价

答: select goods_id,goods_name,market_price-shop_price as g from goods;

  1. 查出每个商品所积压的货款

答: select goods_id,goods_name,shop_price*goods_num as g from goods;

  1. 查询本店积压的总货款

答: select sum(shop_price*goods_num) from goods;

  1. 查询出每个栏目下积压的总货款

答: select cat_id,sum(shop_price*goods_num) as g from goods group by cat_id;

  1. 查询比市场价省200元以上的商品及该商品所省的钱 (分别用 where 和 having 实现)

答: select goods_id,goods_name market_price-shop_price as g from goods having g>200;

select goods_id,goods_name market_price-shop_price from goods where market_price-shop_price>200;
  1. 查询积压货款超过2W的栏目,以及该栏目积压的货款

答: select cat_id,sum(shop_price*goods_num) as g from goods group by cat_id having g>20000;

基础查询练习四(order by 与 limit 查询)

  1. 按价格由高到低排序

答: select goods_id,goods_name,shop_price from goods order by shop_price desc;

  1. 按发布时间由早到晚排序

答: select goods_id,goods_name,shop_price,pub_time from goods order by pub_time asc;

  1. 按栏目由低到高排序,栏目内部按价格由高到低排序

答: select goods_id,goods_name,cat_id,shop_price from goods order by cat_id asc,shop_price desc;

  1. 取出价格最高的前三名商品

答: select goods_id,goods_name,shop_price from goods order by shop_price desc limit 3;

  1. 取出点击量前3到前5名的商品

答: select goods_id,goods_name,shop_price from goods order by click_count desc limit 2,3;

基础查询练习五(连接查询)

  1. 取出所有商品的商品名,栏目名,价格

答: select goods_name,cat_name,shop_price from goods left join category on goods.cat_id=category.cat_id;

  1. 取出第4个栏目下的商品的商品名,栏目名,价格

答: select goods_name,cat_name,shop_price from goods left join category on goods.cat_id=category.cat_id where cat_id=4;

  1. 取出第4个栏目下的商品的商品名,栏目名,品牌名

答: select goods_name,cat_name,brand_name from goods left join category on goods.cat_id=category.cat_id left join brand on goods.brand_id=brand.brand_id where cat_id=4;

基础查询练习六(UNION 查询)

  1. 把 goods 表和 category 表里各取出2列,并集成一个表

答: select goods_name,goods_id from goods limit 4 union select cat_id,cat_name from category limit 4;

基础查询练习七(子查询)

  1. 查询出最新一行商品

答: select goods_id,goods_name,shop_price from goods where goods_id=(select max(goods_id) from goods);

  1. 查询出编号为19的商品的栏目名称(用左连接和子查询分别实现)

答: select cat_name from category where cat_id=(select cat_id where from goods goods_id=19);

select cat_name from goods left join category on goods.cat_id=category.cat_id where goods_id=19;
  1. 用 where 型子查询把 goods 表中的每个栏目下最新的商品取出来

答: select goods_id,goods_name from goods where goods_id in (select max(goods_id) from goods group by cat_id);

  1. 用 from 型子查询把 goods 表中的每个栏目下面最新的商品取出来

答: select * from ()

(本学习笔记仅供个人学习使用)

MySQL相关sql语句的更多相关文章

  1. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  2. MySQL数据库SQL语句基本操作

    一.用户管理: 创建用户: create user '用户名'@'IP地址' identified by '密码'; 删除用户: drop user '用户名'@'IP地址'; 修改用户: renam ...

  3. mysql执行sql语句过程

    开发人员基本都知道,我们的数据存在数据库中(目前最多的是mysql和oracle,由于作者更擅长mysql,所以这里默认数据库为mysql),服务器通过sql语句将查询数据的请求传入到mysql数据库 ...

  4. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  5. MySQL数据库sql语句的一些简单优化

    1.查询条件的先后顺序 有多个查询条件时,要把效率高能更精确筛选记录的条件放在后边.因为MySQL解析sql语句是从后往前的(不知是否准确). 例: select a.*,b.* from UsrIn ...

  6. mysql下sql语句 update 字段=字段+字符串

    mysql下sql语句 update 字段=字段+字符串   mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...

  7. 金蝶K/3 报销相关SQL语句

    金蝶K/3 报销相关SQL语句 use AIS20180607113701 select fopenid,* from dbo.t_XunTong_User where Fname ='' go us ...

  8. 金蝶K/3 审批相关SQL语句

    金蝶K/3 审批相关SQL语句 --http://127.0.0.1/lightApp/todocheckTask.aspx?AccID=84&&FClasstypeID=1071&a ...

  9. 金蝶K/3 固定置产相关SQL语句

    金蝶K/3 固定置产相关SQL语句 select * from vw_fa_card --固定置产打印原始数据 select FAssetID,FAssetNumber,FAssetName,FGro ...

随机推荐

  1. JavaScript 学习笔记(基础学习)

    一:来自W3School工具的学习 1:document.getElementById(id) : 访问某个标签的元素,然后对它进行操作 .innerHTML 对其内容进行修改 2:document. ...

  2. 如何使用Visual Studio Code开发Django项目

    如何获得 Visual Studio Code 访问 http://code.visualstudio.com 下载并安装. 前提条件 安装Python 2.7 及 Python 3.5,Window ...

  3. APS技术中的多目标规划问题

    在进行APS(高级计划与排程)系统开发时,绝大多数情况下是需要考虑多目标的.但面对多目标问题进行规划求解时,我们往往极容易因处理方法不当,而影响输出结果,令结果与用户期望产生较大差别.事实上很多时候用 ...

  4. 【CentOS】PostgreSQL安装与设定

    本教程适合Centos6.7或者RedHat. PostgreSQL安装 1.Postgresql安装包确认 yum list postgresql* postgresql-server.x86_64 ...

  5. StringRedisTemplate常用操作

    stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向r ...

  6. c#委托之浅析

    前言: 这章我们将弄懂,委托是什么?有什么作用?在什么样的场景下可以启到什么作用? 委托适用的场景:当确定处理一个任务时,不确定其处理任务的方法时可使用,这样可以提高扩展性,调用符合条件的处理方法,避 ...

  7. rocketMQ(二 )Centos7 集群

    rocketMQ集群: 在运用中流程一般 是在程序中使用代码编辑生产者,将所需要的消息发送到rocketmq中,然后另一个程序编辑消费者从rocketmq里面获取消息.rocketmq集群 需要对na ...

  8. oracle函数操作

    感于总有些网友提出一些非常基础的问题,比如有没有实现某某功能的函数啊,某某函数是做什么用的啊,格式是什么等等,同时也感受到自己对oracle函数认识的不足,于是集中月余时间专注于oracle函数,小有 ...

  9. Scrapy实战篇(六)之爬取360图片数据和图片

    本篇文章我们以360图片为例,介绍scrapy框架的使用以及图片数据的下载. 目标网站:http://images.so.com/z?ch=photography 思路:分析目标网站为ajax加载方式 ...

  10. [UE4]计算箭头方向:正切、SetRelativeRotation、RotationFromXVector、Get MotionController Thumbstick X

    正切 正弦函数 sinθ=y/r 余弦函数 cosθ=x/r 正切函数 tanθ=y/x 余切函数 cotθ=x/y 正割函数 secθ=r/x 余割函数 cscθ=r/y   已知y和x,求角度θ: ...