MySQL常用的命令

本周学习知识点:MySQL数据库

1.数据库概念

2.数据库常用的命令

3.数据表常用命令

4.数据表的增删改查

5.MySQL的数据类型(列类型)

   5.数值类型

   5.2 字符型

   5.3 日期和时间类型

 

一、数据库概念:

   数据库(DataBase  简称DB ):数据库是长期储存在计算机内的、有组织的、可共享的数据集合。数据库中的数据按一定的数据模型组织、描述和储存,具有较小的冗余、较高的数据独立性和易扩展性,并可为各种用户共享。

 

数据库系统(DataBasSytem,简称DBS ):数据库系统是指在计算机系统中引入数据库后的系统构成,一般由数据库、数据库管理系统(及其开发工具)、应用系统、数据库管理员构成。

 

数据库管理系统(DataBase Management sytem,简称DBMs ):数据库管理系统是位于用户与操作系统之间的一层数据管理软件,用于科学地组织和存储数据、高效地获取和维护数据。DBMS的主要功能包括数据定义功能、数据操纵功能、数据库的运行管理功能、数据库的建立和维护功能。

 

二、数据库相关命令

     1.Mysql -h  127.0.0.1 -u root -p  (dos页面)添加环境变量:

windos 7下计算机-->属性-->系统设置-->高级-->环境变量-->path(双击)-->(D:wamp/mysql/bin)粘在变量值的最后,用分号隔开

    2.查看有哪些数据库: show databases; 

    3.数据库操作命令以 ;结束

    4.默认提示的数据库不要动,可以在 自己建的库 里面做练习

    5.创建数据库   create database 数据库名;

    6.先选中在那个数据库里面创建表  use 数据库名;

    7.删除数据库  drop database 数据库名

 

 三、数据表相关操作命令

   1. 查看数据库有哪些表:show tables

   2. 创建一张表

  create table money(

   id int,name varchar(32),

   money varchar(10));

   3. 修改表名:    rename table 旧名 to 新名  

   4. 删除一张表:  drop table 数据表名

   5. 查看表的结构:desc 表名

 

四、数据表的增删改

1. 增加一条记录  insert into user values('李四','30');

增加多条记录  insert into user values('李四','30'),('王五','18'),('赵丽','33');

查询记录    select  *from 表名;

删除记录  delete from user where name='张三';

   删除表:  drop table tcourse;

更新记录  update 表名 set 新的内容   update user set age='26' where name='name';

5.1 增加一列: alter table goods add price decimal(4,2)

5.2 删除一列: alter table goods drop column goods_name

5.3 改变列类型

1.change  旧的名字 新的名字 列类型  alter table user change age age tinyint;

2. modify  字段名  新的数据类型:  alter table user modify age varchar(3);

 

五、数据类型(列类型)

    

  数值型

     整型:

     tinyint:       范围:0-255

        smallint:       范围:0- 65535

    mediumint:     中等

      int           范围:   42亿多

     bigint 

 

 

 

 

 

 

 

 

 

   整型列的可选属性

        

      1.有符号:signed  有正负数之分

      2.无符号:unsigned  只有正数,没有负数

      tinyint-----范围255个数---如果有正负数之分的话:-128--127 

               如果没有符号之分的话,就表示 0-255 

      3. 列可以声明默认值,而且推荐声明默认值:NOT NULL DEFAULT 0

   

   小数型:

       1. 浮点数   float(M,D)  

       2. 定点数   decimal(M,D)   M精度 :总位数 D标度 :小数位数 

       3. 通过练习比较 float() 和 decimal() 的区别:

    (1) 如果对数据的精度要求比较高,使用定点数

    (2) 定点数以字符串的形式保存,运算的时候会转化,所以效率比较低

             +------+-----------+-----------+

             | name | flo_p  | deci_p |

             +------+-----------+-----------+

             | li | 2349.1270 | 2349.1269 |

             +------+-----------+-----------+

  

字符串类型:

char(M)  定长字符串

varchar(M) 可变长度字符串

