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

Mysql学习笔记

2013-03-06 09:46 197 查看
1,unsigned:不允许出现负数

2, uto_increment:序列编号它的工作原理是这样的:当我们往member数据表里插入数据记录时,如果没有给出member_id列的值(或者给出的值是NULL),Mysql将自动生成下一个编号并赋值给这个数据列。
3,primary key:需要对member_id数据列创建索引以加快查找速度,且该数据列的值唯一。
4,任何primary key列都必须是not null.若定义中忽略了not null,mysql将自动添加上去。
5,查看表:
DESCRIBE member;
desc member;
explain member;
show columns from member;
show fields from member;
6,按条件查看表
#show columns from member like ‘%name’;
#desc member ‘%name’;
#select score.name,grade_event.date,score.score,grade_event.category from score INNER JOIN grade_event.event_id WHERE grade_event.date=’2008-09-23’;
7,show databases;后看到的
mysql数据库中存放着各种用来控制MySQL访问权限的权限分配表。
Test数据库是在MySQL安装过程中创建的。
8,engine=InnoDB; 定义表的存储引擎。
存储引擎:一种用来管理某种特定类型的数据表的处理器。
如果不定义表的存储引擎,MySQL会替你选择默认的存储引擎——MyISAM。
9,ISAM-indexed sequential access method-索引化顺序访问方法。
InnoDB引擎通过引入“外键”概念而具备了保持“引用一致性”的特点,这意味着我们可以通过MySQL让数据表之间的关系满足一定的约束条件。
“外”的含义是“在另一个数据表里”
“外键”:一个给定的键值必须与另一个数据表里的某个键值相匹配。
10,设置2个主键时:两个主键内容的组合代表唯一性!而且这2个值单独出现时都不具备唯一性!
11,foreign key 定义它应该遵守的约束条件。
References 指定score数据列应该与哪个数据表里的哪个数据列相对应。



12,Foreign key定义可以确保在我们的数据行不会有在grade_event或student数据表里并不存在的虚假ID值。
P54
13,加载数据文件
13.1:load data语句就像是一架大型装载机,它能把文件里的数据一次性地全部读到数据表里。
使用方法:load data local infile ‘member.txt’ into table member;
Member.txt里面有好多数据,可以导入到member表里面去。
使用load data local 时出现如下错误:
ERROR 1148(42000):the used command is not allowed with this mysql version
说明Local功能默认禁用。
解决办法:#mysql –local-infile dbname
若不管用说明没有激活local机制,激活之。
13.2:mysqlimport
14,2个等价的查询。
#select last_name from president where state=’VA’or state=’MA’;
#select last_name from president where state in(‘VA’,’MA’);
15,查询NULL
#select last_name from president where death is null;
查看含null的列时,只能用is null 或is not null来判断。
NULL有专用的比较操作符<=>
#select last_name from president where death <=>NULL;
#select last_name,suffix from president where not(suffix<=>NULL);
16,数据删除操作会在数据表里留下一些空洞,而mysql会用你以后插入的新记录来尽量填补这些空洞。会打乱你插入的时间顺序。
17,对查询结果进行排序。
#select last_name,first_name from president order by last_name;
默认是升序排列
#select last_name,first_name from president order by last_name desc;
降序排列加入desc
#select last_name,state from president order by state desc,last_name asc;
州按逆序排列,州相同的姓名按升序排列
18,包含NULL值的数据行,升序排列她们出现在开头,降序在结尾。
把降序的NULL在开头
#select last_name,death from president order by if(death is null,0,1)
,death desc;
If(death is null,0,1)表示death is null返回0,否则为1。0在1前面!
19,限制查询结果中的数据行个数。
#select last_name birth from president order by birth limit 5;
要5行。
#select last_name birth from president order by birth limit 10,5;
要跳过前10的那5行。也就是11-15行。
#select last_name birth from president order by rand() limit 5;
随机抽5行
20,对输出列进行求值和命名
#select 17,format(sqrt(25+13),3);
搜17和25+13的平方根,结果取小数点后3位。
#select concat(first_name,’’,last_name),concat(city,’,’,state)
From president;
给输出列命名。
#select concat(first_name,’’,last_name) as name,
concat(city,’,’,state) as birthplace
From president;
给输出列取别名。
#select concat(first_name,’’,last_name) as name,
concat(city,’,’,state) as ‘birth place’
From president;
如果别名有空格,就用单引号引起来。
#select 1,2 as two,3 three;
为数据列提供别名时,as可以省略。
21,日期
#select * from grade where date=’2008-10-10’;
#select lastname,death from president where death>=’1970-10-10’and
Death<’1980-10-10’;
#select lastname,birth from president where month(birth)=3;
3月出生的总统。
#select lastname,birth from president where month(birth)=‘March’;
3月出生的总统。
#select lastname,birth from president where month(birth)=‘March’and
Dayofmonth(birth)=29;
3月29出生的总统
Year()可以表示年。
Curdate()永远返回今天。
#select lastname,birth from president where month(birth)=month(curdate()) and
Dayofmonth(birth)=dayofmonth(curdate());
今天出生的总统。
#select lastname,birth,death,timestampdiff(year,birth,death) as age
From president where death is not null order by age desc limit 5;
算已死去总统的年纪。列出5人。
#select lastname,expiration from member where
(TO_DAYS(expiration)-TO_DAYS(curdate()))<60;
#select lastname,expiration from member where
Timestampdiff(day,curdate(),expiration)<60;
查60天以内过期的会员。
TO_DAYS()函数可以将日期转化为天数。
#select date_add(‘1970-1-1’,interval 10 year);
返回1980-01-01
#select date_sub(‘1970-1-1’,interval 10 year);
返回1960-01-01
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Mysql学习