Good Day
I created a table, NEW_TABLE, from some of another table columns ,OLD_TABLE.
I added a new column ID of type double
I want to fill the values of the ID column with unique values and then make it the the NEW_TABLE key.
Is there a way to do this in MySQL with a query or a set command?
I tried something like this:
Insert into NEW_TABLE
(select generateId() , col1, col2
from ORIGINAL_TABLE)
Usually you set the field to be an auto increment field when it is defined. To do so afterwards, you can use:
ALTER TABLE NEW_TABLE MODIFY ID int(10) unsigned NOT NULL auto_increment;
To then insert an new record and for it to automatically get an assigned ID, merely omit the field from the insert.
try this:
Insert into NEW_TABLE
(select #row := #row + 1 as generateId, col1, col2
from ORIGINAL_TABLE, (SELECT #row := 0)row)
You should use autoincrement and an integer field (is there any reason for you to want a double key there?):
CREATE TABLE NEW_TABLE (
id INT NOT NULL AUTO_INCREMENT,
col1 CHAR(30) NOT NULL,
col2 CHAR(30) NOT NULL,
PRIMARY KEY (id)
)
Why did you choose DOUBLE and not an integer datatype?
ALTER TABLE NEW_TABLE
MODIFY ID INT UNSIGNED NOT NULL AUTO_INCREMENT ;
ALTER TABLE NEW_TABLE
ADD CONSTRAINT new_table_pk
PRIMARY KEY (ID) ;
and then:
INSERT INTO NEW_TABLE
(col1, col2)
SELECT col1, col2
FROM ORIGINAL_TABLE ;
Related
This question already has answers here:
How can I do 'insert if not exists' in MySQL?
(11 answers)
Closed 6 years ago.
I used the following command to avoid duplicates in a table :
INSERT INTO mytable (num,name)
SELECT 2,'example' FROM mytable WHERE NOT EXISTS
(SELECT * FROM mytable WHERE num=2 AND name='example') LIMIT 1;
It is working but NOT if mytable is empty.
mytable also contain a AUTO_INCREMENT id.
CREATE TABLE mytable (
id int(11) NOT NULL auto_increment,
num int(11) NOT NULL,
name varchar(100) NOT NULL,
PRIMARY KEY (id)
);
Do you recommanded another method or a workaround ?
In my case replacing mytable by DUAL did the trick. (But i have no idea why)
INSERT INTO mytable (num,name)
SELECT 2,'example' FROM mytable WHERE NOT EXISTS
(SELECT * FROM mytable WHERE num=2 AND name='example') LIMIT 1;
Replaced by :
INSERT INTO mytable (num,name)
SELECT 2,'example' FROM DUAL WHERE NOT EXISTS
(SELECT * FROM mytable WHERE num=2 AND name='example') LIMIT 1;
Thanks for the help.
You can create Unique column by using MySQL UNIQUE
Just update your CREATE TABLE query like below
CREATE TABLE mytable (
id int(11) NOT NULL auto_increment,
num int(11) NOT NULL,
name varchar(100) NOT NULL,
PRIMARY KEY (id),
UNIQUE (num) # You can define Unique column like this
);
Note: Then you no need to check Unique value when Save to Database.
And try to use normal SQL query for inserting Data
INSERT INTO mytable (num,name) VALUES(2,'example')
You can do something like
Select * from tbl where _____ = ______
Then add if statement if results are available then insert or otherwise leave
Why do not you simply use INSERT IGNORE. Also why do you want to copy the data from same table?
I encountered an issue with MySQL, adding fields from the selected source table.
Here is an example (you can find it on SQL Fiddle :
CREATE TABLE source_table (
id INT UNSIGNED NOT NULL,
foo VARCHAR(3),
INDEX (id)
);
INSERT INTO source_table (id, foo) VALUES (1, "one");
// Create another table and fill it from the source_table
CREATE TABLE example_table (
id INT UNSIGNED NOT NULL,
bar VARCHAR (3),
INDEX (id)
) SELECT
source_table.id,
source_table.foo
FROM source_table
WHERE source_table.id = 1;
In the end, my example_table will have a foo field, that I never requested to be create.
Plus, bar will be empty while foo will be filled.
The solution I found is to use aliases for each field, but it's redundant:
CREATE TABLE example_table (
id INT UNSIGNED NOT NULL,
bar VARCHAR (3),
INDEX (id)
) SELECT
source_table.id AS id,
source_table.foo AS bar
FROM source_table
WHERE source_table.id = 1;
Is there any trick in MySQL's configuration to avoid this behaviour? I feel like it would create surprising tables.
This is the documented behaviour of insert ... select ..., so there is no way to configure it to avoid this behaviour:
MySQL creates new columns for all elements in the SELECT. For example:
mysql> CREATE TABLE test (a INT NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (a), KEY(b))
-> ENGINE=MyISAM SELECT b,c FROM test2;
This creates a MyISAM table with three columns, a, b, and c.
I am trying to use a trigger to insert the updated value to another table. I have two tables (table1 and table2).
I want to copy the new primary key of table1 to a non-primary key field in table2 in hexadecimal value. the primary key is integer type and the other field is varchar.
The code is as follows:
delimiter /
drop trigger if exists a /
create trigger a before insert on table1 for each row
begin
insert into table2 set table1ID= hex(new.id);
end /
insert into table1 set name='Ronnie';
But, the problem is when i insert into table1, the primary key will not be added to table2. it will add 0 instead.
These are the tables:
create table table1 (
id integer not null auto_increment primary key,
name varchar(45) not null
);
create table table2 (
id integer not null auto_increment primary key,
table1ID varchar(45) not null
);
Your trigger is "before insert", and the new generated id is not available yet. Change it to "after insert":
create trigger a after insert on table1 for each row
begin
insert into table2 (warehouseID) values (hex(new.id));
end /
Please see it here.
Dear MySQL pros out there: I wonder what I am doing wrong. My code is like:
use testdb;
drop table testtable;
create table testtable (
ID int NOT NULL,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20));
Select * from testtable;
alter table testtable auto_increment = 7001;
insert into testtable (lastn,firstn) values('kim','jeff');
Select * from testtable;
insert into testtable (lastn,firstn) values('Lee','jim');
Select * from testtable;
The table generated as follows: (no effect from "alter" statement)
# ID, lastn, firstn
'0', 'kim', 'jeff'
'0', 'Lee', 'jim'
Either change your CREATE TABLE command to set the ID field to auto increment and initialise it like this:
create table testtable (
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20))
AUTO_INCREMENT = 7001;
or alter the table afterwards:
create table testtable (
ID int NOT NULL,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20));
ALTER TABLE testtable MODIFY COLUMN ID INT PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE testtable AUTO_INCREMENT = 7001;
ID needs to be both AUTO_INCREMENT and the PRIMARY KEY. (Those are "sufficient" but not completely "necessary".)
I have a table mytable( id, key, value). I realize that key is generating a lot of data redundancy since my key is a string. (my keys are really long, but repetititve) How do I build a separate table out that has (key, keyID) and then alternate my table to be mytable( id, keyID, value) and keyTable(keyID, key) ?
Create keyTable
Fill keys from mytable:
INSERT INTO keyTable (`key`) SELECT DISTINCT mytable.key FROM mytable;
add keyID column to mytable
Assign keyIDs:
UPDATE mytable SET keyID = (SELECT keyTable.keyID FROM keyTable WHERE keyTable.key = mytable.key);
Remove key column from mytable
i just posted my workout for your problem. Just check this step by step:
CREATE TABLE `keytable` (
`keyID` INT( 11 ) NOT NULL auto_increment,
`key` VARCHAR( 100 ) NOT NULL,
`id` INT( 11 ) NOT NULL
) ;
insert into `keytable` (`key`,`id`) select `key`,`id` from mytable;
ALTER TABLE `mytable` CHANGE `key` `keyID` INT( 11 ) NOT NULL ;
update `mytable` set `keyID`= (select `keyID` from keytable where keytable.id=mytable.id)
ALTER TABLE `keytable` DROP `id` ;