规定了这个类型保存的范围 例如,char(4) -----保存4个字符 ,如果超出的话,自动截取,如果不够的时候,char()自动补全(不够的用空格补齐),varchar() 不补齐

 

区别:

空间:varchar 所占空间 要比 char 占的少

速度:cha类型的 执行速度 要比 varchar 要快,查询的时候,会判断每个类型的空间

char()  范围可以是  0-255之间的字符个数

varchar() 范围可以是 0-65535 之间的字符个数

   text 和 blob:

   text: 文本数据(文章、日记),大的文本,但是搜索速度慢,如果不是特别多,建议使用varchar

 

   blob: 保存二进制数据(照片、电影、压缩包)

使用文件处理,将图片的二进制内容读取出来之后,保存到字符串中,在保存到数据库中

 

 enum 和 set:

 enum() 枚举类型:alter table student modify sax enum('','','保密');

 set()    集合类型:alter table student add column height set('1.60','1.65');

枚举类型 只能保存一个值,set 集合类型一次可以保存多个值

只占用一个或2个字节的空间(因为我们是通过下标来找到对应的值的)

+--------+---------------------------+------+-----+---------+-------+

| Field  | Type                      | Null | Key | Default | Extra |

+--------+---------------------------+------+-----+---------+-------+

| name   | varchar(10)               | YES |     | NULL |       |

| sax    | enum('??','??','±???')      | YES |     | NULL |       |

| age    | tinyint(4)                 | YES |     | NULL |       |

| email  | varchar(20)               | YES |     | NULL |       |

| phone  | bigint(20)                | YES |     | NULL |       |

| jian   | char(140)                 | YES |     | NULL|       |

| gz     | char(10)                  | YES |     | NULL|       |

| rq     | varchar(10)               | YES |     | NULL|       |

| height | set('1.60','1.65','1.70')        | YES |     | NULL|       |

+--------+---------------------------+------+-----+---------+-------+

 

日期和时间类型:

Datetimeyeartimestamp (和php的时间戳不一样,直接显示当前的时间了,PHP中的时间戳是用秒来计算的)

 

日期:年月日  date字段  

日期+时间:   时间:时分秒   time    

年份:        year      

时间戳:      timestamp 

 

MySQL数据库中获得时间和日期的函数

 curdate()函数

 curtime()   

 now()  

 current_stamp

当我们保存一个字段是  时间戳的时候,他会自动的更新当前的时间戳

 

六、查询语句

 where查询语句:

whereselect goods_name from goods where shop_price >1000;

select goods_name,shop_price from goods where shop_price>1000;

 

innot in: select goods_name,goods_id from goods where goods_id in(3,4,5,7);

 

or: select goods_id,goods_name from goods where goods_id = 3 or goods_id=5 or goods_id=7 or  goods_id=8;

 

and: select goods_name,goods_id,shop_price from goods where goods_id<10 and sh

op_price>100;

 

between。。。and:在某个区间的

 select goods_name,goods_id from goods where goods_id between 10 and 20;

 

like模糊查询: 任意字符任意个   任意字符的某一个

select goods_name from goods where goods_name like '%诺基亚%';

select goods_name from goods where goods_name like '诺基_%';

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

