蜡笔筱新

博客园 首页 新随笔 联系 订阅 管理

基本操作:

show databases; 显示所有已经存在的数据库

create database test; 创建名字为test的数据库

drop database test; 删除名字为test的数据库

use test;使用名字为test的数据库

show tables; 显示这个数据库中的所有数据表

create table player(id int not null auto_increment primary key,name varchar(16) not null,level int not null default “0”,fightingValue int not null default “5”);创建一个名字为player的表,表里一共三列属性id,name,level,fightingValue;

describe player; 显示player这张表的属性

select * from player 遍历这张表的所有数据

insert into player (id,name,level,fightingValue)values (1,”法师”,1,10);增加一行数据

select name from player where id = 2; (查询player这张表中id 为 2 这一行的名字信息)

select * from player order by level asc;对player这张表按等级排序(正序)

select * from player order by fightingValue desc;对player这张表按战斗力排序(倒序)

delete from player where id = 1;删除player这张表中的id = 1的一行数据

update player set fightingValue = 50 where id = 1;更新player这张表中id为1的玩家的战斗力为50;

alter table player add column vipLevel int not null default “0”;增加player表中的一个vipLevel字段,类型为int;

alter table player drop vipLevel;删除player表中的vipLevel字段

alter table player rename as player1;将player这张表的名字改为player1;

mySql支持的常用数据类型:

tinyint,int,varchar,char,float,double

 

1、说明:创建数据库
  CREATE DATABASE yourDatabase;
2、说明:删除数据库
  drop database yourDatabase;
3、说明:创建新表
  create table yourTable(col1 type1 [not null] [primary key],col2 type2 [not null],..);

 

4、根据已有的表创建新表:
  A:create table yourTable_new like yourTable _old ;
  B:create table yourTab_new as select col1,col2… from yourTab_old definition only ;
5、说明:删除新表
  drop table yourTable
6、说明:增加一个列
  alter table yourTable add column col type

7、添加主键: Alter table yourTable add primary key(col)
删除主键:Alter table yourTable drop primary key(col)


8、创建索引:create [unique] index indexname on yourTable(col….)
删除索引:drop index indexname
注:索引是不可更改的,想更改必须删除重新建。


9、创建视图:create view viewname as select statement
删除视图:drop view viewname


10、说明:几个简单的基本的sql语句
  选择:select * from yourTable where范围
  插入:insert into yourTable(field1,field2) values(value1,value2);
  删除:delete from yourTable where 范围
  更新:update yourTable set field1=value1 where 范围
  查找:select * from yourTable where field1 like ‘%value1%’;
  排序:select * from table1 order by field1,field2 [desc]
  总数:select count as totalcount from yourTable;
  求和:select sum(field1) as sumvalue from yourTable;
  平均:select avg(field1) as avgvalue from yourTable;
  最大:select max(field1) as maxvalue from yourTable;
  最小:select min(field1) as minvalue from yourTable;

 

11、分组:Group by:
一张表,一旦分组 完成后,查询后只能得到组相关的信息。
组相关的信息:(统计信息) count,sum,max,min,avg 分组的标准)
在selecte统计函数中的字段,不能和普通的字段放在一起;

 

二、提升篇

 

1、说明:子查询(表名1:a 表名2:b)
  select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

 

2、说明:显示文章、提交人和最后回复时间
  select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

 

3、说明:外连接查询(表名1:a 表名2:b)
  select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

 

4、说明:在线视图查询(表名1:a )
  select * from (SELECT a,b,c FROM a) T where t.a > 1;

 

5、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
  select * from table1 where time between time1 and time2
  select a,b,c, from table1 where a not between 数值1 and 数值2

 

6、说明:in 的使用方法
s  elect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

 

7、说明:两张关联表,删除主表中已经在副表中没有的信息
  delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

 

8、说明:四表联查问题:
  select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

 

9、说明:日程安排提前五分钟提醒
  select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

 

