#1062 - Duplicate entry '0' for key 'PRIMARY' 7 - mysql

Getting this error and not being savvy with creating tables and columns some of the other similar problems and answers haven't helped.
This is the error:
Error
SQL query:
INSERT INTO orders_session_info
(txn_signature , orders_id , payment , payment_title , payment_amount , payment_currency , payment_currency_val , sendto , billto , language , language_id , currency , currency_value , firstname , lastname , content_type , affiliate_id , affiliate_date , affiliate_browser, affiliate_ipaddress , affiliate_clickthroughs_id )
VALUES
(
'd4208b15558a761c34dc7c79b87275b4', '20167', 'paypal', 'PayPal', '273.56', 'USD', '1.00000000', '140', '140', 'english', '1', 'USD', '1.00000000', 'Pauls', 'Dandzbergs', 'physical', '0', '0000-00-00 00:00:00', '', '', '0'
);
MySQL said: Documentation
#1062 - Duplicate entry '0' for key 'PRIMARY'
And the code:
-- Table structure for table `orders_session_info`
--
DROP TABLE IF EXISTS orders_session_info;
CREATE TABLE orders_session_info (
txn_signature int(11) NOT NULL AUTO_INCREMENT,
orders_id int(11) NOT NULL,
payment varchar(32) NOT NULL,
payment_title varchar(32) NOT NULL,
payment_amount varchar(32) NOT NULL,
payment_currency varchar(32) NOT NULL,
payment_currency_val varchar(32) NOT NULL,
sendto varchar(32) NOT NULL,
billto varchar(32) NOT NULL,
language varchar(32) NOT NULL,
language_id varchar(32) NOT NULL,
currency varchar(32) NOT NULL,
currency_value varchar(32) NOT NULL,
firstname varchar(32) NOT NULL,
lastname varchar(128) NOT NULL,
content_type varchar(128) NOT NULL,
affiliate_id varchar(128) NOT NULL,
affiliate_date varchar(128) NOT NULL,
affiliate_browser varchar(128) NOT NULL,
affiliate_ipaddress varchar(128) NOT NULL,
affiliate_clickthroughs_id varchar(128) NOT NULL,
PRIMARY KEY (txn_signature),
KEY txn_signature(orders_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
any specific help would be much appreciated...
--
-- Dumping data for table orders_session_info

Your primary key field txn_signature is an auto increment field. This means the database will create a unique value for it automatically when you insert a new row.
Therefore you should not include this field in your INSERT statement - the database will take care of it for you. Right now it thinks you're trying to override the automatic functionality and insert a value of "d4208b15558a761c34dc7c79b87275b4" manually. Obviously this would result in duplicate IDs once you have more than one row, so it's not allowed. Also, your field is an int and you're trying to insert a string into it, which will never work either.

Related

MySQL - Slow query

Sorry for the long post, but the tables involved are quite big.
When I run the query bellow it normally takes about 1m to run. However, when I remove the correlated sub-query I get the query down to 15 seconds. So I think that's the actual problem.
Problem is, I don't really know how to get the SUM results without the correlated query.
QUERY I'm trying to run
SELECT cl_clients.vat as association_vat, cl_clients.name as association_name, cl_clients_with_regions.city_id, cl_clients_with_regions.nut_1, cl_clients_with_regions.nut_2, cl_clients_with_regions.nut_3,
company.vat as company_vat, company.name, company.section, company.division, company.cae,
SUM((
SELECT SUM((COALESCE((cl_finances.sales_community_market), 0) + COALESCE((cl_finances.sales_extra_market), 0)))
from cl_finances
where cl_finances.vat = company.vat
and cl_finances.year = 2018
)) as total_sum
FROM `cl_clients_with_regions`
JOIN `cl_client_intervention_areas` ON `cl_client_intervention_areas`.`city_id` = `cl_clients_with_regions`.`city_id`
JOIN `cl_clients` ON `cl_clients`.`vat` = `cl_client_intervention_areas`.`client_vat`
INNER JOIN cl_clients_with_regions company ON cl_clients_with_regions.vat = company.vat
WHERE `cl_clients_with_regions`.`encrypted_vat` IS NOT NULL
AND `cl_clients_with_regions`.`country` IS NOT NULL
AND `cl_clients_with_regions`.`cae` IS NOT NULL
AND `cl_clients_with_regions`.`cae` != ''
AND `cl_clients_with_regions`.`division` IS NOT NULL
AND `cl_clients_with_regions`.`section` = 'A'
AND `cl_clients_with_regions`.`nut_2` = 'Centro'
GROUP BY association_vat;
CL Clients create table syntax
create table cl_clients
(
vat varchar(20) default '' not null,
encrypted_vat varchar(32) null,
temporary_vat enum ('0', '1', '') default '0' null,
sig_id int null,
phc_id int null,
client_manager int null,
name varchar(300) null,
brand varchar(255) null,
`group` varchar(200) null,
class_internacional varchar(45) null,
logoimage varchar(400) null,
social_capital varchar(100) null,
address varchar(300) null,
gps varchar(25) null,
parish varchar(70) null,
zip_code varchar(10) null,
city int null,
district int null,
country int default 75 not null,
headquarter int null,
person_title varchar(7) null,
person_contact varchar(350) null,
person_phone varchar(20) null,
person_cell varchar(15) null,
person_email varchar(150) null,
person_function varchar(150) null,
language varchar(10) null,
phone varchar(20) null,
mobile_phone varchar(20) null,
fax varchar(20) null,
email varchar(50) null,
email_alternative varchar(50) null,
website varchar(300) null,
pme varchar(500) null,
pme_lider varchar(500) null,
cae varchar(15) null,
cae_2 varchar(15) null,
certified enum ('1', '0', '') null,
sector varchar(350) null,
workers int null,
foundation_date date null,
size enum ('Micro Empresa', 'Pequena empresa', 'Média empresa', 'Grande empresa', 'Não aplicável', 'PME', '') default '' null,
company_type varchar(455) default 'Empresa' null,
type enum ('novo', 'antigo', 'analizar', '') default 'analizar' null,
entity enum ('client', 'prospect', 'suplier', 'potential', '') default 'prospect' null,
client_to_country int null,
partner enum ('1', '0', '') default '0' not null,
partner_dp enum ('0', '1', '') default '0' null,
lucrative enum ('1', '0', '') default '1' not null,
dun varchar(100) null,
debt enum ('50k', '100k', '500k', '1m', '5m', '+5m', '') null,
bankruptcy enum ('1', '0', '') default '0' null,
competitors enum ('1', '0', '') default '0' null,
dun_date date null,
bank_1 varchar(30) null,
bank_2 varchar(30) null,
bank_3 varchar(30) null,
status enum ('0', '1', '') default '1' null,
followup enum ('0', '1', '') default '1' null,
classification enum ('Prestige', 'Premium', 'Current', '') default 'Current' null,
associated enum ('0', '1', '') default '0' null,
industry_id int null,
valid enum ('0', '1', '') default '1' null,
imported_at timestamp null,
modified_by int default 0 null,
responsible_id int null,
partner_type int null,
iberinform_id int null,
iberinform_date timestamp null,
legal_form text null,
going_concern text null,
score int null,
sector_score int null,
tax_status text null,
irc_debtors_ledger enum ('0', '1') null,
ss_debtors_ledger enum ('0', '1') null,
association_class_id int null,
nature enum ('business', 'commercial', 'sectorial', 'other') null,
geographic_scope enum ('national', 'regional', 'local') null,
intervention_nut enum ('nut_1', 'nut_2', 'nut_3') null,
ch_is_associated enum ('0', '1') null,
bi enum ('0', '1') default '0' null,
updated_at timestamp null,
constraint encrypted_vat_UNIQUE
unique (encrypted_vat),
constraint vat
unique (vat),
constraint cl_clients_cl_caes_cae_fk
foreign key (cae) references cl_caes (cae)
on update cascade,
constraint clients_industry_id_fk
foreign key (industry_id) references cl_industries (id),
constraint cltns_asso_clss_id_fk
foreign key (association_class_id) references cl_association_classifications (id)
on update cascade on delete set null,
constraint country_fk_id
foreign key (country) references cl_countries (id)
on update cascade on delete cascade
);
create index cl_client_name_idx
on cl_clients (name);
create index cl_clients_bi_idx
on cl_clients (bi);
create index cl_clients_brand_idx
on cl_clients (brand);
create index cl_clients_company_type_idx
on cl_clients (company_type);
create index cl_clients_geo_index
on cl_clients (city, district);
create index cl_clients_geographic_scope_idx
on cl_clients (geographic_scope);
create index cl_clients_group_idx
on cl_clients (`group`);
create index cl_clients_nature_idx
on cl_clients (nature);
create index cltns_asso_clss_id_fk_idx
on cl_clients (association_class_id);
create index country_idx
on cl_clients (country);
create index fk_clients_industries1_idx
on cl_clients (industry_id);
alter table cl_clients
add primary key (vat);
VIEW being used in the query
create view cl_clients_with_regions as
select `grupoch`.`cl_clients`.`vat` AS `vat`,
`grupoch`.`cl_clients`.`encrypted_vat` AS `encrypted_vat`,
`grupoch`.`cl_clients`.`name` AS `name`,
`grupoch`.`cl_clients`.`brand` AS `brand`,
`grupoch`.`cl_clients`.`country` AS `country`,
`grupoch`.`cl_clients`.`district` AS `district`,
`grupoch`.`cl_clients`.`city` AS `city`,
`grupoch`.`cl_cities`.`id` AS `city_id`,
`grupoch`.`cl_cities`.`name` AS `city_name`,
`grupoch`.`cl_cities`.`nut_1` AS `nut_1`,
`grupoch`.`cl_cities`.`nut_2` AS `nut_2`,
`grupoch`.`cl_cities`.`nut_3` AS `nut_3`,
`grupoch`.`cl_clients`.`cae` AS `cae`,
`grupoch`.`cl_caes`.`description` AS `cae_designation`,
`grupoch`.`cl_caes_divisions`.`division` AS `division`,
`grupoch`.`cl_caes_divisions`.`division_designation` AS `division_designation`,
`grupoch`.`cl_caes_divisions`.`section` AS `section`,
`grupoch`.`cl_caes_divisions`.`section_designation` AS `section_designation`
from (((`grupoch`.`cl_clients` join `grupoch`.`cl_cities` on ((
(`grupoch`.`cl_clients`.`city` = `grupoch`.`cl_cities`.`city_cod`) and (`grupoch`.`cl_clients`.`district` =
`grupoch`.`cl_cities`.`district_id`)))) join `grupoch`.`cl_caes` on ((`grupoch`.`cl_caes`.`cae` = `grupoch`.`cl_clients`.`cae`)))
join `grupoch`.`cl_caes_divisions`
on ((`grupoch`.`cl_caes_divisions`.`division` = `grupoch`.`cl_caes`.`division_id`)))
where ((`grupoch`.`cl_clients`.`country` in (75, 185, 186)) and (`grupoch`.`cl_clients`.`vat` is not null));
CL finances create table syntax
create table cl_finances
(
vat varchar(20) not null,
year int not null,
workers int null,
sells varchar(300) null,
sells_variation varchar(300) null,
international_sells varchar(300) null,
international_sells_variation varchar(300) null,
liquid_results varchar(300) null,
liquid_results_variation varchar(300) null,
capital varchar(300) null,
active_liquid varchar(300) null,
financial_autonomy varchar(300) null,
modified_by int default 0 null,
sales_profitability decimal(13, 2) null,
return_on_capital decimal(13, 2) null,
sales_community_market decimal(13, 2) null,
sales_extra_market decimal(13, 2) null,
created_at timestamp null,
updated_at timestamp null,
primary key (vat, year),
constraint clients_finances_vat
foreign key (vat) references cl_clients (vat)
on update cascade on delete cascade
);
create index cl_finances_year_idx
on cl_finances (year);
create index vat
on cl_finances (vat);
## Create table syntax for cl_client_intervention_areas
create table cl_client_intervention_areas
(
client_vat varchar(20) not null,
city_id int not null,
constraint fk_cl_client_intervention_areas_cl_cities1
foreign key (city_id) references cl_cities (id)
on update cascade on delete cascade,
constraint fk_cl_client_intervention_areas_cl_clients1
foreign key (client_vat) references cl_clients (vat)
on update cascade on delete cascade
);
create index fk_cl_client_intervention_areas_cl_cities1_idx
on cl_client_intervention_areas (city_id);
create index fk_cl_client_intervention_areas_cl_clients1_idx
on cl_client_intervention_areas (client_vat);
The explain result from this query is quite big
I believe the subquery is being executed once per row in the group by clause.
Could you try the following if it makes it any better.
Change i have done is to perform the correlated query as a inline-view and then join with the main table/view -- company.vat
SELECT cl_clients.vat as association_vat, cl_clients.name as association_name, cl_clients_with_regions.city_id, cl_clients_with_regions.nut_1, cl_clients_with_regions.nut_2, cl_clients_with_regions.nut_3,
company.vat as company_vat, company.name, company.section, company.division, company.cae,
SUM(temp_val.sum_val) as total_sum
FROM `cl_clients_with_regions`
JOIN `cl_client_intervention_areas` ON `cl_client_intervention_areas`.`city_id` = `cl_clients_with_regions`.`city_id`
JOIN `cl_clients` ON `cl_clients`.`vat` = `cl_client_intervention_areas`.`client_vat`
INNER JOIN cl_clients_with_regions company ON cl_clients_with_regions.vat = company.vat
LEFT JOIN (SELECT cl_finances.vat
,SUM((COALESCE((cl_finances.sales_community_market), 0) + COALESCE((cl_finances.sales_extra_market), 0))) as sum_val
FROM cl_finances
WHERE 1=1
AND cl_finances.year = 2018
GROUP BY cl_finances.vat
)temp_val
ON company.vat=temp_val.vat
WHERE `cl_clients_with_regions`.`encrypted_vat` IS NOT NULL
AND `cl_clients_with_regions`.`country` IS NOT NULL
AND `cl_clients_with_regions`.`cae` IS NOT NULL
AND `cl_clients_with_regions`.`cae` != ''
AND `cl_clients_with_regions`.`division` IS NOT NULL
AND `cl_clients_with_regions`.`section` = 'A'
AND `cl_clients_with_regions`.`nut_2` = 'Centro'
GROUP BY association_vat;
To further explain the above answer:
Subqueries should never be in the select clause because they execute once per record that is returned / or calculated to return. This causes the query to get slower and slower as the database gets larger.
Instead, the subquery should be in the FROM clause, and the result of the subquery should be joined to and selected.

What seems to be the problem? Sorry im new with this

Im just testing things out but my first code didnt work.
CREATE TABLE `Accounts`.`Registry` ( `Username` VARCHAR(16) NOT NULL ,
`Password` VARCHAR(16) NOT NULL ,
`First Name` INT(20) NOT NULL ,
`Last Name` INT(20) NOT NULL ,
`Gender` INT(1) NOT NULL ,
`ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`Password`, `First Name`, `Last Name`),
UNIQUE (`ID`(1000)),
UNIQUE (`Username`)) ENGINE = MyISAM;
Error
#089 - Incorrect prefix key, the used length is longer than the key part, or the storage engine doesnt support unique prefix keys
This must be the reason for the error UNIQUE (ID(1000))
Try this.
CREATE TABLE Accounts.Registry (
Username VARCHAR(16) NOT NULL ,
Password VARCHAR(16) NOT NULL ,
First Name INT(20) NOT NULL ,
Last Name INT(20) NOT NULL ,
Gender INT(1) NOT NULL ,
ID INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (Password, First Name, Last Name),
UNIQUE (ID),
UNIQUE (Username)) ENGINE = MyISAM;
As already mentioned in comment and by others remove UNIQUE (`ID` (1000)).
For better performance, create a table asInnodb and other useful info shared below
Int stores values as numbers with a range from -2147483648 to 2147483647.
First Name & Last Name change as VARCHAR(20).
Gender you can change as TINYINT.
CREATE TABLE `Accounts`.`Registry` (
`ID` INT NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(16) NOT NULL,
`Password` VARCHAR(16) NOT NULL,
`First Name` INT(20) NOT NULL,
`Last Name` INT(20) NOT NULL,
`Gender` TINYINT(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE (`Username`)
) ENGINE=Innodb;

MySQL foreign key not found even if it exists

I have 2 tables 'open_invoices' and 'paid_invoices' with the below structures:
CREATE TABLE `open_invoices` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`unique_identifier` varchar(255) NOT NULL,
`insert_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cust_nbr` int(11) NOT NULL,
`location` varchar(10) NOT NULL,
`company` varchar(10) NOT NULL,
`invoice_nbr` int(11) NOT NULL,
`future_letter` varchar(1) NOT NULL,
`invoice_total` decimal(8,2) NOT NULL,
`invoice_payments` decimal(8,2) NOT NULL,
`record_type` int(11) NOT NULL,
`terms_desc` varchar(255) NOT NULL,
`due_date` varchar(8) NOT NULL,
`discount_date` varchar(8) NOT NULL,
`orig_disc_avail` decimal(8,2) NOT NULL,
`cust_po` varchar(10) NOT NULL,
`invoice_date` varchar(8) NOT NULL,
`status` int(11) NOT NULL,
`check_no` varchar(10) NOT NULL,
`last_pay_date` varchar(8) NOT NULL,
`as_of_date` varchar(8) NOT NULL,
`as_of_time` varchar(11) NOT NULL,
`remaining` varchar(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_identifier` (`unique_identifier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `paid_invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_change` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`invoice_date` varchar(8) NOT NULL,
`unique_identifier` varchar(255) NOT NULL,
`amount_paid` decimal(8,2) NOT NULL,
`amount_left` decimal(8,2) NOT NULL,
`payment_type` varchar(2) NOT NULL,
`last4` varchar(255) NOT NULL,
`transac_id` varchar(255) NOT NULL,
`customer_id` varchar(255) NOT NULL,
`payment_P_F` varchar(1) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (unique_identifier)
REFERENCES open_invoices (unique_identifier)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I'm trying to insert a row in 'paid_invoices' like below:
INSERT INTO `paid_invoices` (`invoice_date`, `unique_identifier`, `amount_paid`, `amount_left`, `payment_type`, `last4`, `transac_id`, `customer_id`, `payment_P_F`) VALUES ('07/19/18', '126_89948_2576', 0, '37.45', '', '', '', '13776', 'P')
I get the below error:
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`accounts_receivables`.`paid_invoices`, CONSTRAINT `paid_invoices_ibfk_1` FOREIGN KEY (`unique_identifier`) REFERENCES `open_invoices` (`unique_identifier`))
INSERT INTO `paid_invoices` (`invoice_date`, `unique_identifier`, `amount_paid`, `amount_left`, `payment_type`, `last4`, `transac_id`, `customer_id`, `payment_P_F`) VALUES ('07/19/18', '126_89948_2576', 0, '37.45', '', '', '', '13776', 'P')
If I search on google, it says that the foreign key 126_89948_2576 was not found in the table 'open_invoices'.
When I do SELECT * FROM open_invoices WHERE id = 2576 I see the row with the foreign key 126_89948_2576 as shown in the picture below :
[![query 1][1]][1]
but when I do this query SELECT * FROM open_invoices WHERE unique_identifier = '126_89948_2576' I get no result see capture below:
[![query 2][2]][2]
I'm sure there is no extra blank space in the value saved for foreign key 126_89948_2576.
What is going on please? Is there a bug in the version of MySQL I'm using?
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.6.40 - MySQL Community Server (GPL)
Protocol version: 10
Thanks.
[1]: https://i.stack.imgur.com/L2QDX.png
[2]: https://i.stack.imgur.com/T9Fz5.png
It seems you are using wrong divider symbols, "_" instead of "-" in your query. Try unique_identifier = '126-89948-2576'
Because the underscore _ is a wildcard like the percent %, except that it only looks for one character.
SQL pattern matching enables you to use "_" to match any single character and "%" to match an arbitrary number of characters (including zero characters).
you can use
SELECT * FROM open_invoices WHERE unique_identifier like '%126\_89948\_2576%'

How do I INSERT into a database with a primary autoincrement key?

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;

Unexpected behaviour of the query

This particular query is supposed to enter the user into the database.
This query does not always insert the values of the firstname and the lastname fields along with others. firstname and lastname are empty for a few insertions and for the others its working as expected.
INSERT INTO `users` (mobile, passwordHash, firstname, lastname, ent_id, email )
VALUES ('913800341127', '678a1491514b7f1006d605e9161946b1', 'nat', 'sam', '108', NULL)
ON DUPLICATE KEY UPDATE `firstname` = VALUES(firstname),`lastname` = VALUES(lastname)
Related Info:
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`tag` varchar(5) NOT NULL default 'ind',
`username` varchar(50) default NULL,
`firstname` varchar(100) default NULL,
`lastname` varchar(100) default NULL,
`passwordhash` varchar(255) NOT NULL,
`secretq` varchar(255) default NULL,
`secreta` varchar(100) default NULL,
`email` varchar(50) default NULL,
`mobile` varchar(13) default NULL,
`last_login` datetime default NULL,
`ent_id` bigint(20) NOT NULL default '1',
`is_inactive` tinyint(1) NOT NULL COMMENT 'Whether the user is active or not',
PRIMARY KEY (`id`),
UNIQUE KEY `mobile_2` (`mobile`,`ent_id`),
UNIQUE KEY `email_2` (`email`,`ent_id`),
KEY `username` (`username`),
KEY `ent_id` (`ent_id`,`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
syntax error it should be :
ON DUPLICATE KEY UPDATE firstname = 'nat',lastname = 'sam'