I'm working on a servlet that needs to insert some data to the db table with a composite primary key consists of the userid, dataid and CURRENT_TIMESTAMP.
however im getting the following error when executing the query
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry'13-7-2013-09-13 23:22:24' for key 'PRIMARY'
I think this is caused by the multiple insertion of rows to the same table in the same time, though with different dataid. Is there any solution to this problem? Should I cancel CURRENT_TIMESTAMP as a part of the primary key to do the trick or there are some other better workarounds?
Thanks a lot and appreciate for any help!
If it's a log table, it is recommended not to use a primary key. If you want to accelerate some search in this table, create the appropriate indexes.
If you need a primary key (for example, if you plan to use it with JPA), it would be best to use a number, e.g.
ID int AUTO_INCREMENT PRIMARY KEY
For example, log4j can insert each event log into a database using a org.apache.log4j.jdbc.JDBCAppender.
See also MySQL storage engine for a large log table.
Related
I'm trying to make a User-Bundle relation, where the relation between them is many-many.
So, to break this many to many into 2 one-to-many, I've created another table called subscription.
The subscription table has primary keys of both user and bundle as foreign keys. The Subscription table has a composite key made of (userId,bundleId), where both these columns are not null in their respective table.
Now, when I try to insert data in the subscription table using mybatis, it gives error userId cannot be null.
The reason being, mybatis sql is converted to a prepared statement which is used for dynamic parameters.
Here's an insert statement to give you a rough idea about my subscription table
INSERT INTO Subscription (USERID,BUNDLEID,BUNDLESTARTDATE)
VALUES (#{userId},#{bundleId},#{promotionStartDate});
Although I'm passing the userId, it doesn't accept it and calls userId cannot be null. I'm fairly sure this is due to the prepared statement. Please help.
I am having an issue with inputting data from a csv file into a mysql database using JDBC. I have already inserted a table called 'Poet' with the 'PoetName' being the priamry key.
I have populated that table with records that i have held in a CSV file, I then created another table called 'Poem' and it contains a foreign key which is 'PoetName' which references the poet table however whenever I try to populate the table with the CSV file for poems that contains matching values I am being displayed with the following error-
"Cannot add or update a child row: a foreign key constraint fails"
This is strange because the values I have for the primary key 'PoetName' are the same for the values I have used for the foreign key.
Does anyone have an idea of what the issue may be?
Thanks
If you're running into this error, one quick workaround is to add this line to your script:
SET FOREIGN_KEY_CHECKS = 0;
Another note is generally you want your primary key to be of type INT.
So I have a MySQL database schema where there is a USERS table which contains the ID as a primary key for that table, I also have a USER_PASSWORDS table which references the USERS table where the USER_ID will act as a foreign key in this table.
The issue that I am facing is that I am writing an application where the user will be able to sign up and specify a username and password. But I would like to insert the user into the database with one query.
I was thinking I had to insert the username first into the USERS table and see what ID has been given to that username and then insert the hash of the password that the user has entered into the USER_PASSWORDS table and specifying the ID that was queried.
I dont like this approach because it means that I have to:
INSERT into the database
QUERY the database
INSERT into the database again
Is there a better way of doing this?
Thanks
You can't insert into two tables with one insert statement, and you would have to query the users table anyway to get the ID value to insert as a foreign key for the user_passwords table.
Really the only way to do what you want is the solution you've already identified:
Insert into the Users table
Query to get the ID of the User you just inserted
Insert into the USER_PASSWORDS table with the ID you obtained for the User.
You could wrap all this up in a stored procedure that takes user data and password as parameters, which would be the "better" way of doing it.
As you didnt really tell for what system / programming language you need this and you did not provide any code example either, I can only give you some theory what you could do:
Its impossible to insert data into two different MySQL tables with one queries but you can reduce your script atleast by the SELECT query:
1.) There is a function in most mysql apis (Depending on what programming language and MySQL Library you are using) that says "getLastInsertId()", "lastInsertId()" or similar.
This will return the ID that was inserted by the auto-increment of the table after the insert is completed.
Just check the docs of your MySQL-api it will have such a function.
2.)
The second possibility is using a UUID - a very large (commonly 128-bit) long string which is generated totally random. There are more so many possible combinations it will happen more probably that you win in the lottery 10 times in a row then you generate two times the same UUID that is already in your table.
So you just generate the UUID and insert it as a key in both tables and you are done.
Just use google to find out-of-the-box libraries to generate UUID's you dont need to build the alogrithm on your own.
An UUID could looks like this:
4a34fe87-f577-4ea9-9557-1bc8f779a68c
One solution: since the hash is unique you can use the hash as a primary key in the USERS table. Then you know what the primary key (id) is at the time of the insert and can reuse when INSERTING in the USER_PASSWORDS table.
That way you can avoid the id query at least.
Anyway to do Hibernate reverse engineering without putting foreign key in objects.
I am trying to do a Hibernate reverse engineering on my mysql database but I dont want the objects to show foreign keys.. Can this be done?
Exactly same problem here, you should use a new acces to this DB where alter table is not allowed.
So: create a new user with a DBA not add alter table permission and use this connection parameter to acces to this DB, this way alter table will not possible!
(workaround i know)
And the cause can be for this workaround e.g. Liferay doesn't use foreign keys and if hibernate mess up the table, the whole system fall apart...
I'm working on a legacy project. The database is poorly designed. I want to change the DB layer now. The first think go through my mind is hibernate, but I hibernate need a primary key on my table. In fact some of my table does not have primary key. So I did a google search and I find iBatis, it's sounds very good with it. But I don't know whether iBatis enforce a primary key on my table?
Thanks.
No. IBatis does not force you to have a primary key on your table. Its primary job and strength is to map data from resultsets to java objects. The SQL statements to retrieve the data from the database are written by hand (you), so you have almost no constraints or limitations here.