|
一、mysql常見的數(shù)據(jù)類型: int(8) 整形, float(5,1) 浮點型,規(guī)定插入的值不超過5位數(shù)字,小數(shù)點后面帶兩位數(shù)字且小數(shù)點超出后會四舍五入, double(5,2)一樣是浮點型 decimal(5,2) 一樣是浮點型。 char(8)字符型, varchar(8)可變字符型。 date,values(‘2014-04-23’)或(20104423) time,values(‘12:30:50’)或(123050) year,values(2014) datetime,values(‘1993-03-12 12:30:12‘)或(012) null 類型。表示什么都沒有。 null 不等于 ' ' ~~~。 二、mysql常見的運(yùn)算符: +、-、*、/、%、 <,>,=,>=,<=,!=,<=>(用于安全等于null值。no <=> null) NOT 或 !,AND 或 && ,OR 或 ||, 三、mysql常見的函數(shù): ABS(X)返回x的絕對值。 CURDATE() 返回當(dāng)前的日期 2014-04-23 CURTIME() 返回當(dāng)前的時間 19:17:13 NOW() 返回當(dāng)前的日期和時間 2014-04-23 19:17:48 聚合函數(shù):AVG(COLUME) 求該列的平均值, COUNT(COL) 計算列中非null的行數(shù),MIN(COL) 求該列的最小值,MAX(COL) 求該列的最大值, SUM(COL) 求該列值的和,COUNT(*) 計算表中所有的行數(shù),包括null值。 四、數(shù)據(jù)庫和表: 1、創(chuàng)建數(shù)據(jù)庫。 create database db1; 或著 create databse if not exists db1; 2、選擇需要的數(shù)據(jù)庫。 use db1; 3、刪除數(shù)據(jù)庫。 drop database db1; 或者 drop databse if exists db1; 4、創(chuàng)建表:create table table_name (name char(2),id int(2),....) type=xxxx; 4.1 字段約束: 首先 null 不等于 ' ' null: 當(dāng)某列的約束為null時,該字段的值可以插入null,表示沒有數(shù)據(jù) 。建表時系統(tǒng)默認(rèn)是null; not null: 當(dāng)某列的約束為not null時,說明該字段不能插入null值,但可以插入' '值; defualt: 當(dāng)某列的約束為default('**')時,若沒有對該列插入任何數(shù)據(jù),則默認(rèn)是'*'這個值~ 但如果某列沒有定義約束default的時候,系統(tǒng)默認(rèn)是default(null);所以,此時插入數(shù)據(jù)則該數(shù)據(jù)的值為 null; unique:(創(chuàng)建unique時,自動加了index) (他是屬于鍵key的一種)當(dāng)某列的約束為unique時,則要求插入該列的數(shù)據(jù)值不能一樣,連' '這個值都不能一樣~但可以插入多個null值(沒定義 not null的情況下),并且不會報錯。系統(tǒng)默認(rèn)不會加unique這個約束??梢栽诙x列的同時一起定義,也可以在最后定義。 語法:create table t(id int(2) ,unique key unique_name(id)); auto_increment: auto_increment修飾符只適用于int類型的字段。表明Mysql應(yīng)該自動為該字段生成一個數(shù)(從 1 開始,每加一個數(shù)據(jù)該值 +1),意味著,我們插入數(shù)據(jù)時,不用為該字段插入數(shù)據(jù),系統(tǒng)自動會幫我們添加,即使該字段被not null約束。 注意,一個表只能有一個auto_increment字段,而且該字段必須被定義為 鍵(可以是primary key、或unique鍵 )。 primary key: (他是屬于鍵key的一種,但他是主鍵)primary key = not null + unique~ 所以在創(chuàng)建primary key的時候,即使不加入not null,和unique來約束,也能有這兩個的約束效果。 如: create table test (name int(8) primary key);就可以實現(xiàn)主鍵的功能。但一般書上都是這樣寫的 : create table test (name int(8) not null primary key); 主鍵約束不僅僅可以約束一個單獨(dú)字段,還可以約束多字段的組合。 如 : create table test (host int(8) not null,port int(8) not null,primary key(host,key));這樣的話使表可以允許主機(jī)ip或port端口重復(fù),但不允許ip和port同時重復(fù)。 可以看出,primary key 可以在定義列的同時一起定義,也可以在最后定義。 index: 把某列拿出來,單獨(dú)儲存,這樣以后查找會直接定位查找,這樣查詢速度比較快。語句: INDEX index_name(name);為name這個字段創(chuàng)建了名為index_name的索引。也可以INDEX (name),這樣的話為name這個字段創(chuàng)建了名為name的索引。 foreign key:(創(chuàng)建時,自動加了index) 在mysql中使用外鍵的表類型必須是innodb。其他類型的會自動忽略外鍵這個功能。設(shè)置了外鍵,則想向該外鍵插入值的時候,必須是被參照的鍵存在的值。 自動更新和刪除值: 在定義外鍵的語句后加入 ON DELETE :XXX .XXX有四個值:cascade 刪除包含與已刪除鍵值有參照關(guān)系的所有記錄、set null 刪除包含與已刪除鍵值有參照關(guān)系的所有記錄并用null值來替換、restricr 拒絕刪除要求(系統(tǒng)默認(rèn))、no action 什么也不做。ON DELETE restricr 系統(tǒng)默認(rèn)。 在定義外鍵的語句后加入 ON UPDATE: xxxx.XXX有四個值 (和on delete一樣。):cascade 更新包含與更新鍵值有參照關(guān)系的所有記錄、set null 更新包含與已更新鍵值有參照關(guān)系的所有記錄并用null值來替換、restricr 拒絕更新要求(系統(tǒng)默認(rèn))、no action 什么也不做。ON update restricr 系統(tǒng)默認(rèn)。 外鍵定義,一般是在最后定義,如: constraint fk_name foregin key (fk_id_colum) references table2 (id); 4.2 表的類型。 MyISM ISAM HEAP innoDB..等等。系統(tǒng)默認(rèn)的是 innoDB.只有innoDB才有外鍵功能。 5、修改表: 基本命令:alter table table_name action action1,action action 2,....; 添加/刪除列:alter table member add id varchr(9) not null;alter table drop name; 添加/刪除的主鍵:alter table member add primary key(id);alter table t4 drop primary key; 設(shè)置/刪除的默認(rèn)值約束:alter table member alter age set/drop default 1; 加入/刪除索引:alter table t1 add/drop index (index_id) ; 添加/刪除unique鍵:alter table t4 add unique key (myname);alter table t1 drop index id ;(創(chuàng)建unique時,自動加了index)。 如果這個鍵已經(jīng)存在相同的值,可加入ignore來刪除相同數(shù)據(jù),并保留第一條數(shù)據(jù)。 alter ignore table user change name name int(1) unique; 設(shè)置新加入列的位置:alter table member add age int(8) AFTER id/FIRST; 重新命名表:alter table member rename to new_name; //修改已經(jīng)存在字段的名字和數(shù)據(jù)類型:alter table member Change name New_name char(9) auto_increment unique; 添加/刪除auto_increment:(需要是鍵)。alter movie change id int(3) not null auto_increment ;ALTER TABLE t1 CHANGE id id INT(3) NOT NULL ; 添加/刪除外鍵:alete table t1 add constraint fk_name foregin key (fk_id_colum) references table2 (id); alter table t2 drop foreign key fk_name;注意你刪除的是index還是foreign key name;你創(chuàng)外鍵的時候,他會自動幫你加入index。要搞清楚那個名是index的name還是foreign key的name; action可選:ADD 加入新的列或新的約束,DROP ,ALTER,CHANGE 只能修改已經(jīng)存在的列名字和類型和已存在的 not null,及其auto _increment約束,但不能修改已存在的 primary key 和unique這兩個主鍵,但可以新加入這兩個主鍵.MODIFY 和change區(qū)別很小,modify不會修改字段名。 6.刪除表: drop table t1; drop table if exists t1; 7.獲得數(shù)據(jù)庫、表、字段和索引的信息。 show databases; show tables; describe t1; show index from t1; show create table t9;. 五、使用數(shù)據(jù): 5.1 /插入記錄: insert into table_name (name,id) values ('ab',123),('abccc',456); 可以插入計算或者函數(shù):如 insert into grade (math,english,total,time) values (50,50,math+english,now()); 相當(dāng)于math=50,english=50了。 可以使用default值,如 insert into grade (math,english) values(50,default); 使用autoincrement字段:插入的時候,不用寫出來,系統(tǒng)會自動添加。insert into grade (math,english) values(50,default);它的id會自動加的、 使用unique字段:插入的值如果已經(jīng)存在,則可以這樣處理:在insert 后加入 action. action有三個取值:ignore(忽略此操作,什么都不干) / on duplicate key update(只更新部分)/replace(刪除舊的記錄,新的完全取代) 如:insert ignore into t1(id,name)values(1,'q'); insert into t1(id,name)values(1,'q')on duplicate key uodate name='new'; replace into t1 values (1,'new') ; 使用null值::insert ignore into t1(id,name)values(1,null); 5.2/更新記錄: update table-name set id=1,name=2,where age=18; 跟插入記錄差不多,插入記錄用法都可以在更新記錄這里用。 5.3刪除記錄。 delete from table_name where id = 1; 刪除表的所有記錄: delete from table-name; 5.4檢索記錄: select col1,col2 from table-name where id =1; col1,col2 名字是什么 ,返回結(jié)果的第一行就顯示什么名字。 可以使用內(nèi)函數(shù): 如:select max(math),min(english) from grade; 若:select max(math),english from grade; 則返回math和english的最大值,而不是一個最大值+一堆數(shù)據(jù)。 5.5為表和列取別名: select mmmmm as m, qqqqqq as q from tabel_name; selcte m , q from table_name as t where t.m >10; 5.6對查詢結(jié)果進(jìn)行分組: select age from student group by age; 聚合函數(shù)可以英語有記錄組成的每個單獨(dú)組。 selecte age ,count(*) from student group by age; selecte age ,avg(age) from student group by age; 可以想group by 添加asc和desc關(guān)鍵字來以升序和降序為結(jié)果集排序。 selecte age ,avg(age) from student group by age desc/asc; 5.7 對查詢結(jié)果進(jìn)行排序。 ( 你不加order by 來排序的話,數(shù)據(jù)會顯示實際在表中的順序) select idt3 from t3 order by idt3 desc{降序}/asc(升序); 同時進(jìn)行多個排序: select name,age from t1 order by name desc,age asc.
信息發(fā)布:廣州名易軟件有限公司 http://www.jetlc.com
|