2015-02-08 21 views
2

我試圖在我的數據庫中創建一個表格accountdb。我查詢創建表:如何在數據庫中創建表格

$query1="CREATE TABLE asset(id int(16) auto_increment primary key,TotBalance double(35),creditAmnt double(35),debitAmnt double(35))"; 

之後,當我在執行上面的查詢,創建數據庫,但在創建表的錯誤。錯誤如下:

error creating tableYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '),creditAmnt double(35),debitAmnt double(35))' at line 1 

我該如何解決這個錯誤?

+0

請幫助我,因爲我是新的PHP。 – 2015-02-08 05:54:41

回答

4

這裏是正確的查詢:

CREATE TABLE asset(
     id int(16) auto_increment primary key NOT NULL, 
     TotBalance double(35,3), 
     creditAmnt double(35,3), 
     debitAmnt double(35,3) 
    ); 

當一個數據類型爲雙,浮法,或十進制,你需要指定的小數位。

正確語法來創建雙數據類型列:

double(D,M); 

M是數字的總數,並且是D跟隨在小數點的數字的數量。

參見:http://code.rohitink.com/2013/06/12/mysql-integer-float-decimal-data-types-differences/

我希望這可以幫助你。

+0

+1爲談論精度,但我會建議tauqeer也考慮這個:http://stackoverflow.com/a/224866/149076 ...處理貨幣存儲和操縱。 – 2015-02-08 06:36:22

1
CREATE TABLE IF NOT EXIST asset( 
id int(16) auto_increment primary key 
,TotBalance double(35) 
,creditAmnt double(35) 
,debitAmnt double(35) 
); 
+0

對於「IF NOT EXIST」,+1是一種強大的應用程序確保表存在沒有干擾現存數據庫內容風險的方式。 DBMS編碼新手的程序員應該考慮到這一點,並考慮瞭解「遷移」框架,以確保他們的模式與他們的代碼保持同步。 – 2015-02-08 06:32:05

+0

代碼的問題是您必須爲數據類型double傳遞兩個參數。只有double(35)將不起作用。你必須傳遞像double(35,2)這樣的數字,其中2是小數位數。 – Chatz 2015-02-08 06:35:40

1

CREATE TABLE assetsid INT(16)NOT NULL AUTO_INCREMENT, Tot_balance BIGINT(20)NOT NULL, Credit_Amt BIGINT(20)NOT NULL, Debit_Amt BIGINT(20)NOT NULL, PRIMARY KEY(id) )ENGINE = MyISAM DEFAULT CHARSET = latin1