I'm trying to:
- create an empty copy of a table
- insert all the data from the original to the copy
query I'm using is:
CREATE TABLE my_schema.account_copy_like LIKE my_schema.account; -- To inherit all table definitions
INSERT INTO my_schema.account_copy_like SELECT * FROM my_schema.account; -- Copying data from another table
The issue here is that the INSERT INTO... SLEECT * FROM
is not copying as expected, there are some discrepancies on the table information as you can compare on the table size and rows, I was wondering why. It seems that in the copy table it has more rows than the original but it's size is lower?
1 Answer 1
- The copy has not finished yet, so the numbers are not yet updated.
- The source of some things, in particular, "Table rows", is an estimate, so it may never match exactly. (Do not let this concern you.)
- Do
SELECT COUNT(*) FROM account_copy_like;
to give yourself more confidence that all the data is there. OrSELECT SUM(nnn) ...
wherennn
is some numeric column.
answered Oct 17, 2022 at 20:51
-
Thanks, last bullet seemed to gave me the result I was seeking. Apparently the
COUNT
were the same for both original and copy tables, I was confused by the Mysql Workbench table inspector UI values, which now I suppose that is not very thrust worthy.Retrosec6– Retrosec62022年10月17日 23:29:30 +00:00Commented Oct 17, 2022 at 23:29 -
Any UI is going to simply run some queries against the database.
SHOW TABLE STATUS
is notoriously inaccurate; your experiment hit multiple problems with that statement.Rick James– Rick James2022年10月18日 03:58:33 +00:00Commented Oct 18, 2022 at 3:58 -
Basically a
SHOW TABLE STATUS
will give you the result of the statistics. It is highly not recommended to use those values for any integrity check as those values will be inaccurate and depend on how the statistics are refreshed.Hybris95– Hybris952022年10月18日 14:26:07 +00:00Commented Oct 18, 2022 at 14:26
lang-sql
CREATE TABLE my_schema.account_copy_like SELECT * FROM my_schema.account;
?CREATE TABLE my_schema.account_copy_like SELECT * FROM my_schema.account;
just inherits basic columns definitions, but will not inherit indexes and auto_increments, if you would like clone all definitions you should use theLIKE
approach