顯示數(shù)據(jù)庫或表:
因為努力和真誠,有更多的客戶和我們聚集在一起,為了共同目標,成都創(chuàng)新互聯(lián)在工作上密切配合,從創(chuàng)業(yè)型企業(yè)到如今不斷成長,要感謝客戶對我們的高要求,讓我們敢于面對挑戰(zhàn),才有今天的進步與發(fā)展。從網(wǎng)站到小程序開發(fā),軟件開發(fā),重慶App定制開發(fā),十載企業(yè)網(wǎng)站建設(shè)服務(wù)經(jīng)驗,為企業(yè)提供網(wǎng)站設(shè)計,網(wǎng)站改版維護一條龍服務(wù).為企業(yè)提供成都全網(wǎng)營銷,定制開發(fā),原創(chuàng)設(shè)計,十載品質(zhì),值得您的信賴.
showdatabases;//然后可以usedatabase_name;
showtables;
更改表名:
altertabletable_namerenamenew_t;
添加列:
altertabletable_nameaddcolumnc_ncolumnattributes;
刪除列:
altertabletable_namedropcolumnc_n;
創(chuàng)建索引:
altertablec_tableaddindex(c_n1,c_n2);
altertablec_tableadduniqueindex_name(c_n);
altertablec_tableaddprimarykey(sid);
刪除索引:
altertablec_tabledropindexc_n1;
更改列信息:
alter tablet_tablechangec_1c_1varchar(200);
altertablet_tablemodify1c_1varchar(200);
insert插入語句:
insertintotable_name(c_1,c_2)
values('x1',1);
update語句:
update table_namesetc_1=1wherec_2=3;
刪除數(shù)據(jù)庫或者表:
droptabletable_name;
dropdatabasedatabase_name;//使用mysql_drop_db()可以刪除的.
你做好程序以后,把數(shù)據(jù)庫導出成sql文件
1、連接數(shù)據(jù)庫
2、讀取這個sql文件里的sql語句,并執(zhí)行
3、生成一個數(shù)據(jù)庫連接參數(shù)的php文件
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
if?(mysql_query("CREATE?DATABASE?my_db",$con))
{
echo?"Database?created";
}
else
{
echo?"Error?creating?database:?"?.?mysql_error();
}
mysql_close($con);
?
?php
class?ReadSql?{
//數(shù)據(jù)庫連接
protected?$connect?=?null;
//數(shù)據(jù)庫對象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql語句集
public?$sqlArr?=?array();
public?function?__construct($host,?$user,?$pw,?$db_name)?{
$host?=?empty($host)???C("DB_HOST")?:?$host;
$user?=?empty($user)???C("DB_USER")?:?$user;
$pw?=?empty($pw)???C("DB_PWD")?:?$pw;
$db_name?=?empty($db_name)???C("DB_NAME")?:?$db_name;
//連接數(shù)據(jù)庫
$this-connect?=?mysql_connect($host,?$user,?$pw)?or?die("Could?not?connect:?"?.?mysql_error());
$this-db?=?mysql_select_db($db_name,?$this-connect)?or?die("Yon?can?not?select?the?table:"?.?mysql_error());
}
//導入sql文件
public?function?Import($url)?{
$this-sqlFile?=?file_get_contents($url);
if?(!$this-sqlFile)?{
exit("打開文件錯誤");
}?else?{
$this-GetSqlArr();
if?($this-Runsql())?{
return?true;
}
}
}
//獲取sql語句數(shù)組
public?function?GetSqlArr()?{
//去除注釋
$str?=?$this-sqlFile;
$str?=?preg_replace('/--.*/i',?'',?$str);
$str?=?preg_replace('/\/\*.*\*\/(\;)?/i',?'',?$str);
//去除空格?創(chuàng)建數(shù)組
$str?=?explode(";\n",?$str);
foreach?($str?as?$v)?{
$v?=?trim($v);
if?(empty($v))?{
continue;
}?else?{
$this-sqlArr[]?=?$v;
}
}
}
//執(zhí)行sql文件
public?function?RunSql()?{
foreach?($this-sqlArr?as?$k?=?$v)?{
if?(!mysql_query($v))?{
exit("sql語句錯誤:第"?.?$k?.?"行"?.?mysql_error());
}
}
return?true;
}
}
//范例:
header("Content-type:text/html;charset=utf-8");
$sql?=?new?ReadSql("localhost",?"root",?"",?"log_db");
$rst?=?$sql-Import("./log_db.sql");
if?($rst)?{
echo?"Success!";
}
?
1:可以在自己在文本里面寫好相關(guān)的建表語句,然后用phpmyadmin之類的工具導入到你的數(shù)據(jù)庫,然后在通過程序連接數(shù)據(jù)庫,插入,修改,刪除,查詢。一般開發(fā)流程都是在程序開發(fā)之前就需要規(guī)劃數(shù)據(jù)表的結(jié)構(gòu)的。(個人和企業(yè)自身用這樣就可以了)
2:就像你說的那樣創(chuàng)建一個文件專門用來創(chuàng)建數(shù)據(jù)庫和表,也就是程序的安裝模塊,當然也是需要在文本中寫相關(guān)的sql語句的,之后通過程序?qū)氲綌?shù)據(jù)庫去,創(chuàng)建好了之后可以把他刪除。(這種一般給別人開發(fā)的時候用,像那些開源的cms都是這樣的)
$link = mysql_pconnect("localhost","root","");
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "成功";
} else {
echo "失敗" . mysql_error() . "\n";
}
注:不提倡使用函數(shù)mysql_create_db()。最好用mysql_query()來提交一條SQLCREATEDATABASE語句來替代。
當前題目:php建數(shù)據(jù)庫語句 php數(shù)據(jù)庫怎么創(chuàng)建
轉(zhuǎn)載來于:http://www.chinadenli.net/article32/dddsjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站、商城網(wǎng)站、動態(tài)網(wǎng)站、建站公司、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)