一、創(chuàng)建數(shù)據(jù)庫(kù):
| create data data _name; | 
php中創(chuàng)建數(shù)據(jù)庫(kù)的兩種方法:(mysql_create_db(),mysql_query())
| $conn = mysql_connect(“l(fā)ocalhost”,”username”,”password”) or die ( “could not connect to localhost”); mysql_create_db(“data _name”) or die (“could not create data ”); $string = “create data data _name”; mysql_query( $string) or die (mysql_error()); | 
二、選定數(shù)據(jù)庫(kù)
在創(chuàng)建表之前,必須要選定要?jiǎng)?chuàng)建的表所在的數(shù)據(jù)庫(kù)
選定數(shù)據(jù)庫(kù):
通過(guò)命令行客戶(hù)端:
| use data _name | 
通過(guò)
| php: mysql_select_db() | 
| $conn = mysql_connect(“l(fā)ocalhost”,”username”,”password”) or die ( “could not connect to localhost”); mysql_select_db(“test”,$conn) or die (“could not select data ”); | 
三、創(chuàng)建表
| create table table_name | 
如:
| 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) ) | 
在命令行客戶(hù)端需要鍵入整個(gè)命令
在php中使用,mysql_query()函數(shù)
如:
| $conn = mysql_connect(“l(fā)ocalhost”,”username”,”password”) or die ( “could not connect to localhost”); mysql_select_db(“test”,$conn) or die (“could not select data ”); $query = “create table my_table (col_1 int not null primary key, col_2 text )”; mysql_query($query) or die (mysql_error()); | 
四、創(chuàng)建索引
| index index_name(indexed_column) | 
五、表的類(lèi)型
ISAM MyISAM BDB Heap
聲明表類(lèi)型的語(yǔ)法:
| create table table_name type=table_type (col_name column attribute); | 
默認(rèn)使用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 把新列放在已經(jīng)存在的列的后面
例如: