mysql学习笔记

来源:互联网 发布:淘宝苹果手机联保 编辑:程序博客网 时间:2024/05/27 16:43

安装mysql过程省略。。。

打开任务管理器启动mysql服务

>mysql -u root -proot  启动mysql先cd到mysql的bin目录下
>mysql -u root -proot  先设置环境变量path中添加mysql的安装目录D:\Program Files (x86)\MySQL\MySQL Server 5.5\bin
show databases;  查看所有表
---------------------------------
create database tablename;  创建名为tablename的数据库
drop database tablename;   删除名为tablename的数据库
use tablename  使用tablename数据库
quit或者exit  断开mysql链接
show tbales;  查看所有表
describe tablename;  查看表结构
--------------------------------------
按照建表然后增删改查
create table tablename(id int,username vachar(100));  新建tablename表
insert into tablename(id,user_name,pass_woed) values(1,'zhangsan','123456');插入数据
insert into tablename(id,user_name,pass_woed) values(1,'zhangsan','123456'),(2,'lisi','456312');插入多条
load data local infile "filename.sql" into table tablename;  load data从文本文件插入大量数据
delete from tablename;  删除表中所有数据
drop table tablename;  删除表
update tablename set id = 10 where ..  修改表中数据
avg() min() sum() count()函数的使用
distinct
查询时 字段的计算等
limit的使用 limit 1,2;第一个数字为偏移量,第二个数字为记录数
-------------------------------------------------
改变表结构
1.添加列
alter table tablename add birthday date;  添加一列新的birthday 类型为date
mysql还有year类型
alter table tablename add year_born year;  添加出生年份列
2.更改列定义
alter table  tablename change year_born birthday date; 旧的列名称year_born 新列名称birthday 数据类型date 不改列名就写成一样的 
alter table tablename modify oldname 新的列定义;
3.重命名表
alter table tablename rename newtablename;
alter table tablename rename to newtablename;
rename oldtablename to newtablename;
4.删除列
alter table tablename drop 列名;
-----------------------------------------------------
日期函数(部分)附录ab有更多信息
select date_format(birthday,'%m%d%y') from tablename where ..  02/15/2016
select date_format(birthday,'%W %M %e %y') from tablename where .. Tuesday February 15 16 //15号 16年
select date_format(birthday,'%a %D %b, %Y') from tablename where ..
获取系统当前时间
select now(),current_date();
select year(birthday) from tablename;//获取年份用year()
同理:month() dayofmonth()//月 日
dayofyear()一年中的第几天
--------------------------------------------------------
concat()函数的使用
select 
concat(first_name,' ',surname) as name,
month(birthday) as month,
dayofmonth(birthday) as day 
from tablename 
order by month;
-----------------------------------
高级查询
select * from table1,table2 where colunm1=colunm2//
日期计算
select year(now())-year(birthday) from tablename;//根据生日计算年龄
select year(current_date())-year(birthday) from tablename;
select year(now())>year(birthday) from tablename where ..//真的返回1
select year(now())<year(birthday) from tablename where ..//家的返回0
返回日期快捷方式right()//字符串函数
select right(current_date,5),right(birthday,5) from tablename//5返回字符串长度,实际是返回完整字符串2002-04-05的右边五个字符即:04-05
select year(current_date())-(right(current_date(),5)<right(birthday,5)) as age from tablename//年龄计算 当前月份和生日月份比较判定
--------------------------------------------------------
分组group by   sum()函数//注:分组的结果只能返回分组字段和组函数联合使用
select a,sum(value) from tablename group by a;//正确
select a,sum(value) from tablename;//错误

select a,sum(value) as sum from tablename group by a order by sum desc;

-------------------------------------------------------------------------------------------------------

表类型:影响性能速度
事务安全的表类型:InnoDB BDB
事务不安全的表类型:ISAM MyISAM MERGE HEAP
ISAM老类型相较于MyISAM类型,后者索引更小,占用更少系统资源
替换表类型语句:alter table tablename type = MyISAM;
查看表类型语句:show table status from 'test';//查看test库中所有表的类型
  show create table 'tablename';//建表时的信息有表类型信息
MyISAM分三子类型:静态 动态 压缩
静态表:定长表 
动态表:不定长 节省空间 但复杂性上升 要经常维护 碎片化问题
压缩表:只读表
----------------
merge表:形同MyISAM表的合并。
heap表:最快的表类型,内存表,散列索引。缺点是会数据丢失
InnoDB:具有commit 的rollback能力
BDB:伯克利数据库独立mysql存在


0 0
原创粉丝点击