I am setting up a table in mysql of engine type merge in mysql and was wondering if i have to have all my tables created previously that i want to merge. For example:
CREATE TABLE t1 (
a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
message CHAR(20)) ENGINE=MyISAM;
CREATE TABLE t2 (
a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
message CHAR(20)) ENGINE=MyISAM;
INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');
CREATE TABLE total (
a INT NOT NULL AUTO_INCREMENT,
message CHAR(20), INDEX(a))
ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;
Now if i have code that automatically created a t3 table i would have to modify the merge table to add this to the union? Would i use an ALTER query for that?
note: i am not using MySQL partitions because i have a mysql version 5.0.
Now if i have code that automatically created a t3 table i would have to modify the merge table to add this to the union? Would i use an ALTER query for that?
From the documentation:
To remap a MERGE table to a different collection of MyISAM tables, you can use one of the following methods:
DROP the MERGE table and re-create it.
Use ALTER TABLE tbl_name UNION=(...) to change the list of underlying tables.
Beginning with MySQL 5.0.60, it is also possible to use ALTER TABLE ... UNION=() (that is, with an empty UNION clause) to remove all of the underlying tables.
Related
I have a table with a unique auto-incremental primary key. Some entries have been deleted from the table, so there are gaps in the ids, it is not a sequence.
I need a query that will make it a sequence. the table is not connected with any other table, so I can temporarily remove the pk and auto-increment from the id column (until the ids will be a sequence).
I use SQL server
If possible, I want to run the query starting from specific id
You cannot update identity column values, nor you can remove the identity from the column, update the values and set it back. You must create a new table with the same schema, copy the data from the old table, but for ID generate a new value, drop the old table and rename the new one to keep the same name.
use [tempdb]
go
if OBJECT_ID('TestTable') is not null
drop table TestTable
go
create table TestTable (
ID int not null primary key clustered identity(1,1)
, Name varchar(50)
)
insert into TestTable(Name) values
('Row #1'),('Row #2'),('Row #3'),
('Row #4'),('Row #5'),('Row #6'),
('Row #7'),('Row #8'),('Row #9')
delete from TestTable where ID in (3, 4, 8) -- Make some gaps
create table TestTable_NEW (
ID int not null primary key clustered identity(1,1)
, Name varchar(50)
)
insert into TestTable_NEW(Name)
select Name
from TestTable
order by ID -- Preserve the rows order
drop table TestTable
exec sp_rename N'TestTable_NEW', N'TestTable'
However, I will not recommend doing this at all. Identity values are supposed to be immutable. They should never change. If you have problems with gaps, then do not allow deletion of existing records. Also, identity do not guarantee that there will be no gaps in the sequence. Only that the values will be unique and increasing. So you should definitely reconsider your database and/or application design, because it is flawed.
I need to partition my MySQL table using LIST COLUMNS partition. I already have the table with data so I need to alter the table to create the partition.
Here is what I am doing.
Sample schema:
CREATE TABLE mytable (
id bigint(20) primary key auto_increment,
.....
status varchar(10)
);
Alter table script that I am using:
ALTER TABLE mytable PARTITION BY LIST COLUMNS (status)
(
PARTITION p1 VALUES IN (NULL),
PARTITION p2 VALUES IN ('SUCCESS'),
PARTITION p3 VALUES IN ('FAILED')
);
Error: A PRIMARY KEY must include all columns in the table's partitioning function
However, when I just tried to create a table without id column as below:
CREATE TABLE mytable (
status varchar(10)
);
And then try to create the partition using the same above alter script, it worked. So, am I missing anything on the table with id?
SQLFiddle:
Working: http://sqlfiddle.com/#!9/c8a3d1
Not Working: http://sqlfiddle.com/#!9/d06a1
Ok, I think I found the solution. I had to change the primary key. The primary key should include both the id and the status column for partitioning based on the status.
Lets say I need to create two tables which both refer to each other (they need constraints). Is the only way to create the tables without the constraints, and then add them in a separate statement? I remember that some orms can solve this themselves, but is it possible to do this only using sql and in two statements?
You should must create the tables first without foreign keys and append them after both tables are created:
create table t1 (id int not null primary key, id2 int not null);
create table t2 (id int not null primary key, id1 int not null);
alter table t1 add foreign key (id2) references t2(id);
alter table t2 add foreign key (id1) references t1(id);
Good news: A schema dump works perfect (so is my database):
mysqldump -u root so t1 t2 | mysql -u root so
This gives no errors because mysqldump inserts DISABLE KEYS and ENABLE KEYS at the right places.
I executing this statement:
CREATE TABLE test_mod SELECT * FROM `test`
This copied a table into the newly created table. Then I executed this:
INSERT INTO test_mod(`test_name`) SELECT `test_name` FROM test
That statement should have inserted more data to the newly created table. It did so, but the primary key are all 0?!?!
The I executed this statement:
ALTER TABLE test_mod MODIFY test_id INT UNSIGNED NOT NULL AUTO_INCREMENT
And phpmyadmin told me that the column test_id has already an auto_increment attribute.. so how can I populate more data while having an auto increment functionality?
Try
CREATE TABLE test_mod LIKE test;
INSERT INTO test_mod SELECT * FROM test;
This will copy the table definition from "test" and insert all data from it into "test_mod".
I have a table with a column of type INT(7) and I want to make this a foreign key constraint on the primary key of another table. However, the primary key is type INT(11) UNSIGNED, so I need to change INT(7) to match that in order for the foreign key to be created.
Although I don't expect to have any problems converting an INT(7) to INT(11) UNSIGNED (I have checked the column to be changed and it has no unsigned values), is there any way to ask MySQL which rows it would alter the value of? I will take a backup anyway, but I would like to be able to find out if there are likely to be any problems beforehand as I can potentially fix them before running the ALTER TABLE statement.
run
CREATE TABLE tmp SELECT yourcolumnname AS x, yourcolumnname AS y FROM yourtable;
ALTER TABLE tmp MODIFY COLUMN x INT(11) UNSIGNED;
SELECT * FROM tmp WHERE x!=y;
DROP TABLE tmp;
Yes, ALTER TABLE doesn't commit if the TEMPORARY keyword is used.
ALTER TEMPORARY TABLE .... [your code]
Source
EDIT: I just noticed this:
"However, although no implicit commit occurs, neither can the
statement be rolled back. Therefore, use of such statements will
violate transaction atomicity: For example, if you use CREATE
TEMPORARY TABLE and then roll back the transaction, the table remains
in existence."
So I'm not sure about ALTER.