MySQL 数据库 (基础操作)

对数据库的操作:

 

    create database web08_01 character set gbk;// create 创建带有编码数据库

    show create database web08_01;//查看数据库信息

    drop database 库名;//删除数据库

    use 库名;//使用数据库

    select database();//查看当前使用的库

 

对数据库表的操作:

 

    创建表:

 

    create table 表名{

        字段名 类型(长度)[约束],

        字段名 类型(长度)[约束],

        字段名 类型(长度)[约束] 

    }

    primary key;//要求被修饰字段:唯一、非空

    unique;//要求被修饰字段:唯一

    not null;//要求被修饰字段:非空

    

    查看表:

    show tables;

    

    查看表结构:

    desc 表名;

    

    删除表:

    drop 表名;

    

    添加一列:

    alter table 表名 add 字段名 类型(长度)[约束];

 

    修改类型:(长度、约束)

    alter table 表名 modify 要修改的字段名 类型(长度)[约束];

 

    修改字段名:

    alter table 表名 change 旧字段名  新字段名 类型(长度)[约束];

 

    删除表的一列:

    alter table 表名 drop 字段名

 

    修改表名:

    rename table 表名 to 新表名

 

    修改字符集:

    alter table 表名 character set 编码;

 

对数据表记录进行操作

 

    插入记录:

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

    or

    insert into 表名 values(值1,值2,值3...);//为每一字段设置值;

 

    修改记录  :

    updata 表名 set 字段名 = 值...(不带条件);

    updata 表名 set 字段名 = 值 where 条件(带条件);;

 

    删除记录:

    delete from 表名 where  条件(带条件);

    delete  字段名 from 表名 (不带条件);

 

    查询操作:

语法:

    select * | 列名,列名 from 表名;

 条件查询:

    1、查询商品名称为‘左慈’的商品信息

    select * from product where pname = '左慈';

    2、查询价格大于60的商品信息

    select from product where price > 6;

    3、查询带有‘士’字的商品信息

    select * from product where pname like'%士%';

    4、查询商品id在(3,6,9)范围内的所有商品信息

    select * from product where pid in(3,6,9);

    5、查询商品名称含有士’字而且 id 为 6 的 商品信息。

    select * from product where pname like%士% and pid = 6;

    6、查询 id 为 6 或 2 的商品信息

    select * from product where id = 6 or id = 2;

排序:

    1、查询所有商品按价格排序(升序)

    select * from product order by price asc;

    2、(降序)

    desc;

    3、查询含有‘士’字的商品信息并按价格降序排序

    select * from product where pname like %士% order by price desc;

聚和函数:

    1、获得所有商品价格总和

    select sum(price) from product;

    2、获得所有商品的平均价格

    select avg(price) from product;

    3、获得所有商品的个数

    select count(*) from product;

分组操作:

    1、根据cid分组统计个数

    select count(*) from product group by cid;

    2、根据cid分组统计每组商品的平均价格并且平均价格大于20000

    select avg(price) from product group by cid having arg(price) > 20000;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值