10、说明:前10条记录
  select * from table1 where 范围 limit 10;

 

11、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
  select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

 

12、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
  (select a from tableA ) except (select a from tableB) except (select a from tableC)

 

13、说明:随机取出10条数据

  1.通过MYSQL内置的函数来操作,具体SQL代码如下:

  SELECT * FROM tablename ORDER BY RAND() LIMIT 10

  2.不要将大量的工作给数据库去做,这样会导致数据库在某一集中并发时间内锁死并阻塞。建议通过代码随机生成一下1-X(总行数)之间的数字,然后将这10个随机数字作为查询条件,具体语句如:

  SELECT * FROM tablename where ID in (2,8,4,11,12,9,3,1,33)

  可能你还要进行重复排除,并且需要在程序中将10个值串联并连接进入SQL语句中。

 

14、说明:随机选择记录
  SELECT * FROM tablename ORDER BY RAND() LIMIT 1;

 

15、说明:删除重复记录
  1) delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
  2) select distinct * into temp from tablename delete from tablename insert into tablename select * from temp
  评价: 这种操作牵连大量的数据的移动,这种做法不适合大容量但数据操作

# 复习
一、创建库与表

  1、创建库

    create datanase 数据库名称;

  2、 创建表 

create TABLE areas(
id int primary key auto_increment not NULL,
title varchar(20),
pid int,
foreign key(pid) REFERENCES areas(id)
);

二、查询

  1、条件查询

    select * from 表名 where 条件

  2、聚合

    sum、avg、count、min、max

  3、分组

    select * from 表名 group by 分组字段 having 二次筛选

  4、排序

    select * from 表名 order by desc(asc)

  5、分页  

    select * from 表名 limit start,count

三、高级

  1、关系

    一对一、一对多、多对多

    添加外键约束:

      先创建一个表:create table scores(id int primary key auto_increment,stuid int,subid int score deimal(5,2));

      创建外键约束:create table source(id int primary key auto_increment,stuid int,subid int,score dicimal(5,2) foreign key(stuid) references students(id), foreign key(subid) references students(id));

  2、连接查询

    内联、左右连接

    内联:select * from students inner join scores on students.id=scoues.id;

    左连接:select * from students left join scores on students.id=scoues.id;

    右连接:select * from students left join scores on students.id=scoues.id;

  3、自连接

    以自身做为物理表,逻辑分成两个表进行查询

    select s.id as sid,s.title as stitle, x.pid as xpid,x.title as xtitle from areas as s inner JOIN areas as x on s.id=x.pid where s.title ='湖南省'

  4、子查询(嵌套查询)

     select sname, (select sco.score from scores sco inner join subjects sub on sco.subid=sub.id where sub.stitle='语文' and stuid=stu.id) as 语文, (select sco.score from scores sco inner join subjects sub on sco.subid=sub.id where     sub.stitle='数学' and stuid=stu.id) as 数学, (select sco.score from scores sco inner join subjects sub on sco.subid=sub.id where sub.stitle='英语' and stuid=stu.id) as 英语 from students stu;
  5、视图
    封装SQL语句
    创建视图 create view 视图名称 as sql语句
    使用视图
    select * from 视图名称
  6、事务(临时表操作):
    在完成一连串的事情之后才算成功,如果中途有其他语句失败,则其他所有的语句都回滚,都会失败
      例子:
        begin(开始事物)
          update students set id = 2 where name='lixiao';
          delete from students where id =2;
        commit(提交) 执行完两条语句成功后提交
        rollback(回滚)两条语句作废,临时表修改的数据回滚
    开启事务:begin
    提交事物:commit
    回滚:rollback
  7、索引(增加查询速度)
    创建索引:create index indexname on 表名(需要创建索引的字段名称)
    删除索引:drpo index indexname on 表名
 

    

    

        

posted on 2021-10-16 17:32  蜡笔筱新  阅读(45)  评论(0编辑  收藏  举报