I want to create an event than immediately register the user who created that event in an events attendance table. No field for the event is unique, except the primary key and this is assigned by the database. Is there anyway to get a row number or specific field from the last row affected by a non-query on an active database connection?
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
See the MySQL documentation for more info: 28.6.28.3 How to Get the Unique ID for the Last Inserted Row
Related
I want to fetch data in texbox1 event and insert that fetched data into another table based on primary key search it doing perfectly like on primary key 8.
But when I want to enter new data like 9 primary key it adds 0 primary key on blank texbox1 how to prevent this
MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.
By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
ALTER TABLE YourTable AUTO_INCREMENT=100;
I am using LINQ-to-SQL class. I am inserting a new row using LINQ method object.InsertOnSubmit().
I need to set same value which is generate by SQL Server (using Identity) for table primary key column.
Now I need the same value at the time of inserting new row into table. And set the same value for other column in the same table at the time of insert only.
As I cannot update as after inserting because table has UPDATE TRIGGER.
I tried the following
_db.EmpNews.InsertOnSubmit(_EmpNews);
...
_db.DisplaySeq = _EmpNews.ID;
...
_db.SubmitChanges();
Where ID is the auto-generated (Identity) column.
The first question really is: why would you need to store the same value in two separate columns in the same table? What do you need this for? Doesn't seem to make a lot of sense to me....
Since the value of the IDENTITY column is only available once the row has actually been inserted, there is no way to get that value and set it to another column before the row has indeed been saved to the database table.
That basically leaves three options to get that value and store it somewhere else:
you can write an AFTER INSERT trigger that just set the other column to the value that's just been inserted in the IDENTITY column
you could wrap the whole saving process into a stored procedure which you call from your C# code (instead of just saving the object) and you would do the INSERT of the row, then get the newly created IDENTITY value and update the row again with that new value. But that would cause an UPDATE to happen - which you seem to say is impossible for you because of an UPDATE trigger (not quite clear on why this should be a problem....)
you can write two lines of C# code to get the IDENTITY value after it's been inserted (and available in the ID property of your object) and then store the object a second time. But that, too, would cause an UPDATE to happen - which you seem to say is impossible for you because of an UPDATE trigger (not quite clear on why this should be a problem....)
So I guess your best option would be an INSERT trigger to do this.
Try something like this:
CREATE TRIGGER trInsertEmpNews
ON dbo.EmpNews AFTER INSERT
AS BEGIN
UPDATE dbo.EmpNews
SET DisplaySeq = i.ID
FROM INSERTED i
WHERE dbo.EmpNews.ID = i.ID
END
I have question, how can i insert a new data into a database that the primary key and foreign key is always equal in value?
ex. i entered my name into Name table and that Name table has PK and FK. every time i insert a new data, the FK was empty. i expect that the value of FK is same as the value of PK even they have different field name.
above is my database relationship. every time i insert new data the EventsID pk(Eventstbl) wont copy to EvnetsID FK(Organizationtbl)
The referential integrity does not work as you described. It better suits functionality of the triggers. The purpose of the PK and foreign key constraint is to prevent insertion of data which is not exist in other table as PK. Therefore, if you want to copy data from Eventstbl to Organizationtbl upon inserting a new record to the former, you need to write a trigger for the insertion event of the Eventstbl. Your PK - FK constraint will work like following, when you insert new record to Organizationtbl, it will check Eventstbl table for the corresponding EventsID. If it does not exist, it will not allow you to insert new record to Organizationtbl. I hope it helps.
Well, you can use a trigger in EventsTbl, an after insert / update trigger. So this trigger could insert / update the other table you need. You can use the INSERTED table to catch the new value of the PK. I hope it helps.
I'm imagining that this is a simple question, but I'm running into syntax issues I think.
I have a table that has an event ID as primary key(eventID), a device ID that of what triggered the event, and a timestamp of when the event triggered. I"m processing the mySQL(5.1) elements via C# and command line.
My dilemma is that I have to duplicate/import the database to another database and I am getting duplicate entries due to the eventID being the primary key and it being updated everytime a new record is added automatically. Changing that isn't an option. So what I want to do is only insert into the database if my current deviceID+timestamp combination doesn't already exist.
I've found a lot of query examples, but they either throw syntax issues or simply don't work as needed. In pseudo code this is what I'm looking for..
IF timestampToInsert EXISTS WHERE deviceIDtoInsert ALSO EXISTS ON SAME ROW
UPDATE ROW
ELSE
INSERT INTO myTABLE (deviceID, timeStamp) VALUES (deviceIDtoInsert, timestampToInsert)
It sounds like you want to do an "upsert".
In MySQL this is called "INSERT...ON DUPLICATE KEY UPDATE":
INSERT INTO myTABLE (deviceID, timeStamp)
VALUES (deviceIDtoInsert, timestampToInsert)
ON DUPLICATE KEY UPDATE ...
Note this will only work if you have a unique index on (deviceID,timestamp)
I'm subsequently inserting a single record in two tables. With the first insert i put the record in the main table, where the primary key value gets generated. Now i need to put some fields of this record in another table including the key value of the main table. How do i do this?
"SELECT LAST_INSERT_ID();" will return the last autoincrement ID generated.