Need to insert same Date of Birth for Different person. Ie. I have created a web page where student registers. in MySQL I have created DOB field and if any student with same dob inserted it tells Duplicate entry and record is not inserted. i need to insert the record for DOB
INSERT INTO `degree` (`Candidate`, `Father`, `Course`, `Year`, `DOB`, `Roll`)
VALUES ('AAAA\r\n', 'AAAAA\r\n', 'AAA', '2199', '1933-06-21', 'AAAAAAA')
MySQL said:
Documentation
#1062 - Duplicate entry '1933-06-21' for key 'DOB'
You have a unique key on the DOB field that needs to be removed.
Find the name of the index in the output of
show create table degree
and then remove the unique key with:
alter table degree drop index NAME_OF_INDEX
There's a good chance you still want an index on the field, just not a unique one in which case you can re-add it wth
alter table degree add index (DOB)
Related
CREATE TABLE IF NOT EXISTS students (
student_id INT,
name VARCHAR(24),
major VARCHAR(24),
PRIMARY KEY(student_id)
);
SELECT * FROM student;
INSERT INTO students VALUES(1,'Jack','Biology');
You're specifying the primary key (student_id) and from the error it already exists. You have a few options:
Don't specify the primary key. It should be set to autoincrement anyway, assuming that this is the primary table that students are entered into, and from the name of the table (students) it seems like it is. Then the query will be:
INSERT INTO students VALUES('Jack','Biology');
and then the table will autoincrement the primary key to the next pointer.
Use INSERT IGNORE. This will silently fail if you try to insert a student ID that already exists (or on any query that violates unique keys).
INSERT IGNORE INTO students VALUES(1, 'Jack','Biology');
This will not cause table changes, but it will also not cause an error that interrupts the script, and it will insert any rows that don't fail, say if you had multiple values inserted. The plain INSERT will fail for the entire list, not just the erroneous value.
Use ON DUPLICATE KEY UPDATE. This will update a list of values if it encounters a duplicate key.
INSERT INTO students VALUES(1, 'Jack','Biology')
ON DUPLICATE KEY UPDATE name = values(name), major = values(major);
In this case, you will change the values in the table that match the key. In this case, whichever student is student_id 1 will have its name and major updated to the supplied values. For instance, let's say that Jack changed his major to Chemistry. This would update student_id 1 to Jack, Chemistry and reflect his new major.
Use REPLACE INTO. I avoid this one. It is similar to ON DUPLICATE KEY UPDATE, but it removes the old entry and replaces it with a new one with a new ID. This can cause you problems with foreign keys, and also if you have a small primary key and you constantly replace into it, you can end up with a primary id that's bigger than the limits you set.
Well, your student_id is primary key, clearly that table is already exist with some data with student_id=1 hence you cannot insert another row with the same primary key value.
I have a table for updating times for a game, in which columns are unique to avoid duplicates.
My problem is the following: how to update a particular column only and avoid inserting a new row if they are the same?
Here is the query.
I did this code and it still enters a new row instead of updating the Time Column only:
INSERT INTO `leaderboard` (`Persona`, `Level`, `Track`, `Car`, `Mode`, `Time`,
`Date`, `Timestamp`, `HMS`)
VALUES ('STIG', 80, 'SPA FRANCORCHAMPS',
'AUDI R8 LMS ULTRA', 'HD', '02:17.332',
'2014-12-06', '1417825487', '1:24:47')
ON DUPLICATE KEY UPDATE `Time` = '02:17.332';
You are actually creating a composite unique key in:
ALTER TABLE `leaderboard`
ADD UNIQUE KEY `Persona` (`Persona`,`Level`,`Track`,`Car`,`Mode`,`Time`,`Date`,`Timestamp`,`HMS`);
It means that the tuple (Persona, Level, Track, Car, Mode, Time, Date, Timestamp, HMS) will be unique. However, the time columns are to be updated, and it is very likely they won't be unique.
Maybe what you want to do is:
ALTER TABLE `leaderboard`
ADD UNIQUE KEY `Persona` (`Persona`,`Level`,`Track`,`Mode`);
I have a table called Book_details, which has a primary key as Bid and now this BID is also a foreign key for other two table in the same database (I.e slend_details, Book_inventory)
When I insert data into inventory_details table, I'm getting the following error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__book_invent__Bid__1367E606". The conflict occurred in database "library_management", table "dbo.Book_details", column 'Bid'.
The statement has been terminated.
This is the insert statement I used:
insert into Book_inventory (Bid, present) values (001,1)
This is the create statement I used for my book_inventory table:
create table book_inventory (
Bid varchar(50) Not null Foreign Key references Book_details(Bid),
present bit Not null*
)
I have even checked the values i.e the data I am trying to enter in book_inventory table is present in the Book_details table. Still I get the error. Can someone help me out?
Your insert;
insert into Book_inventory (Bid,present) values (001,1)
inserts Bid as an integer, which most likely means that it simplifies the value to 1 which does not exist as a Bid.
Quoting the value and inserting it as an actual string should work;
insert into Book_inventory (Bid,present) values ('001',1)
column Bid is varchar and you are using it as integer. try this query
insert into Book_inventory (Bid, present) values ('001',1)
I've been reading up on how to use MySQL insert on duplicate key to see if it will allow me to avoid Selecting a row, checking if it exists, and then either inserting or updating. As I've read the documentation however, there is one area that confuses me. This is what the documentation says:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed
The thing is, I don't want to know if this will work for my problem, because the 'condition' I have for not inserting a new one is the existence of a row that has two columns equal to a certain value, not necessarily that the primary key is the same. Right now the syntax I'm imagining is this, but I don't know if it will always insert instead of replace:
INSERT INTO attendance (event_id, user_id, status) VALUES(some_event_number, some_user_id, some_status) ON DUPLICATE KEY UPDATE status=1
The thing is, event_id and user_id aren't primary keys, but if a row in the table 'attendance' already has those columns with those values, I just want to update it. Otherwise I would like to insert it. Is this even possible with ON DUPLICATE? If not, what other method might I use?
The quote includes "a duplicate value in a UNIQUE index". So, your values do not need to be the primary key:
create unique index attendance_eventid_userid on attendance(event_id, user_id);
Presumably, you want to update the existing record because you don't want duplicates. If you want duplicates sometimes, but not for this particular insert, then you will need another method.
If I were you, I would make a primary key out of event_id and user_id. That will make this extremely easy with ON DUPLICATE.
SQLFiddle
create table attendance (
event_id int,
user_id int,
status varchar(100),
primary key(event_id, user_id)
);
Then with ease:
insert into attendance (event_id, user_id, status) values(some_event_number, some_user_id, some_status)
on duplicate key
update status = values(status);
Maybe you can try to write a trigger that checks if the pair (event_id, user_id) exists in the table before inserting, and if it exists just update it.
To the broader question of "Will INSERT ... ON DUPLICATE respect a UK even if the PK changes", the answer is yes: SQLFiddle
In this SQLFiddle I insert a new record, with a new PK id, but its values would violate the UK. It performs the ON DUPLICATE and the original PK id is preserved, but the non-UK ON DUPLICATE KEY UPDATE value changes.
I am trying to write a query to check if a record exists (based on couple of clause and not unique identifier) if such a search return records then I need to update all the found records if nothing found then I need to INSERT a record. Note that I can't use IF EXISTS because I am trying to make a query for a client side script and not a server side. So I came a cross the idea of INSERT INTO .... ON DUPLICATE KEY
Can I do this without knowing the row key identifier? So if I find a record where accountid = 17 and name = 'Mike' then update it to make the name 'Mike A' if there is no record with these 2 clause then insert a record.
This is an attempt that is giving me a syntax error
INSERT INTO test (name, accountid) VALUES ('Mike', 17)
ON DUPLICATE KEY
UPDATE test SET name='Mike A' WHERE name ='Mike' AND accountid = 17
Can this method handle what I am trying to do? If yes then can you please correct my syntax?
Thank you
The only way you can get this to work is if you have a primary key or unique constraint on the fields. From the documentation:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an
UPDATE of the old row is performed. For example, if column a is
declared as UNIQUE and contains the value 1, the following two
statements have identical effect:
create table test (name varchar(100), accountid int);
insert into test values ('Mike', 17);
alter table test add unique (name, accountid);
insert int test (name, accountid) values ('Mike', 17)
on duplicate key update name='Mike A';
SQL Fiddle Demo
Without the unique key, it will insert a duplicate record.