I'm trying to build a trigger that will auto-fill information into tblRegistrations when a new entry is created in tblAttendees and I'm unsure what I should be using to get it to take the new Auto-incremented key from the tblAttendees?
So in the code Registration ID is the primary key auto_incremented for tblRegistrations and AttendeeID is the primary key auto_incremented for tblattendees which exists as a foreign key on tblRegistrations
CREATE TRIGGER trgRegistration
AFTER INSERT ON tblattendees
FOR EACH ROW
INSERT INTO tblregistration (RegistrationID, AttendeeID, EventID, RegistrationDate, RegistrationPaid)
VALUES (AUTO_INCREMENT, AUTO_INCREMENT, '3', CURRENT_DATE, '0')
When I try to end a new entry into the attendees table to test the trigger I keep getting this error:
"#1054- Unknown Column 'Auto_Increment' in 'field list'
Clearly it's a problem with how I have by Values formatted, but I'm uncertain on how to proceed.
The AUTO_INCREMENT column RegistrationID should take care of itself if you put NULL there or just leave it out of the column list.
As for the newly-generated AttendeeID, you can access that using the NEW keyword, which stands for the newly-inserted row that caused the trigger to run.
CREATE TRIGGER trgRegistration
AFTER INSERT ON tblattendees
FOR EACH ROW
INSERT INTO tblregistrations (AttendeeID, EventID, RegistrationDate, RegistrationPaid)
VALUES (NEW.AttendeeID, '3', CURRENT_DATE, '0');
Related
I am trying to execute the below line into MySQL and keep getting an error
INSERT INTO Orders VALUES ("C1000",'2018-04-15', '2018-04-18', 33.98)
Error:
Error 1136 column count doesn't match value count at row 1
Table DDL:
CREATE TABLE Orders (
CustomerID VARCHAR(10) NOT NULL,
OrderDate DATE NOT NULL,
ShipDate DATE NOT NULL,
TotalOrderAmount DECIMAL(10,2) NOT NULL,
PRIMARY KEY (CustomerID, ShipDate),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
)
Can somebody please assist with correcting this error? I have been working on this for a few days and I can't get it.
Where will they get inserted?
You are missing the column names of the table Orders.
mysql> SHOW TRIGGERS;
You have a BEFORE INSERT or AFTER INSERT trigger on the Orders table that is inserting a row into a different table whenever you insert into Orders. The query inside the trigger is incorrect and throwing this error indirectly.
Given the following table:
DROP TABLE IF EXISTS my_table;
CREATE TABLE IF NOT EXISTS my_table(
id INT NOT NULL,
timestamp TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,
data BLOB NULL,
PRIMARY KEY (id)
);
I can insert on it with:
INSERT INTO my_table (timestamp, data) VALUES
('2014-07-11 11:25:48.185', LOAD_FILE('sql/file.bin'));
In the above insert I was not enforced to insert the id field.
How may I create the table (my_table) so that it prevents inserts without id?
I would every insert to be made (providing the id) like, i.e.:
INSERT INTO my_table (id, timestamp, data) VALUES
(7, '2014-07-11 11:25:48.185', LOAD_FILE('sql/file.bin'));
I was thinking NOT NULL was there for it.
To prevent inserts with an empty value for ID (or not value passed), simply define the column as NOT NULL as you defined it.
I can't see how your example worked (i.e. inserting only into (timestamp, data)).
Now, the fact that there is another table with a trigger that inserts in this one does not have any effect on the ID column of this table. If you define it as AUTO_INCREMENT, whenever you insert a new row, the ID will automatically get a new value which will be fully independent from any data of the first table.
You can have as many tables as you wish with auto-incremented fields, each running a different sequence (and hence their numbering will be fully independent).
To summarize:
CREATE TABLE IF NOT EXISTS my_table(
id INT NOT NULL AUTO_INCREMENT ,
timestamp TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ,
data BLOB NULL ,
PRIMARY KEY (id)
);
I have this SQL statement for inserting data into my database, I just need to update instead when a duplicate value is entered, how can I do this do I need to use the primary key or can I use a specific value
The auto_incremented column that you see is the primary key called license_id
SQL CODE
INSERT INTO License
(license_id,
license_number,
start_date,
end_date,
duration,
expiry_date)
VALUES
('5',
'00005555',
'2015-11-22',
'2015-11-23',
'1',
'2015-11-24')
on duplicate key update ????
What do I fill in by the question marks
I just done something like this, it didnt work
INSERT INTO License
(license_id,
license_number,
start_date,
end_date,
duration,
expiry_date)
VALUES
('5',
'00005555',
'2015-11-23',
'2015-11-24',
'1',
'2015-11-25')
on duplicate key update license_id = values(license_id)
There is no point in doing on duplicate key update license_id = values(license_id) because the license_id you're trying to insert already exists in the table and it would just change the license_id of an existing record to the same value as before. Rather, the on duplicate key update is intended to update the other values. In your case it could maybe be, for example,
on duplicate key update
license_number = values(license_number)
start_date = values(start_date),
end_date = values(end_date),
duration = values(duration),
expiry_date = values(expiry_date);
I found how to do it, i tried without the primary key and the database just got confused, it entered a new record so I did this, used a different table
INSERT INTO Product (product_id,product_name, version) VALUES ('1', 'nfinityX', '5')
on duplicate key update product_id='1', product_name='infinity', version='5';
There is alot of related code that you have not seen, which makes it hard for you guys to understand the context of what I mean, but this seams to have worked, as you can see above.
I had that record already, and tested what I wrote above and it worked, I had to put primary key so it knows what row to update, otherwise it will update other rows and get confused. Is this the right way, I dont know.
SQL query:
INSERT INTO `website_categorization`.`category_keyword` (
`ID` ,
`ID_Category` ,
`Keyword` ,
`Score`)
VALUES (
NULL , '18', 'free mail', ''
), (
NULL , '18', 'web email free', ''
)
MySQL said:
#1062 - Duplicate entry '18-free mail' for key 'ID_Category'
It shows this duplicate entry error even though there is no entry at row no 1062. ( ID is primary key, and unique(ID_Category,Keyword) ).
Can u help me in this?...
You already have a row in your database with the values '18' and 'free mail'. You can't have two such rows because of the unique constraint. You have some choices:
Remove the original row and try your insert again: DELETE FROM yourtable WHERE ID_Category = '18' AND Keyword = 'free mail'.
Remove the unique constraint to allow both rows to exist.
Use INSERT IGNORE to ignore the error.
Use REPLACE instead of INSERT to replace the old row with the new row.
Attempt the INSERT knowing that the client-side will be alerted of the error.
Well, it means that the data you are inserting breaks the unique constraints. From the error messasge I'd say some data already exists with the pair (18, 'free mail') - you say that is constrained to be unique.
The row number is not an indication, because it doesn't correspond to the key.
Your ID_category key is declared as unique and thus you cannot have two entries with the same value.
If your ID field is truly a primary key, it is mostly likely (or should be) auto-incremented. So leave that field out of the INSERT query.
That is MySQL Error number 1062, not row number. The error means duplicate entry. You are inserting NULL and '18' twice in ID and ID_Category respectively, so it will throw this error the 2nd time you do it. ID_Category is very likely the name of your index. You can do a
show index from website_categorization.category_keyword
to see the index name.
So I have a table like this:
create table `test` (
`testId` int(11) not null auto_increment,
`text` varchar(10) not null default '',
primary key(`testId`),
unique(`text`)
) engine=innodb;
My insert would be
insert into test (text) values ('a');
insert into test (text) values ('b');
insert into test (text) values ('a');
the 3rd insert will fail, but I want it to return the testId for the duplicate (for 'a').
Is this possible without writing a second query?
You can't do this in an INSERT query because the INSERT does not return any rows.
When an INSERT fails because of a duplicated key - normally the thing that built the query knows what data it sent, so it could use this.
You may be able to achieve what you want by using 12.2.5.3. INSERT ... ON DUPLICATE KEY UPDATE
If a table contains an AUTO_INCREMENT
column and INSERT ... UPDATE inserts a
row, the LAST_INSERT_ID() function
returns the AUTO_INCREMENT value. If
the statement updates a row instead,
LAST_INSERT_ID() is not meaningful.
However, you can work around this by
using LAST_INSERT_ID(expr). Suppose
that id is the AUTO_INCREMENT column.
To make LAST_INSERT_ID() meaningful
for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
There are two ways of doing this. If you just want to replace the existing data, you can use
REPLACE INTO <table_name> VALUES <values>
That is the simplest thing. But if you just want to check whether the data exist, you can first, do this
SELECT COUNT(*) FROM <table> WHERE <field>='<field_value>
If exists, query returns more than 0.
Hope this helps.