我与Mysql:老实说我在大三上学期的时候也上过一门数据库原理的课程,教授的主要是SQL语言,使用微软的SQL Server2000,当然,上课归上课,因为没项目做,自己也总是逃课要,只是 到期末才来临时抱佛脚,最后87分漂过。但后来我才发现,原来SQL其实是蛮重要的,即使你不往数据库研究这方面深入,在一般的技术笔试中都会较大权重的SQL知识检查,因为几乎所有的项目中都要涉及到数据,有数据就要用到SQL,因为SQL是数据操作语言的规范,我自己也体会到了SQL的重要性。所有抽空把自己关于对SQL的知识做个整理。当然我这里要强调一下,因为我在实习时使用的是瑞典MySQL AB公司的Mysql语言,所以下面总结的是这方面的知识,但是与SQLServer,Orcal,DB2差不多,都是基于SQL语法,只不过Mysql开源且适应于中小型企业的数据开发,基本能满足需求了。好了,言归正传吧:
然后,我们就可以开始使用Mysql了,
打开Dos命令行,Ctrl+R,输入cmd,按回车即可进入经典的DOS窗口。
启动Mysql服务:输入 net start mysql,正确情况下就会看到“Mysql已启动成功”。
进入Mysql:输入命令mysql -u root -p 回车,再敲root,当看到下图时就表明你已经成功进入Mysql界面,
创建一个数据库:create database kendy;
创建一张数据表: use kendy;create table mytable( name varchar(10), age int(4));
查看表的创建代码:show create table mytable;
查看表结构:desc mytable;
新增加字段: alter table mytable add school char(20), add company char(50),add id int(11) ;
删除一个字段:alter talbe mytable drop column companry;
插入具体数据:insert into mytable values('kendy',20,'SCNU',100054);
insert into mytable values('Tom',30,'SCNU',100024);
insert into mytable values('Jaky',23,'SCNU',100034);
insert into mytable values('Lizy',22,'SCNU',100014);
显示前3条内容:select * from mytable limit 3;
更改记录值:update mytable set name='Jacky' where name='Jaky';
添加主键:alter table add primary key (id);
删除指定记录:delete from mytable where id="100014";
修改age字段的数据类型为int(3): alter table mytable modify column age int(3);
查询总人数和平均年龄: select count(name) as 总人数 ,avg(age) as 平均年龄 from mytable;
删除mytable数据库:drop database kendy;
其它:
导入数据到表如果是导入.sql文件则用source
导入数据到表:load data local infile '/download/CM_20150712135902.txt' into table mytable fields terminated by ',';
导入sql文件到数据库:use epcde; source D:\epcdb.sql
查询结果导出到外文件中 select * from tablename limit 10 into outfile 'C:/result.txt';(把前十条输出到指定文件中,会自动创建
建立相同结构的表:create table A like B;
从一个表导入数据到另一个表中:insert into A select * from B ; (A 与 B 的结构必须一样,不然得指定字段属性)
附:我实习时做的一个跨月查询语句:
******************************************************************************************
select a.starttime,a.city_en,sum(a.upcount) as upcount,sum(a.downcount) as downcount, sum(a.link) as link,sum(a.timelength) as timelength,mdn,count(distinct a.mdn) as
users from (select starttime,city_en,sum(upcount) as upcount,sum(downcount) as downcount, sum(upcount+downcount) as link,sum(timelength) as timelength,mdn,count (distinct mdn) as users from aaa_analyse_4g_user_pre_201507 where 1=1 and city_en in ('GZ','SZ','DG','FS','ST','ZS','HZ','JM','ZJ','JY','ZH','MM','ZQ','MZ','QY','CZ','SW','HY','SG','YJ','YF') and starttime >='20150701' and starttime <='20150831' group by city_en,mdn union select starttime,city_en,sum(upcount) as upcount,sum(downcount) as downcount, sum(upcount+downcount) as link,sum(timelength) as timelength,mdn,count(distinct mdn) as users from aaa_analyse_4g_user_pre_201508 where 1=1 and city_en in
('GZ','SZ','DG','FS','ST','ZS','HZ','JM','ZJ','JY','ZH','MM','ZQ','MZ','QY','CZ','SW','HY','SG','YJ','YF') and starttime >='20150701' and starttime <='20150831' group by city_en,mdn) a group by a.city_en ;
//其中,union是联合查询,a是临时表名,外嵌套select语句。
*******************************************************************************************
原创作品,希望对你有帮助,欢迎转载,转载请著明出处,谢谢!作者:greatkendy123