Mysql autoincrement error when insert a new register - mysql

I have this table
CREATE TABLE IF NOT EXISTS `test`.`Challenge` (
`idChallenge` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`description` VARCHAR(45) NULL DEFAULT NULL,
`start` DATETIME NOT NULL,
`ending` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`idChallenge`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;
When I try insert a new register:
insert into challenge (name, start) values ("test", now());
Gives me this error:
Error Code: 1364. Field 'id_challenge' doesn't have a default value
But first I don't have this column in my table and if I put it is create.

Us must use backticks ` for reserved words.
insert into Challenge (`name`, `start`) values ("test", now());

The problem are with idChallenge, if I change it to idchallenge works.

Related

MySQL Update on duplicate error

I have two tables, EVENTS and EVENT_ATTENDEES.
I'd like to be able to attempt a row insert on event_attendees and if the call_sign and event_id already exist in the table, it should update the response for that row instead of inserting a new one.
Events Table
CREATE TABLE `events` (
`event_id` int(11) NOT NULL,
`created_by` varchar(1000) NOT NULL,
`event_type` varchar(1000) NOT NULL,
`event_name` varchar(1000) NOT NULL,
`event_start` datetime NOT NULL,
`event_end` datetime NOT NULL,
`max_attendees` varchar(10) NOT NULL,
`open_registration` varchar(10) NOT NULL
)
ALTER TABLE `events`
ADD PRIMARY KEY (`event_id`);
ALTER TABLE `events`
MODIFY `event_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11
Event Attendees
CREATE TABLE `event_attendees` (
`id` int(11) NOT NULL,
`event_id` int(11) NOT NULL,
`call_sign` varchar(10) NOT NULL,
`response` varchar(10) NOT NULL
)
ALTER TABLE `event_attendees`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `event_id_2` (`event_id`,`call_sign`),
ADD KEY `event_id` (`event_id`) USING BTREE;
ALTER TABLE `event_attendees`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;
ALTER TABLE `event_attendees`
ADD CONSTRAINT `event_id_fk` FOREIGN KEY (`event_id`) REFERENCES `events` (`event_id`) ON DELETE CASCADE ON UPDATE NO ACTION;
I have tried the below query, but constantly get an error (I have created an event that has event_id = 10):
INSERT INTO event_attendees
(event_id, call_sign, response)
VALUES
('10', '007', 'Declined')
ON DUPLICATE KEY UPDATE
response = VALUES('Declined')
" #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 ''test')' at line 6"
There is no need of VALUES here. Modify your query as:
INSERT INTO event_attendees
(event_id, call_sign, response)
VALUES
('10', '007', 'Declined')
ON DUPLICATE KEY UPDATE
response = 'Declined'
From doc:
you can use the VALUES(col_name) function to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement
Here you can find more about VALUES function.

Issue in insert query mysql error#1364

my query is
INSERT INTO `session_card` (`flags`, `history`, `showtime`, `session_id`, `card_id`, `card_mode`) VALUES ('', 'ö', '12', 9410, '256', 'rw')
and the table structure is
DROP TABLE IF EXISTS `session_card`;
CREATE TABLE IF NOT EXISTS `session_card` (
`id` int(11) NOT NULL,
`session_id` int(11) DEFAULT NULL,
`card_id` int(100) DEFAULT NULL,
`history` varchar(1000) DEFAULT NULL,
`flags` varchar(255) NOT NULL DEFAULT '',
`showtime` varchar(2000) DEFAULT NULL,
`card_mode` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `session_card_unique_key` (`session_id`,`card_id`,`card_mode`),
KEY `fk` (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
COMMIT;
Now I dont understand what is the issue here also my phpmyadmin show only error code it doesnt give me the error expatiation. Any one can help me with this.
You need to have AUTO_INCREMENT on your id column, or you will need to pass a unique id manually every time you insert a row.
Change:
CREATE TABLE IF NOT EXISTS `session_card` (
`id` int(11) NOT NULL,
to:
CREATE TABLE IF NOT EXISTS `session_card` (
`id` int(11) NOT NULL AUTO_INCREMENT,
I would also suggest adding UNSIGNED as well since id's usually don't contain negative values.
In your schema, you have given id` int(11) NOT NULL but while inserting data you are not passing any value for id. as id is a not null constraint you should pass the value for id.
Modify you ID column to AUTO_INCREMENT flag
ALTER TABLE session_card MODIFY id int NOT NULL AUTO_INCREMENT
Make sure you table is empty before executing the above sql
If you are not setting a primary key id to Auto Increment then provide a value while inserting.
Otherwise, set it to AutoIncrement. This is better way to assigning a value.
INSERT INTO `session_card`
(`id`,`flags`, `history`, `showtime`, `session_id`, `card_id`, `card_mode`) VALUES
('1','', 'ö', '12', 9410, '256', 'rw')
Might be it's because of strick mode
Disable it:
SET sql_mode = '';
After this try insert query.
please make id key is auto increment
like following.
ALTER TABLE session_card MODIFY id int NOT NULL AUTO_INCREMENT
Otherwise add manually id at insertion time. like following.
INSERT INTO session_card
(id,flags, history, showtime, session_id, card_id, card_mode) VALUES
('1','', 'ö', '12', 9410, '256', 'rw')

Error Code: 1264. Out of range value for column

I'm trying to get my code to work and just from the first line I'm inserting I get an error. I've already changed the value of the phone number's to varchar, but this error still remains.
This is my code:
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Customer` (
`Customer_ID` INT NOT NULL,
`Customer_F_Name` VARCHAR(45) NOT NULL,
`Customer_L_Name` VARCHAR(45) NOT NULL,
`Customer_Address` VARCHAR(45) NOT NULL,
`Customer_DOB` DATE NOT NULL,
`Customer_H_Phone` VARCHAR(20) NOT NULL,
`Customer_W_Phone` VARCHAR(20) NOT NULL,
`Customer_Type` VARCHAR(45) NOT NULL,
`Customer_Sponsor_ID` INT NULL,
PRIMARY KEY (`Customer_ID`),
INDEX `fk_Customer_Customer1_idx` (`Customer_Sponsor_ID` ASC),
CONSTRAINT `fk_Customer_Customer1`
FOREIGN KEY (`Customer_Sponsor_ID`)
REFERENCES `mydb`.`Customer` (`Customer_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
INSERT INTO `Customer` (`Customer_ID`, `Customer_F_Name`, `Customer_L_Name`,
`Customer_Address`, `Customer_DOB`, `Customer_H_Phone`, `Customer_W_Phone`,
`Customer_Type`, `Customer_Sponsor_ID`) VALUES
(1, 'Nicole', 'Gilbert', '8295 Golden Leaf Alley',
'1992-03-28', '5744813109', '1646648129', 'Student', NULL);
And then when I just insert the first line of data this is the error I get:
INSERT INTO `Customer` (`Customer_ID`, `Customer_F_Name`, `Customer_L_Name`,
`Customer_Address`, `Customer_DOB`, `Customer_H_Phone`, `Customer_W_Phone`,
`Customer_Type`, `Customer_Sponsor_ID`) VALUES
(1, 'Nicole', 'Gilbert', '8295 Golden Leaf Alley',
'1992-03-28', '5744813109', '1646648129', 'Student', NULL)
Error Code: 1264. Out of range value for column
'Customer_H_Phone' at row 1
Thank you!

Can I add a Unique key on table creation in SQL?

I am trying to translate a collection of MySQL functions to SQL, and I'm having issues with a UNIQUE KEY issue:
-- -----------------------------------------------------
-- Table testform
-- -----------------------------------------------------
CREATE TABLE `testform` (
`FormId` INT(11) NOT NULL AUTO_INCREMENT,
`TTId` INT(11) NULL DEFAULT NULL,
`TestName` VARCHAR(100) NULL,
PRIMARY KEY (`FormId`),
UNIQUE KEY `TF_Composite` (`TTId`, `TestName`));
When I try and test this in SQLFiddle, it's giving me the error
Incorrect syntax near the keyword 'KEY'.
I have tried searching for this, but so far all I have come up with is "Unique Constraints". Is there a difference between a "Key" and a "Constraint" in SQL? And if so, how can I add this in the table creation statement?
Your syntax is all messed up. Please look at books on-line (MSDN).
https://msdn.microsoft.com/en-us/library/ms174979.aspx
The sample code below create a table in tempdb. This table automatically gets destroyed when the service is restarted.
-- Just a example, throw away after reboot
USE [tempdb]
GO
-- Create the table
CREATE TABLE DBO.TESTFORM
(
FORM_ID INT IDENTITY(1, 1) NOT NULL ,
TT_ID INT NULL,
TEST_NAME VARCHAR(100) NULL,
CONSTRAINT PK_FORM_ID PRIMARY KEY (FORM_ID),
CONSTRAINT UN_COMPOSIT UNIQUE (TT_ID, TEST_NAME)
);
-- Seventies Band
INSERT INTO TEMPDB.DBO.TESTFORM VALUES (1, 'John');
INSERT INTO TEMPDB.DBO.TESTFORM VALUES (2, 'Paul');
INSERT INTO TEMPDB.DBO.TESTFORM VALUES (3, 'Mary');
GO
-- Show data
SELECT * FROM TEMPDB.DBO.TESTFORM
GO
The image below shows the data in this table.
Try This.
CREATE TABLE testform (
FormId INT(11) NOT NULL AUTO_INCREMENT,
TTId INT(11) NULL DEFAULT NULL,
TestName VARCHAR(100) NULL,
PRIMARY KEY (FormId),
CONSTRAINT TF_Composite UNIQUE (TTId,TestName));
More Details..
For Better Understanding about Primary and Unique you can refer below page.
Primary and Unique Key Creation
For MySQL Database
CREATE TABLE `phone` (
`id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`country` DECIMAL(5,0) UNSIGNED NOT NULL,
`area` DECIMAL(5,0) UNSIGNED NOT NULL,
`number` DECIMAL(8,0) UNSIGNED NOT NULL,
`extension` DECIMAL(5,0) UNSIGNED DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ix_phone` (`country`, `area`, `number`, `extension`),
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
For alter Table :
ALTER TABLEphone
ADD UNIQUE INDEXix_phone(country,area,number,extension);

MySQL INSERT BEFORE TRIGGER Fails?

I'm running MySQL 5.1.48 on Windows XP and the following trigger test doesn't work:
-- Drop the old table
DROP TABLE IF EXISTS `ikasan01`.`ikasanwiretap`;
-- Create
CREATE TABLE `ikasan01`.`ikasanwiretap` (
`Id` bigint(20) NOT NULL AUTO_INCREMENT,
`ModuleName` varchar(255) NOT NULL,
`FlowName` varchar(255) NOT NULL,
`ComponentName` varchar(255) NOT NULL,
`EventId` varchar(255) NOT NULL,
`PayloadId` varchar(255) NOT NULL,
`PayloadContent` varchar(255) NOT NULL,
`CreatedDateTime` datetime NOT NULL,
`UpdatedDateTime` datetime NOT NULL,
`Expiry` datetime NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- In order to use now() as a DEFAULT value on a datetime column we
have to do it via a BEFORE INSERT TRIGGER
CREATE TRIGGER defaultUpdateDateTime
BEFORE INSERT ON ikasanwiretap FOR EACH ROW
SET NEW.UpdatedDateTime = now();
-- Test the trigger
insert into IkasanWiretap (ModuleName, FlowName, ComponentName,
EventId, PayloadId, PayloadContent, CreatedDateTime, Expiry) values
('3', '3', '3', '3', '3', '3', now(), now());
On my trigger test I still get the MySQL server stating that the UpdateDateTime has no default value.
I've tried just about everything at my disposal so I thought I'd ask you helpful lot ;) Any ideas?
Cheers,
Martijn
PS: X-posted on the London Java User Group and the Ikasan EIP Mailing list.
I guess the trigger is started after the "not null" constraints are verified.
Remove the "not null" on UpdatedDateTime.