您的位置:首页 > 数据库 > MySQL

(学习笔记)MySQL基本操作语句

2016-10-27 12:16 671 查看
一、数据库

1) 创建数据库

    create databases 名字;

2) 查看数据库

    show databases;

3) 指定数据库

    use 数据库名;

4) 删除数据库

    drop database 数据库名;

************************************************************************************************************************************************************************************************

二、表


1) 创建表

    create table 表名(

    列名1 数据类型描述,

    列名2 数据类型描述, 

    ......

    列名n 数据类型描述

    );

e.g

create table a(

    -> id int unsigned not null auto_increment primary key,

    -> name char(10) not null,

    -> sex varchar(5) not null,

    -> age tinyint not null,

    -> tel char(13) null default"-"

    -> ); 

【unsigned-无符号;not null-非空;auto_increment-递增;primary key-主键】

2) 显示当前数据库中的表

    show tables;

3) 查看表的结构

    describe 表名;show columns from/in 表名;

4) 根据已有的表创建新表

   ① create table 新表名 like 旧表名;      e.g 
create table b like a;

   create table 新表名 as select 列名,...,列名 from 旧表名;    e.g  create table b as select id,name from a;

     【复制整个表:create table 新表名 as select * from 旧表名】 (as可省)

5) 删除表

    drop table 表名;

6) 重命名表

    rename table 表名 to 新表名;alter table 表名 rename 新表名;

================================================================================================================================

7) 向表中插入数据

   直接插入数据

      insert into 表名(列名1,列名2,...,列名n) values(值1,值2,...,值n);

      e.g    insert into a(id,age,name,sex,tel) values(1,12,"小明","男","123"),(2,12,"小红","女","111");

     【插入一行所有数据时,可省略表名后的列名  insert into 表名 values(...);此时values按建表时列的定义顺序】

   插入从另一个表中检索出来的数据

      insert into 要插入数据的表名(列名1,列名2,...,列名n) select 列名1,列名2,...,列名n   from  检索数据的表名;

      e.g    insert into b(name,sex,age) select name,age,sex from a;

8) 查询表中数据

   查询某一列

     select 列名 from 表名;

   查询表中所有数据

     selet * from 表名
4000


   按特定条件查找

     select 列名 from 表名 where 条件

     e.g  查询年龄在21岁以上的所有人信息: select * from students where age > 21;

            查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";

            查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;

9) 修改表中数据

    update 表名 set 列名=新值 where 条件

    e.g  将id为5的手机号改为默认的"-": update students set tel=default where id=5;

           将所有人的年龄增加1: update students set age=age+1;



10) 删除表中数据


    delete from 表名 where 条件;

    e.g 将id为5的行删除:delete from a where id=5;

================================================================================================================================

11) 添加列

    alter table 表名 add 新列名 数据类型描述;

    e.g 默认加在表末:alter table a add address char(5) not null;

          在指定列后添加列:alter table a add address char(5) after age;

          在表的最前面添加列:alter table a add address char(5) first;

12) 修改列

    alter table 表名 change 列名 新列名 新数据类型;

    e.g 将列名tel改为telphone:alter table b change tel telphone char(13) null default"-";

          修改name列数据类型:alter tabel b change name name char(20) not null;

13) 删除列

    alter table 表名 drop 列名;

    e.g 删除表中f列:alter table b drop f;

================================================================================================================================

14) 排序

    ① 升序:select * from 表名 order by 列名,...,列名;

    降序:select * from 表名 order by 列名,...,列名 desc;

    e.g select * from a order by name;

15) 求某列数据总数(不含缺省)

    select count(所求列名) as 结果列名 from 表名;

    e.g  telphone列有数据的行数:  select count(telphone) as totalcount from a;

           统计表的总行数:select count(*) as count from a;

16) 对某列数据求和

    select sum(所求列名) as 结果列名 from 表名;

    e.g 求age列总和: select sum(id) as idsum from a;

17) 求某列数据平均值

    select avg(所求列名) as 结果列名 from 表名;

    e.g 求age列平均值:select avg(age) as ageavg from a;

18) 求某列数据最 大/小 值


    select max/min(所求列名) as 结果列名 from 表名;

    e.g 求age列最大值:select max(age) as maxage from a;

================================================================================================================================


   

   

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: