登陆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. zabbix3.4使用外部邮件报警

    打开web页面,找到 管理--报警媒介类型 打开 Email 填写外部邮箱信息 点击更新,这样发件人和邮箱服务器信息定义完成. 添加收件人信息: 接下来创建动作,配置---动作---选触发器 ok 过 ...

  2. windows 时间服务器配置详解

    最近发现公司的服务器时间一直不准确,也无法和外部时间源进行同步.公司是域环境,按理说客户端PC应该自动和域控AD进行时间同步,但是PC机时间老是不能同步,不是慢了就是快了,就是和域控时间不一致.其它服 ...

  3. 【SQL触发器】类型 FOR 、AFTER、 Instead of

    1.AFTER(for)触发器 (操作后) after触发器是指在操作成功后,所采取的一些动作! 比如:下面是我创建好的一个after触发器 creat trigger [dbo].[T_Carego ...

  4. 安装owncloud出现:Error while trying to create admin user: An exception occurred while executing

    安装owncloud出现:Error while trying to create admin user: An exception occurred while executing 1.安装ownc ...

  5. Python脚本之Lrc歌词去时间轴转Txt文件,附带酷狗音乐APP关联已有krc歌词

    一.Lrc歌词去时间轴转Txt文件 环境:Python2.7.x, Mac(Windows需装cygwin环境,当然你也可以自己改代码,Python新手,勿喷) # -*- coding: UTF-8 ...

  6. 分类问题的几个评价指标(Precision、Recall、F1-Score、Micro-F1、Macro-F1

    轉自 https://blog.csdn.net/sinat_28576553/article/details/80258619 四个基本概念TP.True Positive   真阳性:预测为正,实 ...

  7. HTML5 新的 Input 类型

    Input 类型: color(拾色器) color 类型用在input字段主要用于选取颜色,如下所示: 支持浏览器 实例 从拾色器中选择一个颜色: 选择你喜欢的颜色: <input type= ...

  8. TypeScript: this bind 和 回调的正确用法

    TypeScript 中如果传递了 而且在回调函数中用了this 的话, 就要小心了, 这个this 不一定是指向当前类对象了,如果想确保指向的还是那个对象的话, 需要在传递那个方法的时候, 先调用b ...

  9. Linux 的文件软链接如何删除

    Linux 的文件软链接如何删除创建软链接即用 ln -s 原始文件或文件夹 目标文件或文件夹 举例:[root@recover test]# pwd/test[root@recover test]# ...

  10. jar包冲突排解方法(sbt)

    jar包依赖冲突,版本不兼容会导致各种各样的问题.这里推荐一款sbt插件用于查找项目中的jar包依赖关系,通过该插件可以轻松的看出某个jar包依赖哪些jar,以及某个jar被哪些jar所依赖.此外该插 ...