2

I am trying to create a table in my database, accountdb. My query to create the table is:

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

After that when I am executing the above query, the database is created, but there is an error in creating the table. The error is as follows:

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

How can I fix this error?

Brian
  • 14,610
  • 7
  • 35
  • 43

3 Answers3

4

Here is the correct query:

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

When the datatype is double, float, or decimal, you need to specify the decimal places.

Correct syntax to create a double datatype column:

double(D,M);

M is the total number of digits and D is the number of digits following the decimal point.

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

I hope this helps you.

Brian
  • 14,610
  • 7
  • 35
  • 43
Oli Soproni B.
  • 2,774
  • 3
  • 22
  • 47
  • +1 for talking about precision but I would suggest that tauqeer also consider this: http://stackoverflow.com/a/224866/149076 ... for dealing with currency storage and manipulation. – Jim Dennis Feb 08 '15 at 06:36
1
CREATE TABLE IF NOT EXIST asset( 
id int(16) auto_increment primary key
,TotBalance double(35)
,creditAmnt double(35)
,debitAmnt double(35)
);
Ega
  • 55
  • 19
  • +1 for "IF NOT EXIST" as a robust way for apps to ensure that tables exist without risk of disturbing extant database contents. Programmers new to DBMS coding should take this to heart and also consider learning about "migrations" frameworks to ensure that their schema is maintained in sync with their code. – Jim Dennis Feb 08 '15 at 06:32
  • the issue with the code was that you have to pass two parameters for the data type double. just double(35) won't work.you have to pass something like double(35,2) where 2 is the number of decimal places. – Chatz Feb 08 '15 at 06:35
1

CREATE TABLE assets ( id 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

Deepak Keynes
  • 311
  • 2
  • 16