Mysql 小結

我與Mysql:老實說我在大三上學期的時候也上過一門數據庫原理的課程,教授的主要是SQL語言,使用微軟的SQL Server2000,當然,上課歸上課,因爲沒項目做,自己也總是逃課要,只是 到期末纔來臨時抱佛腳,最後87分漂過。但後來我才發現,原來SQL其實是蠻重要的,即使你不往數據庫研究這方面深入,在一般的技術筆試中都會較大權重的SQL知識檢查,因爲幾乎所有的項目中都要涉及到數據,有數據就要用到SQL,因爲SQL是數據操作語言的規範,我自己也體會到了SQL的重要性。所有抽空把自己關於對SQL的知識做個整理。當然我這裏要強調一下,因爲我在實習時使用的是瑞典MySQL AB公司的Mysql語言,所以下面總結的是這方面的知識,但是與SQLServer,Orcal,DB2差不多,都是基於SQL語法,只不過Mysql開源且適應於中小型企業的數據開發,基本能滿足需求了。好了,言歸正傳吧:


首先,我們要下載一個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;


刪除kendy數據表:drop table 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章