after starting (and almost finishing) a database project for school, I found out I was supposed to use Oracle Express instead of MySQL. So when I brought in the code I had written from mysql to oracle, everything stopped working. I'm not too familiar with oracle databases but I assumed their syntax is quite simaler to MySqls.
Are there any structures in this file that are different in oracle DBs?
anything you guys can see would help. Thanks!
CREATE SCHEMA IF NOT EXISTS marys_limo_service;
use marys_limo_service;
DROP TABLE IF EXISTS Driver;
CREATE TABLE Driver(
d_id int,
d_name varchar(50),
d_contact varchar(30),
CONSTRAINT Driver_d_id PRIMARY KEY(d_id)
);
DROP TABLE IF EXISTS Limo;
CREATE TABLE Limo(
l_id int,
l_callsign varchar(12),
l_type varchar(40),
CONSTRAINT Limo_pk PRIMARY KEY(l_id)
);
DROP TABLE IF EXISTS Aclient;
CREATE TABLE Aclient (
c_id int,
c_name varchar(50),
c_contact varchar(30),
c_methpmt varchar(20),
CONSTRAINT Aclient_pk PRIMARY KEY(c_id)
);
DROP TABLE IF EXISTS Qualify;
CREATE TABLE Qualify (
q_id int,
l_id int,
d_id int,
CONSTRAINT Qualify_q_id_PK PRIMARY KEY(q_id),
CONSTRAINT Qualify_l_id_FK FOREIGN KEY(l_id) REFERENCES Limo(l_id),
CONSTRAINT Qualify_d_id_FK FOREIGN KEY(d_id) REFERENCES Driver(d_id)
);
DROP TABLE IF EXISTS Rental;
CREATE TABLE Rental (
r_id INT,
r_date DATE,
r_fee FLOAT,
c_id INT,
q_id INT,
CONSTRAINT Rental_r_id_PK PRIMARY KEY(r_id),
CONSTRAINT Rental_c_id_FK FOREIGN KEY(c_id) REFERENCES Aclient(c_id),
CONSTRAINT Rental_q_id_FK FOREIGN KEY(q_id) REFERENCES Qualify(q_id)
);
INSERT INTO Driver
VALUES
(1,"alf","9022332332"),
(2,"bob","9022322323");
INSERT INTO Limo
VALUES
(1,"Car One", "stretch limo"),
(2,"Car Two", "hummer limo"),
(3,"Car Three", "armored personnel carrier");
INSERT INTO Qualify
VALUES
(1,1,1),
(2,2,1),
(3,3,1),
(4,1,2),
(5,2,2);
INSERT INTO Aclient
VALUES
(1,"ann","9022332332", "cash"),
(2,"bub","9022322323", "CC");
INSERT INTO Rental
VALUES
(1, "2015/3/21", 550, 2, 1),
(2, "2015/3/21", 1000, 1, 5),
(3, "2015/3/20", 2050, 1, 3),
(4, "2015/3/19", 550, 1, 1),
(5, "2015/3/20", 500, 2, 4);
COMMIT;
-- Q.1
INSERT INTO marys_limo_service.driver
VALUES
(3,"Cal","9024919999"),
(4,"Dan","9024914545");
COMMIT;
-- Q.2
INSERT INTO Qualify
values(6,1,3),
(7,2,3),
(8,3,3),
(9,3,4);
select * from Qualify
-- Q.3
UPDATE Rental
SET q_id = 4
where
r_id = 1;
UPDATE Rental
SET q_id = 3
WHERE
r_id = 4;
select * from rental
-- Q.4
CREATE TEMPORARY TABLE Replace_contents
as (SELECT l_id, d_id FROM Qualify
WHERE d_id != 1);
In Oracle, privileged users (such as SYS) can create other users. That would look like this:
SQL> connect sys/pwd as sysdba
Connected.
SQL> select tablespace_name from user_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
SQL> create user so_test identified by test
2 default tablespace users
3 temporary tablespace temp
4 quota unlimited on users;
User created.
SQL> grant create session, create table, create view to so_test;
Grant succeeded.
Now, connect as newly created user and run code (I fixed); later I'll post code you can reuse. There's no drop if exists in Oracle, so I'm just dropping those tables (paying attention to FK constraints!) and ignoring dropping errors.
Other errors include:
use varchar2, not varchar (that's not exactly an error, but - Oracle recommends it)
use single quotes for strings, not double quotes
either specify date format mask (such as to_date('2015/03/21', 'yyyy/mm/dd')) or use date literal (e.g. date '2015-03-21') or alter session and specify desired format
terminate all commands with semi-colon
insert into should be used for each row separately, unless you use insert all or insert into ... select ... from
OK, here we go:
SQL> connect so_test/test
Connected.
SQL> -- drop them first, because of FK constraints
SQL> drop table rental;
drop table rental
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> drop table qualify;
drop table qualify
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> -- Now, your code, fixed
SQL> DROP TABLE Driver;
DROP TABLE Driver
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE TABLE Driver(
2 d_id int,
3 d_name varchar2(50),
4 d_contact varchar2(30),
5 CONSTRAINT Driver_d_id PRIMARY KEY(d_id)
6 );
Table created.
SQL> DROP TABLE Limo;
DROP TABLE Limo
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE TABLE Limo(
2 l_id int,
3 l_callsign varchar2(12),
4 l_type varchar2(40),
5 CONSTRAINT Limo_pk PRIMARY KEY(l_id)
6 );
Table created.
SQL> DROP TABLE Aclient;
DROP TABLE Aclient
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE TABLE Aclient (
2 c_id int,
3 c_name varchar2(50),
4 c_contact varchar2(30),
5 c_methpmt varchar2(20),
6 CONSTRAINT Aclient_pk PRIMARY KEY(c_id)
7 );
Table created.
SQL> DROP TABLE Qualify;
DROP TABLE Qualify
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE TABLE Qualify (
2 q_id int,
3 l_id int,
4 d_id int,
5 CONSTRAINT Qualify_q_id_PK PRIMARY KEY(q_id),
6 CONSTRAINT Qualify_l_id_FK FOREIGN KEY(l_id) REFERENCES Limo(l_id),
7 CONSTRAINT Qualify_d_id_FK FOREIGN KEY(d_id) REFERENCES Driver(d_id)
8 );
Table created.
SQL> DROP TABLE Rental;
DROP TABLE Rental
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE TABLE Rental (
2 r_id INT,
3 r_date DATE,
4 r_fee FLOAT,
5 c_id INT,
6 q_id INT,
7 CONSTRAINT Rental_r_id_PK PRIMARY KEY(r_id),
8 CONSTRAINT Rental_c_id_FK FOREIGN KEY(c_id) REFERENCES Aclient(c_id),
9 CONSTRAINT Rental_q_id_FK FOREIGN KEY(q_id) REFERENCES Qualify(q_id)
10 );
Table created.
SQL> INSERT INTO Driver VALUES (1,'alf','9022332332');
1 row created.
SQL> INSERT INTO Driver VALUES (2,'bob','9022322323');
1 row created.
SQL>
SQL> INSERT INTO Limo VALUES
2 (1,'Car One', 'stretch limo');
1 row created.
SQL> INSERT INTO Limo VALUES
2 (2,'Car Two', 'hummer limo');
1 row created.
SQL> INSERT INTO Limo VALUES
2 (3,'Car Three', 'armored personnel carrier');
1 row created.
SQL>
SQL> INSERT INTO Qualify VALUES
2 (1,1,1);
1 row created.
SQL> INSERT INTO Qualify VALUES
2 (2,2,1);
1 row created.
SQL> INSERT INTO Qualify VALUES
2 (3,3,1);
1 row created.
SQL> INSERT INTO Qualify VALUES
2 (4,1,2);
1 row created.
SQL> INSERT INTO Qualify VALUES
2 (5,2,2);
1 row created.
SQL>
SQL> INSERT INTO Aclient VALUES
2 (1,'ann','9022332332', 'cash');
1 row created.
SQL> INSERT INTO Aclient VALUES
2 (2,'bub','9022322323', 'CC');
1 row created.
SQL> alter session set nls_date_format = 'yyyy/mm/dd';
Session altered.
SQL> INSERT INTO Rental VALUES
2 (1, '2015/3/21', 550, 2, 1);
1 row created.
SQL> INSERT INTO Rental VALUES
2 (2, '2015/3/21', 1000, 1, 5);
1 row created.
SQL> INSERT INTO Rental VALUES
2 (3, '2015/3/20', 2050, 1, 3);
1 row created.
SQL> INSERT INTO Rental VALUES
2 (4, '2015/3/19', 550, 1, 1);
1 row created.
SQL> INSERT INTO Rental VALUES
2 (5, '2015/3/20', 500, 2, 4);
1 row created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
SQL> -- Q.1
SQL> INSERT INTO driver VALUES
2 (3,'Cal','9024919999');
1 row created.
SQL> INSERT INTO driver VALUES
2 (4,'Dan','9024914545');
1 row created.
SQL> COMMIT;
Commit complete.
SQL>
SQL> -- Q.2
SQL> INSERT INTO Qualify values
2 (6,1,3);
1 row created.
SQL> INSERT INTO Qualify values
2 (7,2,3);
1 row created.
SQL> INSERT INTO Qualify values
2 (8,3,3);
1 row created.
SQL> INSERT INTO Qualify values
2 (9,3,4);
1 row created.
SQL>
SQL> select * from Qualify;
Q_ID L_ID D_ID
---------- ---------- ----------
1 1 1
2 2 1
3 3 1
4 1 2
5 2 2
6 1 3
7 2 3
8 3 3
9 3 4
9 rows selected.
SQL>
SQL>
SQL> -- Q.3
SQL>
SQL> UPDATE Rental
2 SET q_id = 4
3 where
4 r_id = 1;
1 row updated.
SQL>
SQL> UPDATE Rental
2 SET q_id = 3
3 WHERE
4 r_id = 4;
1 row updated.
SQL> select * from rental;
R_ID R_DATE R_FEE C_ID Q_ID
---------- ---------- ---------- ---------- ----------
1 2015/03/21 550 2 4
2 2015/03/21 1000 1 5
3 2015/03/20 2050 1 3
4 2015/03/19 550 1 3
5 2015/03/20 500 2 4
SQL>
SQL> -- Q.4
SQL> drop table replace_contents;
drop table replace_contents
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE global TEMPORARY TABLE Replace_contents
2 as SELECT l_id, d_id FROM Qualify
3 WHERE d_id != 1;
Table created.
SQL>
Code you can reuse:
-- drop them first, because of FK constraints
drop table rental;
drop table qualify;
-- Now, your code, fixed
DROP TABLE Driver;
CREATE TABLE Driver(
d_id int,
d_name varchar2(50),
d_contact varchar2(30),
CONSTRAINT Driver_d_id PRIMARY KEY(d_id)
);
DROP TABLE Limo;
CREATE TABLE Limo(
l_id int,
l_callsign varchar2(12),
l_type varchar2(40),
CONSTRAINT Limo_pk PRIMARY KEY(l_id)
);
DROP TABLE Aclient;
CREATE TABLE Aclient (
c_id int,
c_name varchar2(50),
c_contact varchar2(30),
c_methpmt varchar2(20),
CONSTRAINT Aclient_pk PRIMARY KEY(c_id)
);
DROP TABLE Qualify;
CREATE TABLE Qualify (
q_id int,
l_id int,
d_id int,
CONSTRAINT Qualify_q_id_PK PRIMARY KEY(q_id),
CONSTRAINT Qualify_l_id_FK FOREIGN KEY(l_id) REFERENCES Limo(l_id),
CONSTRAINT Qualify_d_id_FK FOREIGN KEY(d_id) REFERENCES Driver(d_id)
);
DROP TABLE Rental;
CREATE TABLE Rental (
r_id INT,
r_date DATE,
r_fee FLOAT,
c_id INT,
q_id INT,
CONSTRAINT Rental_r_id_PK PRIMARY KEY(r_id),
CONSTRAINT Rental_c_id_FK FOREIGN KEY(c_id) REFERENCES Aclient(c_id),
CONSTRAINT Rental_q_id_FK FOREIGN KEY(q_id) REFERENCES Qualify(q_id)
);
INSERT INTO Driver VALUES (1,'alf','9022332332');
INSERT INTO Driver VALUES (2,'bob','9022322323');
INSERT INTO Limo VALUES
(1,'Car One', 'stretch limo');
INSERT INTO Limo VALUES
(2,'Car Two', 'hummer limo');
INSERT INTO Limo VALUES
(3,'Car Three', 'armored personnel carrier');
INSERT INTO Qualify VALUES
(1,1,1);
INSERT INTO Qualify VALUES
(2,2,1);
INSERT INTO Qualify VALUES
(3,3,1);
INSERT INTO Qualify VALUES
(4,1,2);
INSERT INTO Qualify VALUES
(5,2,2);
INSERT INTO Aclient VALUES
(1,'ann','9022332332', 'cash');
INSERT INTO Aclient VALUES
(2,'bub','9022322323', 'CC');
alter session set nls_date_format = 'yyyy/mm/dd';
INSERT INTO Rental VALUES
(1, '2015/3/21', 550, 2, 1);
INSERT INTO Rental VALUES
(2, '2015/3/21', 1000, 1, 5);
INSERT INTO Rental VALUES
(3, '2015/3/20', 2050, 1, 3);
INSERT INTO Rental VALUES
(4, '2015/3/19', 550, 1, 1);
INSERT INTO Rental VALUES
(5, '2015/3/20', 500, 2, 4);
COMMIT;
-- Q.1
INSERT INTO driver VALUES
(3,'Cal','9024919999');
INSERT INTO driver VALUES
(4,'Dan','9024914545');
COMMIT;
-- Q.2
INSERT INTO Qualify values
(6,1,3);
INSERT INTO Qualify values
(7,2,3);
INSERT INTO Qualify values
(8,3,3);
INSERT INTO Qualify values
(9,3,4);
select * from Qualify;
-- Q.3
UPDATE Rental
SET q_id = 4
where
r_id = 1;
UPDATE Rental
SET q_id = 3
WHERE
r_id = 4;
select * from rental;
-- Q.4
drop table replace_contents;
CREATE global TEMPORARY TABLE Replace_contents
as SELECT l_id, d_id FROM Qualify
WHERE d_id != 1;
I have a Friendship relation that maps two User's together.
Here are the two FKs:
fk_friend_one_id
fk_friend_two_id
I have a UNIQUE INDEX for (fk_friend_one_id, fk_friend_two_id) but you can insert the ids in both ways and it would still work:
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (1, 2);
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (2, 1);
How should I enforce uniqueness in this case?
Here are some solutions I have thought of:
Just always insert in both order.
Ensure that fk_friend_two_id > fk_friend_one_id. Sadly this cannot be enforced in the db level with MySQL because CHECK does nothing.
Starting with MySql 8.0.16 you can add a CHECK constraint that actually does something.
(Between MySql 5.7 & 8.0.16 you could add one, but it would just be ignored)
For MariaDb a working CHECK constraint was implemented since version 10.2.1. Reference
So if your version supports it, then it could be something like this example:
create table Friend (
id int primary key auto_increment,
name varchar(30) not null
);
insert into Friend (name) values
('John Doe'), ('Jane Shepard')
create table Friendship (
id int primary key auto_increment,
fk_friend_one_id int not null,
fk_friend_two_id int not null,
CONSTRAINT chk_friend2_gt_friend1 check (fk_friend_two_id > fk_friend_one_id),
CONSTRAINT fk_friend_one_id foreign key (fk_friend_one_id) references Friend(id),
CONSTRAINT fk_friend_two_id foreign key (fk_friend_two_id) references Friend(id),
CONSTRAINT idx_friends unique index (fk_friend_one_id, fk_friend_two_id)
);
insert into Friendship (fk_friend_one_id, fk_friend_two_id) value (2, 1);
CONSTRAINT `chk_friend2_gt_friend1` failed for `mydb`.`Friendship`
insert into Friendship (fk_friend_one_id, fk_friend_two_id) value (1, 1);
CONSTRAINT `chk_friend2_gt_friend1` failed for `mydb`.`Friendship`
insert into Friendship (fk_friend_one_id, fk_friend_two_id) value (1, 2);
select * from Friendship order by id;
id | fk_friend_one_id | fk_friend_two_id
-: | ---------------: | ---------------:
1 | 1 | 2
db<>fiddle here
(The other Answers are working too hard. All it takes is a UNIQUE and some simple SQL code.)
If you have values $x and $y to insert into a table, then be sure to put them in in a canonical order:
INSERT INTO tbl (a, b)
VALUES ( LEAST($x, $y),
GREATEST($x, $y) )
And have
UNIQUE(a, b) -- or, better yet, PRIMARY KEY(a,b)
Note that whether the values are (1,2) or (2,1), the only row that will be inserted is (1,2)
What should be done if you try to put both in? One will go in as (1,2); the other will get an error on the INSERT. You can have the error ignored via INSERT IGNORE. If there are other columns that need setting, consider using INSERT .. ON DUPLICATE KEY UPDATE ..
It can be achieved with stored procedures and triggers. You can try something like this:
DELIMITER $
CREATE PROCEDURE `friendship_check`(IN fk_friend_one_id INT, IN fk_friend_two_id INT)
BEGIN
IF fk_friend_one_id < fk_friend_two_id THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on friendship failed';
END IF;
END$
DELIMITER ;
-- before insert trigger
DELIMITER $
CREATE TRIGGER `friendship_before_insert` BEFORE INSERT ON `Friendship`
FOR EACH ROW
BEGIN
CALL friendship_check(new.fk_friend_one_id,new.fk_friend_two_id);
END$
DELIMITER ;
I have a table where player character information is kept. I have another table where all possible status affects are kept. I am trying to figure out how to create a relationship where any/all/none of the character entries in the first table can be linked to any/all/none of the status entries in the second table. My research has shown me that I will probably need a third table, and may have to use composite Primary Keys and/or surrogate keys?
So here is an update based on responses. I seem to have working what I was hoping to achieve. Here is the code:
create table characters (
char_id int primary key auto_increment,
name varchar(25)
);
create table health_status (
status_id int primary key auto_increment,
stat_name varchar(15)
);
create table char_status (
char_id int,
status_id int,
primary key (char_id, status_id)
);
insert into characters (name) values ('Godzilla');
insert into characters (name) values ('King Kong');
insert into characters (name) values ('Mecha-Dragon');
insert into characters (name) values ('Chris Hanson');
insert into characters (name) values ('Journey');
insert into characters (name) values ('Lou Diamond Phillips');
insert into characters (name) values ('RedHot');
insert into health_status (stat_name) values ('Bleeding');
insert into health_status (stat_name) values ('Shaken');
insert into health_status (stat_name) values ('Frightened');
insert into health_status (stat_name) values ('Petrified');
insert into health_status (stat_name) values ('Poisoned');
insert into health_status (stat_name) values ('Slowed');
insert into health_status (stat_name) values ('Rushed');
insert into health_status (stat_name) values ('Endowed');
insert into char_status (char_id, status_id) values (1, 1);
insert into char_status (char_id, status_id) values (1, 3);
insert into char_status (char_id, status_id) values (1, 6);
insert into char_status (char_id, status_id) values (2, 2);
insert into char_status (char_id, status_id) values (2, 4);
insert into char_status (char_id, status_id) values (3, 7);
insert into char_status (char_id, status_id) values (6, 8);
insert into char_status (char_id, status_id) values (7, 2);
insert into char_status (char_id, status_id) values (7, 7);
select characters.name, health_status.stat_name
from characters
left join char_status on characters.char_id = char_status.char_id
left join health_status ON health_status.status_id =
char_status.status_id;
I have three questions. Does anyone see any way this could be cleaned up, or a better way to achieve my goal? Also, is having the compound PK in the table char_status really doing anything useful? And finally, Is there a way to organize the output with a query to list the character name only once, followed by all its associated status - or is this something for a different language to do? Thanks for everyone's help!
Based on your structure you say you need to further create new table and put data into it.**
In order for you to connect between tableA and TableB you need to have
a Primary key in one and also its reference in other
**. Many to many relationship is something like a mutual one where tableA depends on many elements of tableB and also vice-versa.
You may have to normalize your tables first based on your situation.
Have a look at Normalization_tuorial
Next also have a look at one-many and many-many etc..
As you found in your research
create a third table with 2 fields that will hold the "id" (main field) from those 2 tables you want to make relation many to many...
CREATE TABLE `many_to_many` ( `information_id` INT(11) NOT NULL , `status_id` INT(11) NOT NULL , PRIMARY KEY (`information_id`, `status_id`)) ENGINE = MyISAM;
Pay attension to the PRIMARY KEY that depend on thos 2 fields.
Now you can add for every information_id many status_id relations,
and for status_id many information_id relations.
For first try, run this query to add values to see it
INSERT INTO `many_to_many` (`information_id`, `status_id`) VALUES (1, 1), (1, 2), (2, 1), (2, 2);
I'm having a problem with the auto increment id increasing when I don't want to it. I'm aware that the auto increment id increases when using INSERT IGNORE so I'm working around that, but still getting a behavior I can't figure out.
I'm building a normalized table of transactions, and in this table there is a column for first name which will have a reference table of transaction_first_names. My workflow is that I load data into a non normalized staging table, compare the values in the staging table with the values in the reference tables and if they do not exist into the reference table, then move the data from the staging table to the normalized table.
The issue I'm having is that when I try to insert any "new" values from the staging table into the reference tables, it seem to increment the autoincrement id's in the reference table in a way I can't explain. I wouldn't normally be ocd or stingy with id's, but as a continuing process I don't want the id's to continually be chewed through.
Here is my setup, link & code. As you can see in the second result the last inserted value was given the id of 16, whereas the goal is that id should be 9:
Runnable Example - http://rextester.com/KVMO89341
CREATE TABLE IF NOT EXISTS `transaction_first_names` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `u_first_name` (`first_name`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `transaction_stage` (
`transaction_id` BIGINT(20) UNSIGNED NOT NULL,
`first_name` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`transaction_id`),
INDEX `first_name` (`first_name`(191))
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
TRUNCATE transaction_stage;
TRUNCATE transaction_first_names;
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3658822144, 'Michael');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3658825319, 'Pete');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3658828867, 'Robert');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3658865656, 'Martin');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3659080925, 'Charlews');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3659943769, 'Christopher');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3660191699, 'Robert');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3660192662, 'Errol');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3660194469, 'Frank');
INSERT INTO `transaction_stage` (`transaction_id`, `first_name`) VALUES (3660200483, 'Frank');
-- first select
SELECT DISTINCT st.first_name
FROM transaction_stage st
LEFT JOIN transaction_first_names f ON st.first_name <=> f.first_name
WHERE f.id IS NULL
AND st.first_name IS NOT NULL;
-- first insert
INSERT INTO transaction_first_names (`first_name`)
SELECT DISTINCT st.first_name
FROM transaction_stage st
LEFT JOIN transaction_first_names f ON st.first_name <=> f.first_name
WHERE f.id IS NULL
AND st.first_name IS NOT NULL;
-- second insert
INSERT INTO transaction_first_names (`first_name`)
VALUES ('Another name');
-- check autoincrement
SELECT * FROM transaction_first_names order by id asc;
DROP TABLE IF EXISTS transaction_first_names;
DROP TABLE IF EXISTS transaction_stage;
I've tried wrapping the select distinct in the first insert statement, but no luck.
Ah, InnoDB handles things a bit differently depending on how the system variable innodb_autoinc_lock_mode is set.
For lock modes 1 or 2, gaps may occur between successive statements
because for bulk inserts the exact number of auto-increment values
required by each statement may not be known and overestimation is
possible.
https://dev.mysql.com/doc/refman/5.7/en/innodb-auto-increment-handling.html
my table looks like this
CREATE TABLE sample
(
id int auto_increment primary key,
W_ID varchar(20),
TC_ID varchar(20),
foo varchar(20),
bar varchar(20)
);
I want to insert a new row into this table, but if the combination of W_ID and TC_ID already exists, i want to update the row with the new values of 'foo' and 'bar'
I know there are a lot of similar questions on here, but i can't figure it out....
made a sample in sqlfiddle.com
i am using 5.6.11-MySQL - Apache/2.2.22 (Win32) PHP/5.3.25
you can create a unique key of the combination of W_ID and TC_ID then perform your upsert on it as follows:
CREATE TABLE sample
(
id int auto_increment primary key,
W_ID varchar(20),
TC_ID varchar(20),
foo varchar(20),
bar varchar(20)
);
alter table sample add constraint UNIQUE (W_ID, TC_ID);
INSERT INTO sample
(W_ID, TC_ID,foo,bar)
VALUES ('1', '2','123','123');
INSERT INTO sample
(W_ID, TC_ID,foo,bar)
VALUES ('2', '2','123','123');
INSERT INTO sample
(W_ID, TC_ID,foo,bar)
VALUES ('3', '2','123','123');
INSERT INTO sample
(W_ID, TC_ID,foo,bar)
VALUES ('1', '4','123','123');
INSERT INTO sample
(W_ID, TC_ID,foo,bar)
VALUES ('2', '3','123','123');
INSERT INTO sample
(W_ID, TC_ID,foo,bar)
VALUES ('1', '2','123','123')
ON DUPLICATE KEY UPDATE
`foo` = 'newFoo';
your results would look like:
ID W_ID TC_ID FOO BAR
1 1 2 newFoo 123
2 2 2 123 123
3 3 2 123 123
4 1 4 123 123
5 2 3 123 123
If you add unique constraint on the two columns you'll be able to use on duplicate key update syntax.
Adding an unique constrain should be something like that:
alter table table_name add unique index index_name(col1, col2);
You'll find more details here