I'm digging on MySQL's online ddl (5.7)
For some operations, it support ALGORITHM such as INPLACE, COPY.
This is I understood
INPLACE modifies original table
COPY creates temporary table -> copies all records to it -> and replace original table
Plus, this link refers that adding index to a table does not rebuild table
https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-operations.html
but, after I add index and check tables CREATE_TIME it is updated
CREATE DATABASE db;
USE db;
CREATE TABLE hello(id int primary key auto_increment, col varchar(20));
SELECT CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='db' and TABLE_NAME='hello';
ALTER TABLE hello ADD INDEX `idx_col` (col), ALGORITHM=INPLACE;
# CREATE TIME is updated
SELECT CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='db' and TABLE_NAME='hello';
I'm not sure whether CREATE_TIME doesn't actually mean table creation time, or
whether table is rebuilt even with ALGORITHM=INPLACE specified.
Could anyone help me find out what is going on here?
Related
AS described in the MySQL documentation here, it should be possible to drop a column instantly with a syntax like this one:
ALTER TABLE tbl_name DROP COLUMN column_name, ALGORITHM=INSTANT;
It is documented that it is only possible with the following constraints:
Dropping a column cannot be combined in the same statement with other ALTER TABLE actions that do not support ALGORITHM=INSTANT.
Columns cannot be dropped from tables that use ROW_FORMAT=COMPRESSED, tables with a FULLTEXT index, tables that reside in the data dictionary tablespace, or temporary tables. Temporary tables only support ALGORITHM=COPY.
Unfortunately, I am unable to use the syntax described above. For example, here is my test code:
CREATE TABLE MyTable (
MyPrimaryKey bigint NOT NULL AUTO_INCREMENT,
UserId char(36) NOT NULL,
Username varchar(254) NOT NULL,
PRIMARY KEY (MyPrimaryKey),
UNIQUE KEY IX_UserId (UserId)
) ENGINE=InnoDB;
ALTER TABLE MyTable DROP COLUMN Username, ALGORITHM=INSTANT;
When I run this against MySQL 8.0.28, I get the following error:
Error Code: 1845. ALGORITHM=INSTANT is not supported for this operation. Try ALGORITHM=COPY/INPLACE.
Am I doing something wrong or the documentation is missing something?
Note that the ROW_FORMAT of my table is Dynamic. Here is how I got the row format:
SELECT table_name, row_format
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name = 'MyTable';
This functionality has been added to mysql 8.0.29. See the release notes for more details.
When I alter MySQL table, add new column for my log table. specific statement like below:
alter table log_xxx add column `new_column` smallint NOT NULL after post_date;
Will above statement will lock table log_xxx if I execute SQL to insert some records when I execute this alter statement?
I make some test, execute insert statement do not include new column successfully while the alter statement still under processing. So I have no idea about the alter table add column will lock table or not?
Is there anyone who knows about this problem?
It depends on the storage engine, if you use InnoDB, it may alter table in place and not block the DML operation, and if then MyISAM is used, the update and write operations that start after alter begins, will be blocked until the new table is ready.
Those links maybe help:
https://dev.mysql.com/doc/refman/5.7/en/alter-table.html
https://dev.mysql.com/doc/refman/5.7/en/innodb-create-index-overview.html
I am using an artificial primary key for a table. The table had two columns, one is the primary key and the other one is a Dates (datatype: Date) column. When I tried to load bulk data from a file (which contained values for the second column only), the YYYY part of the dates were added to the primary key column (which was the first column in the table) and the rest of the date was truncated.
So I needed to reset the table. I tried it using the Truncate table statement, but it failed with an error because this table was referenced in the foreign key constraint of another table. So I had to do it using the delete * from table; statement. I did delete all the records, but then when I inserted the records again (using the insert into statement this time), it started incrementing the ID starting from the year after the last year in the year I had previously inserted (i.e. it did not refresh it).
NOTE:- I am using MySQL 5.5 and InnoDB engine.
MY EFFORT SO FAR:-
I tried ALTER TABLE table1 AUTO_INCREMENT=0; (Reference Second Answer) ---> IT DID NOT HELP.
I tried ALTER TABLE table1 DROP column; (Reference- answer 1) ---> Error on rename of table1
Deleted the table again and tried to do:
DBCC CHECKIDENT('table1', RESEED, 0);
(Reference) ---> Syntax error at "DDBC" - Unexpected INDENT_QUOTED
(This statement is right after the delete table statement, if that
matters)
In this article, under the section named "Auto Increment Columns for INNODB Tables" and the heading "Update 17 Feb 2009:", it says that in InnoDB truncate does reset the AUTO_INCREMENT index in versions higher than MySQL 4.1... So I want some way to truncate my table, or do something else to reset the AUTO_INCREMENT index.
QUESTION:-
Is there a way to somehow reset the auto_increment when I delete the data in my table?
I need a way to fix the aforementioned DDBC CHECKINDENT error, or somehow truncate the table which has been referenced in a foreign key constraint of another table.
Follow below steps:
Step1: Truncate table after disabling foreign key constraint and then again enable-
set foreign_key_checks=0;
truncate table mytable;
set foreign_key_checks=1;
Step2: Now at the time of bulk uploading select columns in table only those are in your csv file means un-check rest one (auto id also) and make sure that colums in csv should be in same order as in your table. Also autoid columns should not in your csv file.
You can use below command to upload data.
LOAD DATA LOCAL INFILE '/root/myfile.csv' INTO TABLE mytable fields terminated by ',' enclosed by '"' lines terminated by '\n' (field2,field3,field5);
Note: If you are working in windows environment then change accordinglyl.
You can only reset the auto increment value to 1 (not 0). Therefore, unless I am mistaken you are looking for
alter table a auto_increment = 1;
You can query the next used auto increment value using
select auto_increment from information_schema.tables where
table_name='a' and table_schema=schema();
(Do not forget to replace 'a' with the actual name of your table).
You can play around with a test database (it is likely that your MySQL installation already has a database called test, otherwise create it using create database test;)
use test;
create table a (id int primary key auto_increment, x int); -- auto_increment = 1
insert into a (x) values (1), (42), (43), (12); -- auto_increment = 5
delete from a where id > 1; -- auto_increment = 5
alter table a auto_increment = 2; -- auto_increment = 2
delete from a;
alter table a auto_increment = 1; -- auto_increment = 1
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.
I'm new to MySQL and want to know that if I have a table with 25 column and the first one of it is the "id". Would the computer render every time through the whole table to search the particular "id".
if you construct the query like SELECT * FROM $table_name WHERE table_id=$id; then it will not render all table.
And as #dku.rajkumar says in the comment, it depends on what you want to fetch and your query structure.
It may depend on the query and also the STORAGE Engine you choose to use.
like MyIsam or InnoDb
example
CREATE TABLE tablename (
id INT UNSIGNED PRIMARY KEY
)ENGINE=MyIsam;
CREATE TABLE tablename (
id INT UNSIGNED PRIMARY KEY
)ENGINE=InnoDB;
there do exist difference in way tables are stored ,dependiing on storage engine , which certainly will reflect in the criteria mysql server (mysqld) performs search to cater your needs .