Mysql 基本语句练习

一、怎样查看数据库信息?
desc 数据库名;

 

二、怎样查看数据表信息?
desc 表名;          //查看表的属性和属性值

或者用select语句: //查看表的行记录信息
select * from 表名 (where nub='100001');


三、将数据写入数据库要经过什么步骤?
1. 创建表:

    create datable 数据库名;

2. 创建用户:

   grant select,create,drop on stud.* to 用户名@localhost identitied by "密码";      //(注意双引号)

3.创建(多个)空表,添加表的属性和主键 :

   create table 表名(...主键..);

4.增加主键:

   Alter table 表名 add primary key (属性名1,属性名2);

5.属性字段操作(都是用alter 开头):

   5.1增加一个列(即属性):

        Alter table 表名 add 属性名 varchar(40) not null;
   5.2 修改列字段名称:(例如修改列属性名称和属性: )
   Alter table 表名 change name 新名字 varchar(20)(必须加上格式限制) not null;
 5.3 修改列字段属性值:(例如改成varchar(30):)
   Alter table 表名 modify 属性名 属性新值(例如varchar(30)) not null;
   5.4.删除列字段:
   Alter table 表名 drop column 属性列名;

6.记录操作(insert ,update ,delete):

   6.1. 往空表里插入行数据(即记录):
   insert into 表名 values('','','',..);           //(单引号) 。注意增加列是alter,增加行是insert
   6.2.修改(更新)行信息:
     update 用户名 set 行某个属性="该行修改后的属性内容" where ...     //注意单引号
   例如:update std_info set name='张三丰' where nub='100001';      //将原记录中名字“张三”改成“张三丰”
 6.3删除表某一行信息(即记录):

        delete from 表名 where ..='...'; //注意delete 是直接接着from

7.删除某个表:

  drop table 表名;


四、查询有哪几种:

1.有序order查询:

  select * from 表名 order by 属性名 desc; //默认升序 。有order by就没有where
2.分组group查询:

 select .. from 表名 group by 属性名(例如:class,sex); //有group by就没有where

3.查询表中行总数(记录总数):

  select count(*) from 表名;

3.多个表联合查询:
select avg(b.score) as '平均分'         //查询可以用as重用名
from std_info a,score_info b,curricula c    //表名可以直接重用名
where a.nub=b.nub and b.code=c.code and c.Curlum='英语' and a.class='9701';
4. 查看平均分:

select a.curlum as '课程名',avg(score) from curricula a,score_Info b where
a.code=b.code group by a.code;

注意:

 
1.四个聚合函数不能搭配order by。只能group by??

2.group by 有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group
by 后面。最常用的四个聚合函数:count,avg,min,max。


 

 

  

posted @ 2013-09-22 17:46  无脚的鸟  阅读(316)  评论(0编辑  收藏  举报