When I try to run this statement:
CREATE TABLE 'score_table'
(
'name' text NOT NULL,
'email' text NOT NULL,
'company' text NOT NULL,
'score_total' bigint(11) NOT NULL,
'score_string' longtext NOT NULL,
'id' int(11) NOT NULL auto_increment,
'date' timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY ('id')
)
ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
I get this error:
#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 ''score_table'
('name' text NOT NULL, 'email' text NOT NULL, 'company' text NOT N' at line 1
And I have no idea what's wrong, any help would be greatly appreciated!
Table and field names need to be put in backticks instead of single quotes (or no quotes at all):
CREATE TABLE `score_table`.....
Use backticks and not single quotes:
Your SQL statement should be:
CREATE TABLE score_table (
`name` text NOT NULL,
`email` text NOT NULL,
`company` text NOT NULL,
`score_total` bigint(11) NOT NULL,
`score_string` longtext NOT NULL,
`id` int(11) NOT NULL auto_increment,
`date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`))
ENGINE= MyISAM DEFAULT CHARSET=latin1;
You shouldn't enclose your table name in single quotes like that.
CREATE TABLE `score_table` (`name` text NOT NULL, `email` text NOT NULL, `company` text NOT NULL, `score_total` bigint(11) NOT NULL, `score_string` longtext NOT NULL, `id` int(11) NOT NULL auto_increment, `date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
As mentioned earlier table name and field names must not be in Quotes so avoid them i edited your query try this and also, if you are using Magic Quotes avoid using them as well(just avoid Magic quotes in your current scenario to find if you are doing it all correct.)
CREATE TABLE score_table (name text NOT NULL, email text NOT NULL, company text NOT NULL, score_total bigint(11) NOT NULL, score_string longtext NOT NULL, id int(11) NOT NULL auto_increment, date timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (id) ) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
Related
When payment happen, sometimes its captured double entry in table.
I want to ignore double entry capture so i want to insert records when these created, user_id, amount fields should be unique.
How do i make it ? Below is my table.
CREATE TABLE `transactions` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(20) NOT NULL,
`project_id` int(20) DEFAULT NULL,
`foreign_id` int(20) NOT NULL,
`class` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`transaction_type_id` int(20) DEFAULT NULL,
`amount` float(10,2) NOT NULL,
`description` text COLLATE utf8_unicode_ci,
`payment_gateway_id` int(20) DEFAULT NULL,
`gateway_fees` float(10,2) NOT NULL,
`is_old` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=266 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
To strictly answer your question, you create a unique composite key on the combination of those 3 columns. That way no two rows can exist with a combination of the 3 of them in the composite index.
CREATE TABLE `transactions2` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(20) NOT NULL,
`project_id` int(20) DEFAULT NULL,
`foreign_id` int(20) NOT NULL,
`class` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`transaction_type_id` int(20) DEFAULT NULL,
`amount` float(10,2) NOT NULL,
`description` text COLLATE utf8_unicode_ci,
`payment_gateway_id` int(20) DEFAULT NULL,
`gateway_fees` float(10,2) NOT NULL,
`is_old` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
unique key(created,user_id,amount) -- <------------------- right here
);
insert some data to test it:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 12:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- inserts fine (above)
Try it again with exactly the same data:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 12:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- error 1062: Duplicate entry
-- change it barely:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 13:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- inserts fine
Also, use ENGINE=INNODB. Read about that Here.
Please read Mysql multi column indexes a.k.a. composite indexes.
Lastly, the concept of what you are talking about is not far off from Insert on Duplicate Key Update. Just throwing that reference out there for you.
You can achieve the same using ,handling at the time of inserting,
You can try WHERE NOT EXISTS with INSERT.
Something like this.(You need to specify column name who has NOT NULL constraint,i missed all those columns)
INSERT INTO table_name(`created`,`user_id`,`amount`) VALUES =
'$created','$user_id','$amount'
WHERE NOT EXISTS
(SELECT *
FROM table_name
WHERE created ='$created' AND user_id='$user_id' AND amount='$amount')
Hope this helps.
I exported the sql from one phpmyadmin db and imported same at another phpmyadmin db.
The create table statement is as follows
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) DEFAULT NULL,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The insert sql file also I have exported it as follows
INSERT INTO `h_stats` (`Id`, `Category`, `Success`, `Failure`, `ThCount`, `OCount`, `HDate`, `Count`) VALUES
(13, 'Hits', 31303, 8828, 8893, 30372, '2015-04-07', 40151),
Note :
When I try to insert new rows at the place of id NULL value is coming.
Can anyone suggest me what to change in create table statement.
You need to define the id as primary key and auto-increment .
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So when you add null for id, the auto-increment will come in to play and set it the next value in insert.
Note that this will enforce you to have unique value for id so while doing insert leave id into the insert statement. Something as
insert into h_stats (`Category`,`Success` ...) values ( ....);
Im using phpmyadmin and I have a table categories with 4 fields:
-> id
-> id_father_category (I put this with predefined: NULL) and I also put a checkbox Null
-> name
-> content
I want to give null values to my id_father_category field.
But Im having this error:
Warning: #1366 Incorrect integer value: '' for column 'id_father_category field' at row 1
And my field id_father_category stays automatically with value 0, and I dont want that.
Somebody there knows how I can solve this problem?
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Hope this will solve your problem...
This is an addition to #Nirjhor's answer.
ALTER TABLE categories MODIFY id_father_category INT(11) DEFAULT NULL;
or you can recreate the whole thing:
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Setting the default value to NULL will handle your issue as well as set the field to NULL. One thing to note here is to make sure you don't have some weird index on that field cause that can also create an issue.
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT,
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
INSERT INTO `contracts` (`contractId`, `username`, `title`, `contractText`, `date`, `contractState`) VALUES
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(32) CHARACTER SET utf8 NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
`email` varchar(20) CHARACTER SET utf8 NOT NULL,
`usergroup` tinyint(1) NOT NULL COMMENT,
`name` varchar(32) CHARACTER SET utf8 NOT NULL,
`lastname` varchar(32) CHARACTER SET utf8 NOT NULL,
`state` tinyint(4) NOT NULL COMMENT,
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci;
When running this query on the SQL server, I am getting the following error:
MySQL said: Documentation
#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 '
title varchar(20) CHARACTER SET utf8 NOT NULL,
contractText text CHAR' at line 13
why this is coming up?
You did not add a comment to the username column.
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT ,
add one here---------------------------------------^
or remove the COMMENT keyword
in this line
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
add some comment in for COMMENT or Remove Comment. Below is working code.
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3-
panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
Just fix your table creation to add explicit comment or remove COMMENT keyword if it is not needed.
Try modify it to become:
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'add explicit comment her',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
Remove comment from this line:
`username` varchar(32) CHARACTER SET utf8 NOT NULL , //COMMENT
I have a very strange problem here. I am exporting a query through phpmyadmin and then when I run it using mysql_query I get an error saying that syntax of the query isn't correct.
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 TABLE IF NOT EXISTS `wp_cfs_events` ( `event_id` int(11) NOT NULL AUTO_' at line 14
The same query runs perfectly fine using phpmyadmin. What could be the problem?
Here is the SQL query
CREATE TABLE IF NOT EXISTS `wp_cfs_certificates` (
`certificate_id` varchar(8) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`certificate_price` enum('40','50') NOT NULL,
`certificate_order_time` datetime DEFAULT NULL,
`is_used` tinyint(1) DEFAULT '0',
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_events` (
`event_id` int(11) NOT NULL AUTO_INCREMENT,
`event_name` varchar(60) DEFAULT NULL,
`event_desc` varchar(2048) DEFAULT NULL,
`event_date` date DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `wp_cfs_events` (`event_id`, `event_name`, `event_desc`, `event_date`) VALUES
(1, 'Culture Night', 'all about culture', '2013-05-16'),
(2, 'Sports Night', 'all about sports', '2013-05-31'),
(3, 'Random Night', 'randomness overloaded', '2013-06-15'),
(4, 'Winter Fest', 'the awesome winter fest', '2013-11-20'),
(5, 'Archived Event', 'this event has been occured in past', '2013-04-02');
CREATE TABLE IF NOT EXISTS `wp_cfs_parents` (
`parent_id` int(11) NOT NULL,
`parent_name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_registrations` (
`registration_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`certificate_id` varchar(8) NOT NULL,
`event_id` varchar(45) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`child_name` varchar(45) DEFAULT NULL,
`child_age` int(11) DEFAULT NULL,
PRIMARY KEY (`registration_id`),
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `wp_cfs_registrations`
ADD CONSTRAINT `certificate_id` FOREIGN KEY (`certificate_id`) REFERENCES `wp_cfs_certificates` (`certificate_id`) ON DELETE NO ACTION ON UPDATE CASCADE;
Its because mysql_query() doesn't support multiple queries
Its mentioned in the official PHP documentation
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.