mysql 表属性操作随笔

语句 set names utf8;用于设定数据库编码,让中文可以正常显示。

创建数据库:

 CREATE   DATABASE   `database` 
                           CHARACTER   SET   'utf8' 
                           COLLATE   'utf8_general_ci '; 

创建表:

CREATE   TABLE   `database_user`   ( 
                          `ID`   varchar(40)   NOT   NULL   default   ' ', 
                          `UserID`   varchar(40)   NOT   NULL   default   ' ', 
                                )   ENGINE=InnoDB   DEFAULT   CHARSET=utf8; 

创建表:

CREATE TABLE `csyzz` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age1` char(10) DEFAULT NULL,
  `time` datetime DEFAULT NULL,
  `author` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

修改表名

alter table test rename test1;

–添加表列

alter table test add  column name varchar(10); 

–删除表列

alter table test drop  column name; 
或
alter table family drop age

修改表列名age为age1;

alter table ceshishuju change  column age age1 varchar(30);

–修改表列类型

alter table test modify address char(10) 
或
alter table test change address address  char(40)

修改表中列类型的语法:
alter table 表名 modify 列名 新类型 新参数;

alter table family modify age varchar 

如果想增加一列,并位于表的最前面,用first
alter table 表名 add 列名称 列类型 列参数 first;

alter table m1 add pid int not null first;

添加数据

insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ; 

给特定的列添加数据

insert into Info (code,name) values('p010','李四');

自增长列的处理

insert into family values('','p001','数据','T001','数据',1);

insert into 表名 values(值)

删除数据
删除所有数据

delete from family

删除特定的数据

delete from Info where code='p001'

delete from 表名 where 条件

修改数据
修改所有数据

update Info set name='徐业鹏' 

修改特定数据

update Info set name='吕永乐' where code='p002' 

修改多列

update Info set name='吕永乐',sex=1 where code='p003'

update 表名 set 要修改的内容 where 条件

读取数据
(1)简单读取,查询所有列(*) 所有行(没有加条件)

select * from Info

(2)读取特定列

select code,name from Info

(3)条件查询

select * from Info where code='p003'

(4)多条件查询

select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系

(5)关键字查询(模糊查询)
查所有包含奥迪的汽车

select * from car where name like '%奥迪%'; #百分号%代表任意多个字符

查以’皇冠’开头的所有汽车

select * from car where name like '皇冠%';

查询汽车名称中第二个字符是’马’的

select * from car where name like '_马%'; #下划线_代表任意一个字符

(6)排序查询

select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc

先按brand升序排,再按照price降序排

select * from car order by brand,price desc

范围查询
select * from car where price>40 and price<60 --查询价格在40-60之间的

select * from car where price between 40 and 60   --between...and...

离散查询 查询离散值,例如查询汽车价格是30、40、50、60等整数的

select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)
select * from car where price not in(30,40,50,60)   --价格除30,40,50,60以外的数的

聚合函数(统计查询)

select count(*) from car    --查询这张表里面有多少数据。count方法可以用来求条数
select count(code) from car #取所有的数据条数。code为主键,内容不为空,可以用code查询。
select sum(price) from car #求价格总和
select avg(price) from car #求价格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值

数据查询原理:数据库在查询数据的时候会每一条数据都会查一遍,查询的时候如果条件成立,会返回数据true,如果不成立,会返回false。如果返回的是true,就会选取这条数据,如果返回的是false,就pass掉这条数据。例如,如果select * from car where 1=1,输出的会是表格的所有数据,因为1=1恒满足条件。

分页查询 每一页有很多数据,可以查看下一页的数据。

select * from car limit 0,10 #分页查询,跳过几条数据(0)  取几条(10)

规定一个每页显示的条数:m
当前页数:n

select * from car limit (n-1)*m,m  取第n页显示的m条数据

去重查询

select distinct brand from car     --distinct表示去重,前面代码表示对brand列去重查询

去重查询适合查1列,查2列就不适合用了。

分组查询
查询汽车表中,每个系列下汽车的数量。按照brand分组。

select brand,count(*) from car group by brand

分组之后,只能查询该列或聚合函数

取该系列价格平均值大于40的系列代号

select brand from car group by brand having avg(price)>40

取该系列油耗最大值大于8的系列代号
select brand from car group by brand having max(oil)>8

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值