So I have a table that is laid out according to:
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Now when I insert with
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES( NULL, ~values);
This returns an error that dealership_id cannot be NULL error 1048.
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(0, ~values);
This returns an error that dealership_id already exists for value 0.
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(default, ~values);
This returns an error that dealership_id has no default value error 1364.
When I simply say screw it, and ommit dealership_id
INSERT INTO dealerships (~the rest~), VALUES(~values);
I get the error that a value must be specified for dealership_id.
Every-time I've worked with autoincrement primary keys. Normally inserting NULL would work for the index to do the auto-magical things for me. What is happening??
Platform Specific Information:
MariaDB10.1.18 x64
Windows Server 2012 R2
For create add AUTO_INCREMENT
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
and for insert don't insert the key (is automatically create by auto increment)
insert into `dealerships` (
`zone`,
`phone`,
`fax` ,
`name`,
`address1`,
`address2`',
`servicephone` ,
`timezone`
) values (
'vale_zone',
1,
2 ,
'val_name',
'val_address1',
'val_address2',
3 ,
'val_timezone'
)
To insert a field with auto increment property, use ID int NOT NULL AUTO_INCREMENT in the create table.
CREATE TABLE `dealerships` (
`dealership_id` INT NOT NULL AUTO_INCREMENT,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
After, you have two solutions to insert data.
Solution 1
INSERT INTO dealerships(zone, phone, fax, name) VALUES('zone1', '00000', '00000', 'name1')
In this case I chossen to add data in only few columns (add the others on the wame way).
Solution 2
INSERT INTO dealerships SET zone = 'zone1', phone = '0000', name = 'Eddy'
Here, I used the field SET field = value, field2 = value2.
As you can see I didn't added any reference to the ID. Normal, the field is AUTO_INCREMENT mode, and Mysql (or tothers SGBD) will create automatically a value.
Of course, you can force the value:
INSERT INTO dealerships SET dealership_id = 10, zone = 'zone1', phone = '0000', name = 'Eddy'
INSERT INTO dealerships(dealership_id, zone, phone, fax, name) VALUES(9, 'zone1', '00000', '00000', 'name1')
http://www.w3schools.com/sql/sql_insert.asp
About your errors
Normal. You specified NOT NULL
Normal, there is already a record for the value 0 (check your database)
Normal. You can't use default. This is a constant with his own value (1364).
Normal, the field as if can't be ommited
The only way to does not have this issue is to remove completely the reference of the ID field. Use the solution described :)
Georges explained it all for you. If you want to add auto increment to table just run this sql statement
ALTER TABLE dealerships MODIFY COLUMN dealership_id BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT;
This will add the auto increment property on the column dealership_id. And if you want to change auto increment start value run this (Change 100 to your start value)
ALTER TABLE dealerships AUTO_INCREMENT=100;
Related
I stumbled upon weird behavior in MySQL.
Lets say that I have only one record in table
| id | oib |
|----|-----|
| 1 | 5 |
Field oib is unique.
INSERT INTO `test` (`id`, `oib`) VALUES (NULL, '6')
I get following exception
Duplicate entry '5' for key 'oib_UNIQUE'
And this keeps going on no matter what value I try to save.
Anyone have idea what could cause this. I've never seen it.
UPDATE:
Here is CREATE TABLE statement:
CREATE TABLE IF NOT EXISTS `user` (
`id` int(25) NOT NULL AUTO_INCREMENT,
`email` varchar(45) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`dob` int(25) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`zip` int(6) DEFAULT NULL,
`oib` int(11) DEFAULT NULL,
`position` tinyint(4) DEFAULT NULL,
`role` varchar(45) DEFAULT NULL,
`status` tinyint(4) DEFAULT NULL,
`note` mediumtext,
PRIMARY KEY (`id`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `oib_UNIQUE` (`oib`)
)
You cannot insert null value in id primary key
Are you sure your table have one record for oib = '5' ?
You cannot insert null into primary key.
In Mysql, ID is Auto Increment but when converted to Postgres there is no Auto Increment .
Mysql database
CREATE TABLE IF NOT EXISTS `cities` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`state_id` bigint(20) NOT NULL,
`district_id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(1000) DEFAULT NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) DEFAULT NULL,
`is_active` char(1) NOT NULL DEFAULT 'Y',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `state_id` (`state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;
After Converted to postgres database
CREATE TABLE cities (
"id" bigint NOT NULL,
state_id bigint NOT NULL,
district_id bigint NOT NULL,
"name" varchar(255) NOT NULL,
description varchar(1000),
created_by int NOT NULL,
modified_by int,
is_active char(1) NOT NULL,
created timestamp,
modified timestamp,
PRIMARY KEY ("id")
);
INSERT INTO cities(state_id, district_id, city_type, name, description,
created_by, modified_by, is_active, created, modified)
VALUES
(1, 1, '', 'Ramtek', null, 1, null, 'Y',
'2015-04-16 10:44:11', '2015-04-16 10:44:11');
You could use bigserial in such cases. It provides an auto increment feature in postgres:
CREATE TABLE cities (
"id" bigserial PRIMARY KEY,
Not Null constraint is provided by default.
Docs : http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-SERIAL
I am working on magento platform.I face a problem regarding values insertion to specific field: My query run perfect but one specific column not working for any query.I try my best but didn't find why .When i change the column type from int to varchar type it works.This is my table structure.
CREATE TABLE `followupemails_emaillogs` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
the "followupemails_id" column not working in insert and update query.This is one update query where record exist that id(29). UPDATE followupemails_emaillogs SET followupemails_id=5 WHERE id =29.
This is insertion query INSERT INTO followupemails_emaillogs SET followupemails_id=4, schedule_time='2013-10-23 08:10:00', email_status='pending', client_name='ayaz ali'.this works fine on fiddle but not on my sqlyog ? what could be the issue.At last i find query that work perfect
.INSERT INTO followupemails_emaillogs (followupemails_id,schedule_time,email_status,client_name,client_email) VALUES (26,'2013-10-23 08:10:00','pending','ayaz ali','mamhmood#yahoo.com');
Can anyone tell me why set query not working but second query works perfect.so that i can accept his answer.Thanks for all your help
Try like this
To Create,
CREATE TABLE followupemails_emaillogs (
id int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
schedule_time datetime DEFAULT NULL,
sent_time datetime DEFAULT NULL,
email_status varchar(100) DEFAULT NULL,
client_name varchar(250) DEFAULT NULL,
client_email varchar(250) DEFAULT NULL,
followupemails_i int(11) DEFAULT NULL,
UNIQUE (id)
)
To Insert,
INSERT INTO followupemails_emaillogs (schedule_time,sent_time,email_status,client_name,client_email,followupemails_i)
VALUES
('2012-05-05','2012-05-06',"sent","sagar","sagar#xxxx.com",2)
the whole query is ok
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
but at the last there is dot which is actually error so remove the dot and create the table
latin1.
so remove the dot sign and not null in id filed use this line by default fields are null so don't use default null
id int (8) AUTO_INCREMENT
CREATE TABLE `followupemails_emaillogs` (
`id` int (8) AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100),
`client_name` varchar(250),
`client_email` varchar(250),
`followupemails_id` int,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
don't need (11) in the sql query for int operator , This get for length of the nvarchar,varchar datatype column only not a int datatype,So change and write int instead of int(11) and int(8)
Try this query instead of your query
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
I like to append a table to add column but without using alert table command
e.g.
This is the table which is missing some columns.
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`passwd` varchar(40) NOT NULL,
`isActive` tinyint(1) NOT NULL default '1',
`lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
So if i run this query then it should automatically add missing columns into my tables
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`passwd` varchar(40) NOT NULL,
`name` varchar(100) NOT NULL,
`originalUser` tinyint(1) NOT NULL default '0',
`isActive` tinyint(1) NOT NULL default '1',
`lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Can this be possible to do without using alert table command ?
I understand your question as you want to add some columns to your table. Please be informed that the term row is usually related to the actual data in your table, not the columns itself. If my assumption is wrong, please clarify your question.
You cannot use CREATE TABLE for altering a table. It is there to create table,
and if it cannot create it, it will in most cases throw an error like you described. Another command exists for that reason: ALTER TABLE.
You might do it something like this.
(1) Create your table with your CREATE TABLE syntax above:
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`passwd` varchar(40) NOT NULL,
`isActive` tinyint(1) NOT NULL default '1',
`lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
(2) Use ALTER TABLE like this to make the modifications I think you want to have in your second statement (two more columns):
ALTER TABLE
ADD COLUMN `name` varchar(100) NOT NULL AFTER `passwd`,
ADD COLUMN `originalUser` tinyint(1) NOT NULL default '0' AFTER `name`;
Not related to your question, but I'd avoid column names like name, because if you don't escape them properly it'll throw you other errors (see reserved words).
I encountered this quite a few times so far, but still don't understand it (my MySQL internals skills are equal to none).
I know it's probably a PEBKAC but trying to replicate the behavior manually ends up with an error (autoincrement).
CREATE TABLE `foo_bar` (
`id` int(12) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(12) unsigned DEFAULT NULL,
`order_id` int(12) unsigned DEFAULT NULL,
`email_address` varchar(50) DEFAULT NULL,
`mobile_number` varchar(20) DEFAULT NULL,
`message` longtext NOT NULL,
`message_received` int(12) unsigned DEFAULT NULL,
`failed_to_send` tinyint(1) unsigned DEFAULT NULL,
`fraudulent_activity` tinyint(1) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=ARCHIVE DEFAULT CHARSET=utf8;
When your program inserts a row in the database, it should provide NULL as the value for auto-incremented field:
CREATE TABLE `customers` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 128 ) NOT NULL
) ENGINE = MYISAM ;
INSERT INTO `customers` ( `id` , `name` )
VALUES ( NULL , 'Customer 1' ), ( NULL , 'Customer 2' );
If you try to insert a specific value in id field, MySQL will give an error:
SQL query:
INSERT INTO `customers` ( `id` , `name` )
VALUES ( '1', 'Customer 3' );
MySQL said:
#1062 - Duplicate entry '1' for key 'PRIMARY'
Although the answer to "what caused this" didn't come up, REPAIR TABLE fixes the problem.
Answering this so I can close the question.