天天看點

MySQL使用初步—mysql資料庫的基本指令

一、建立資料庫:

create database database_name;

php中建立資料庫的兩種方法:(mysql_create_db(),mysql_query())

$conn = mysql_connect(“localhost”,”username”,”password”) or

die ( “could not connect to localhost”);

1.

mysql_create_db(“database_name”) or

die (“could not create database”);

2.

$string = “create database database_name”;

mysql_query( $string) or

die (mysql_error());

MySQL使用初步—mysql資料庫的基本指令

二、標明資料庫

在建立表之前,必須要標明要建立的表所在的資料庫

標明資料庫:

通過指令行用戶端:use database_name

通過php: mysql_select_db()

mysql_select_db(“test”,$conn) or

die (“could not select database”);

三、建立表

create table table_name

如:

(

column_1 column_type column attributes,

column_2 column_type column attributes,

column_3 column_type column attributes,

primary key (column_name),

index index_name(column_name)

)

在指令行用戶端需要鍵入整個指令

在php中使用,mysql_query()函數

$query = “create table my_table (col_1 int not null primary key,

mysql_query($query) or

四、建立索引

index index_name(indexed_column)

五、表的類型

isam myisam bdb heap

聲明表類型的文法:

create table table_name type=table_type

(col_name column attribute);

預設使用myisam

六、修改表

alter table table_name

更改表名

alter table table_name rename new_table_name

或者(高版本中)

rename table_name to new_table_name

添加和删除列

添加列:alter table table_name add column column_name colomn attributes

例如: alter table my_table add column my_column text not null

first 指定插入的列位于表的第一列

after 把新列放在已經存在的列的後面

删除列:alter table table_name drop column column name

添加和删除索引:

alter table table_name add index index_name (column_name1,column_name2,……)

alter table table_name add unique index_name (column_name)

alter table table_name add primary key(my_column)

alter table table_name drop index index_name

如:alter table_name test10 drop primary key

更改列定義:

用change或是modify指令可以更改列的名稱或是屬性。要更改列的名稱,還必須重新定義列的屬性。例如:

alter table table_name change original_column_name new_column_name int not null

注意:必須要重新定義列的屬性!!!

alter table table_name modify col_1 clo_1 varchar(200)

七、向表中輸入資訊(insert)

insert into table_name (column_1,column_2,column_3,…..)

values (value1,value2,value3,……)

如果要存入字元串,則需要使用單引号“’”将字元串括起來,但是需要注意字元的轉意

如:insert into table_name (text_col,int_col) value (’hello world’,1)

需要轉義的字元有:單引号’ 雙引号” 反斜杠 百分号% 下劃線_

可以連續使用兩個單引号轉義單引号

八、updata語句

updata table_name set col__1=vaule_1,col_1=vaule_1 where col=vaule

where部分可以有任何比較運算符

table folks

id fname iname salary

1 don ho 25000

2 don corleone 800000

3 don juan 32000

4 don johnson 44500

updata folks set fname=’vito’ where id=2

updata folks set fname=’vito’ where fname=’don’

updata folks set salary=50000 where salary<50000

九、删除表、資料庫

drop table table_name

drop database database_name

在php中可以通過mysql_query()函數使用drop table指令

在php中删除資料庫需要使用mysql_drop_db()函數

十、列出資料庫中所有可用表(show tables)

注意:使用該命前必須先標明資料庫

在php中,可以使用mysql_list_tables()得到表中的清單

十一、檢視列的屬性和類型

show columns from table_name

show fields from table_name

使用mysql_field_name()、mysql_field_type()、mysql_field_len()可以得到類似資訊!

十二、基本的select語句

要求指出進行選擇的表,以及要求的列名稱。若要標明所有的列,可用*代表所有的字段名

select column_1,column_2,column_3 from table_name

或者

select * from table_name

用mysql_query()可向mysql發送查詢

十三、where子句

限制從查詢(select)傳回的記錄行

select * from table_name where user_id = 2

如果要對存儲字元串(char、varchar等類型)的列進行比較,就需要在where子句中用單引号把要比較的字元串括起來

如:select * from users where city = ‘san francisco’

通過向where子句添加and或是or,可以一次比較幾個運算符

select * from users where userid=1 or city=’san francisco’

select 8 from users where state=’ca’ and city=’san francisco’

注意:空值不能和表中的任何運算符比較,對于空值,需要使用is null或是is not null謂詞

select * from users where zip!=’1111′ or zip=’1111′ or zip is null

如果要找到包含任何值(除空值以外)的所有記錄,可以

select * from table_name where zip is not null

十四、使用distinct

當使用distinct時,mysql引擎将删除有一樣結果的行。

select distinct city,state from users where state=’ca’

十五、使用between

使用between可以選擇在某個範圍内的值,between可用于數字,日期,文本字元串。

select * from users where lastchanged between 20000614000000 and 20000614235959

select * from users where lname between ‘a’ and ‘m’

十六、使用in/not in

若某列可能傳回好幾個可能的值,就可以使用in謂詞

select * from users where state=’ri’ or state=’nh’ or state=’vt’ or state=’ma’ or state=’me’

如果要達到相同的結果,但結果集相反,可使用not in 謂詞

select * from user where state not in (‘ri’,'nh’,'vt’,'ma’,'me’)

十七、使用like

如果需要使用通配符,則要使用like

select * from users where fname like ‘dan%’ %比對零個字元

select * from users where fname like ‘j___’ 比對以j開頭的任意三字母詞

mysql中like不區分字母大小寫

十八、order by

order by語句可以指定查詢中傳回的行的順序,可對任意列類型排序,通過在末尾放置asc或是desc以設定按升序或是降序排列,如果不設定,預設使用asc

select * from users order by lname,fname

可以按照需要根據任意多的列排序,也可以混合使用asc和desc

select * from users order by lname asc, fname desc

十九、limit

limit限制從查詢中傳回的行數,可以指定開始的行數和希望傳回的行數

得到表中的前5行:

得到表的第二個5行:

二十、group by 與聚合函數

使用group by後mysql就能建立一個臨時表,記錄下符合準則的行與列的所有資訊

count() 計算每個集合中的行數

select state,count(*) from users group by state

*号訓示應該計算集合中的所有行

select count(*) from users

計算表中所有的行數

可以在任何函數或列名後使用單詞as,然後指定一個作為别名的名稱。如果需要的列名超過一個單詞,就要使用單引号把文本字元串括起來

sum() 傳回給定列的數目

min() 得到每個集合中的最小值

max() 得到每個集合中的最大值

avg() 傳回集合的品均值

having

限制通過group by顯示的行,where子句顯示在group by中使用的行,having子句隻限制顯示的行。

二十一、連接配接表

在select語句的from部分必須列出所有要連接配接的表,在where部分必須顯示連接配接所用的字段。

select * from companies,contacts where companies.company_id=contacts.company_id

當對一個字段名的引用不明确時,需要使用table_name.column_name文法指定字段來自于哪個表

二十二、多表連接配接

在select後面添加額外的列,在from子句中添加額外的表,在where子句中添加額外的join參數–>