How to create a table in a database - php

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?

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.

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

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

Related

MariaDB create database relational tables

I created 2 tables as follows:
$sql = "CREATE TABLE users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(25) UNIQUE NOT NULL,
password VARCHAR(256) NOT NULL,
first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,
email varchar(50) UNIQUE NOT NULL
date_created VARCHAR(25) NOT NULL,
date_modified VARCHAR(25) NOT NULL
)";
This table was created successfully.
I tried to create a 2nd table as follows:
$sql = "CREATE TABLE users_performance (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT(11) REFERENCES users(id) UNIQUE NOT NULL,
performance text NOT NULL
)";
This failed with following error:
Error creating table: You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'NOT NULL, performance text NOT NULL )' at line 3
Now the error line is:
user_id INT(11) REFERENCES users(id) UNIQUE NOT NULL
I have scratched through Maria DB manual but can't quickly get a solution.
I know this syntax works fine on postgresql. Why is it not working on mysqli
I prefer to create 2 separate tables for future changes, etc.
Thanks
First, check that you are using MariaDB 10.5 or later. Inline foreign keys are not implemented in older versions of MariaDB.
Then, move the UNIQUE NOT NULL before the foreign key option. The order of column options is important.
Also, make sure the data type of the foreign key column is the same as the column it references. In this case, you need to make it INT UNSIGNED. The "length" option (11) doesn't matter, and you can omit it.
user_id INT UNSIGNED UNIQUE NOT NULL REFERENCES users(id),
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=54964388cd0d880169c0332934c13b3d

Sql query will give weird 'unrecognized data type (near key) error in mysql

I haven't worked with php and mysql for a long time and my return isn't so well-received by the platform.
I created tables using phpmyadmin and then I used the code below the generate scripts for my tables:
show create table recipes
I dropped the tables and then started to use the generated scripts to recreate my tables, only to see that they are giving me syntax errors. This is the generated script.
CREATE TABLE recipes (
 id int NOT NULL AUTO_INCREMENT,
 name varchar(50) NOT NULL,
 serving varchar(50) NOT NULL,
 description text NOT NULL,
 image_url varchar(100) NOT NULL,
 calories float DEFAULT NULL,
 tag varchar(100) NOT NULL,
 meal int NOT NULL,
 user_id int NOT NULL,
 PRIMARY KEY (id),
 KEY user_id (user_id),
 FULLTEXT KEY name_full_text (name),
 FULLTEXT KEY tag_full_text (tag),
 CONSTRAINT recipes_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
What I want from my table is that it use id as the primary key, user_id as the foreign key to the users table (which is already created), and I need two FULLTEXT indexes on two of the columns.
The strange thing is that I am getting syntax errors in lines 11, 12, and 13. With the main one being unrecognized data type. (near key). I am also getting a comma or a closing bracket was expected (near name_full_text). Then again,phpmyadmin` generated this query itself and I don't know why it is not accepting it.
Any Suggestions?

php mysqli creat index error

I use PHP mysqli to create a table and index.But I got this error.
I have already run these sql in mysql client and it worked well.
$creatTableSql = <<<EOF
CREATE TABLE IF NOT EXISTS duplicate_attachment(
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
md5 VARCHAR(32) NOT NULL,
filesize INT(11),
ids TEXT NOT NULL
);
CREATE UNIQUE INDEX duplicate_attachment_id_uindex ON duplicate_attachment (id);
CREATE UNIQUE INDEX duplicate_attachment_md5_uindex ON duplicate_attachment (md5);
EOF;
$mysqli->query($creatTableSql);
var_dump($mysqli->error);
string(226) "You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'CREATE UNIQUE INDEX duplicate_attachment_id_uindex ON
duplicate_attachment (id);' at line 8"
This is not a single query but a set of multiple queries.
You should run them one by one.
$mysqli->query("CREATE TABLE IF NOT EXISTS duplicate_attachment(
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
md5 VARCHAR(32) NOT NULL,
filesize INT(11),
ids TEXT NOT NULL
)";
$mysqli->query("CREATE UNIQUE INDEX duplicate_attachment_id_uindex ON duplicate_attachment (id)";
$mysqli->query("CREATE UNIQUE INDEX duplicate_attachment_md5_uindex ON duplicate_attachment (md5)";

what is use of 'KEY' in mysql , it is not allowing me edit record in phpmyadmin

Here is my database mapping table definition,
you can try this, when I create this table and add some records in to it, it is not let me edit or delete the records by phpmyadmin although by query it should be possible,
CREATE TABLE IF NOT EXISTS `map2` (
`map_table_a` varchar(25) DEFAULT NULL,
`map_id_a` int(10) DEFAULT NULL,
`map_table_b` varchar(25) DEFAULT NULL,
`map_id_b` int(10) DEFAULT NULL,
KEY `map_table_b` (`map_table_b`,`map_id_b`),
KEY `map_table_a` (`map_table_a`,`map_id_a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I don't know reason behind this behavior
Depending on your phpMyAdmin version, you should see this error message:
While you do have KEY columns, you have no PRIMARY or UNIQUE columns defined. This is why phpMyAdmin cannot edit your data - it has no way to be sure it is editing the correct row.
Suggested solution: Add the following into your table definition, preferably as the first column:
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY

mysql problems with full text and type

I'm using xampp software
In that I opened mysql my admin and paste that sql code. That shows an error like
#1214 - The used table type doesn't support FULLTEXT indexes
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE = MYISAM' at line 7
CREATE TABLE code (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
chapter TINYINT UNSIGNED NOT NULL,
code TEXT NOT NULL,
FULLTEXT (title,code)
) TYPE = MYISAM;
Try this instead:
CREATE TABLE code
(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
chapter TINYINT UNSIGNED NOT NULL,
code TEXT NOT NULL,
FULLTEXT (title,code)
) ENGINE = MyISAM;

Categories