order by:排序的方式(升序ASC、降序DESC

select goods_name,shop_price from goods order by shop_price desc;

 

limit 限制数量:select goods_name,shop_price from goods order by shop_price desc limit 3;

select goods_name,shop_price from goods order by shop_price desc limit 10,3;

 

分组查询:group by  count(),sum(),avg(),max(),min()

  select min(shop_price)from goods;

  select cat_id,avg(shop_price) from goods group by cat_id;

  select cat_id,count(cat_id) from goods group by cat_id;

  select cat_id,max(shop_price)from goods group by cat_id;

  select cat_id,min(shop_price)from goods group by cat_id;

 

  select cat_id,avg(shop_price) as avg_price from goods group by cat_id hav

ing avg_price>100;

 

两科及两科以上不及格者的平均值:

select xs,avg(score) from name group by xs;

 select xs,score<60 from name;

select xs,sum(score<60) from name group by xs;

 select xs,sum(score<60) as gk from name group by xs having gk>=2;

select xs,sum(score<60) as gk, avg(score)from name group by xs having gk>=2;

 

union 连接查询:

 

unionorder by必须使用limit,数量一致,字段一致

(select goods_name,shop_price from goods where cat_id=3 order by shop_pric

e asc limit 3)union(select goods_name,shop_price from goods where cat_id=4 order by shop_price desc limit 3);

 

重复内容会覆盖:select name,score from java union select name,score from php;

 

避免覆盖:select name,score from java union  all select name,score from php;

 

导出文件:select name,score from java union  all select name,score from php into outfile 'E:student.xls';

 

#####列表测试(子查询

create table category(

cat_id tinyint not null default 0,

cat_name varchar (32) not null default'',

parent_id tinyint not null default 0

)default charset utf8;

 

Insert into category select cat_id,cat_name,parent_id from ecshop.ecs_category;

 select cat_id from ecs_category where cat_name='GSM手机';

 select goods_name,cat_id from ecs_goods where cat_id=3;

先由cat_name得到cat_idselect cat_id from ecs_category where cat_name='GSM手机';

 

将得到的作为条件:

 select goods_name,cat_id from ecs_goods where cat_id=(select cat_id from

ecs_category where cat_name='GSM手机');

 

两科及两科以上不及格者的平均值(子查询):

 select xs from name where score<60 group by xs having sum(score<60)>=2;

  select xs,avg(score)from name where xs in('张三','李四')group by xs;

 

 select xs,avg(score)from name where xs in(select xs from name where score

<60 group by xs having sum(score<60)>=2) group by xs;

 

学习总结:

一、乱码:

set names gbk;

建表的时候,保存utf8编码的,使用的时候(根据国家选择适合的编码)

 

utf8:国际标准的编码,通用性强,如果想做一个网站,将来想面向世界,使用utf8

gbk:编码的集合((大陆、台湾)、日本、韩国)

gb2312:大陆的,是gbk的一个子集

 

解决的方法:

创建表的时候,设置编码为utf8

使用的使用 set names gbk;在那个国家设置为对应的编码

 

二、NULL 和 default

在数据库中,NULL 既不是 0  也不是 空字符串,不能参与任何运算,不是我们需要的

 

 

如果添加数据的时候,不想给某个字段值,为了避免出现 NULL,尽量给他一个默认值

default:

 alter table student modify name varchar(32) not null default '';

 insert into student(id,stu_id) values(30,59);

 

 

 

三、#创建商品表

create table goods(

goods_id int primary key auto_increment,

cat_id smallint not null default 0,

goods_sn char(15) not null default '',

goods_name varchar(30) not null default '',

click_count mediumint unsigned not null default 0,

brand_id smallint not null default 0,

goods_number smallint not null default 0,

market_price decimal(7,2) not null default 0.00,

shop_price decimal(7,2) not null default 0.00,

add_time int unsigned not null default 0

)charset utf8;

 

ecshop goods 商品表中,查询到这些记录,并添加到自己创建的数据表中

insert into phone.goods select goods_id,cat_id,goods_sn,goods_name

,click_count,brand_id,goods_number,market_price,shop_price,add_time from ecshop.

ecs_goods;

 

四、分页类

 PHP+MySQL分页的实现:

     1.获得当前的页码数

     2.设置每页显示几条记录

     3. 去数据库查询数据

     4.查询所有的记录,并显示

     5.计算总的记录数

     6.计算总的页数

     7.显示超链接

 封装分页类:

  目的: 以后在其他页面需要分页了,只需要加载这个类,调用方法即可

static  静态方法:

只能通过类来访问:

类名:: 静态方法名

 

取整的函数分类:

echo ceil(7.09).'<br/>';

echo floor(7.99).'<br/>';

echo round(7.49).'<br/>';

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值