I have a problem with a mysql table which I'd like to clone and give another name. When I try the
CREATE TABLE data1 AS SELECT * FROM data;
It gets stuck in execution, and never actually do anything, and I have to abort.
The thing is that my table "data" has some extra commands in the sql source like
CREATE INDEX keywords ON data (keywords);
It has a few of these, and I suspect that this is what's causing the issue, since I have been able to clone other tables without these extra commmands. I'm new in sql, so I have no idea how to overcome this problem. Anyone care to help?
Use is create table like syntax. to create a table with the same structure and after this you can copy the data.
https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html
a second solution is to use mysqldump to extract the table structur including the data, rename the table in dump file and Reimport the it.
-- To copy table with constraints
CREATE TABLE new_table_name LIKE old_table_name;
-- To copy without constraints
CREATE TABLE new_table_name
(SELECT * FROM old_table_name WHERE 0);
source
Related
So I have this problem where I need to create an exact copy of another table (schema, indexes) but not the data. We need to temporarily store data before we move it to the table it mirrors.
I can create a mirror table like CREATE TABLE foo_mirror LIKE foo; But I want to automatically update the schema when the schema of the table foo changes'.
I have considered temporary tables as well instead of mirror tables but we will have 2 different processes copying data to the mirror table foo_mirror and copying data to the actual table foo. So I can't use a single session.
So I can compare the schema using the following queries:
-- for_mirror schema
SELECT DISTINCT *
FROM `information_schema`.`columns`
WHERE `TABLE_NAME` LIKE 'foo_mirror'
-- foo schema
SELECT DISTINCT *
FROM `information_schema`.`columns`
WHERE `TABLE_NAME` LIKE 'foo'
I could write some code that will apply updates in a job, or perhaps a trigger. I'm wondering if there is a better way to keep tables in sync or even a tool where I don't need to manage this myself.
I also see a potential problem e.g. Changing the type for a column may fail if data hasn't been updated correctly in the mirror table (string to integer - "one" -> 1).
I'm trying to "duplicate" a table from my db creating a temporary one. However I need indexes are not copied.
I'm using this query right now:
CREATE TEMPORARY TABLE temp365 LIKE contactlens;
but the result table contains also indexes. I checked out the documentation and I don't see a way to copy the structure without indexes.
Because I can't have static name for indexes, I was wondering how I can drop all of them using simple SQL.
I was starting trying to avoid to copy them at all, but it seems not possible.
It's just to provide some examples to #wchiquito's comment.
It's pretty easy to create a copy of a table without indexes using CREATE TABLE ... SELECT command.
If you don't need to copy any rows from original table just provide a false value in WHERE clause or specify 0 in LIMIT one. Some examples:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] temp365 SELECT * FROM contactlens WHERE 0;
or a bit different way:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] temp365 SELECT * FROM contactlens LIMIT 0;
This way you'll get the same result as with CREATE TABLE ... LIKE, but without indexes.
I have two databases. Now I'm trying to import all table schema only from first database to second database. I already exported all table structure from first database using phpmyadmin. But I can't import the table structures in the second database. phpmyadmin says
#1050 - Table 'XXXXXX' already exists
How can I import only the table structure correctly?
Note: Both databases had same table and all table had same structure. I have changed some table structures in the first database that I can't remember right now. Now I need to merge both table structure only. Also both database contains different data set and I can't afford any data loss from both databases.
Before executing any command I would recommend taking full database backup, otherwise you may lost a few days of sleep.
Using ALTER command
Use ALTER command to modify table structure. Here's sql that adds age not nullable age field to users table.
ALTER TABLE users ADD age int(11) not null
Re-creating table
I wouldn't recommend this method because you'll have data loss. Drop old table then create with new schema.
DROP TABLE mytable;
CREATE TABLE mytable (
...
);
Or if you want to keep data you can:
Duplicate or rename table to different name.
Create a new table with new schema.
Copy data from old table: INSERT INTO newtable SELECT * FROM oldtable
Renaming tables might cause relationship issues. I would recommend using ALTER command as much as possible. You can also take a look at scheme migration(aka: code first migration, database migration).
The main Issue is merging the tables. To identify the differences between the two tables I use a small software called SQL Power Architect.
I was asked in a viva "how can you create a table structure without copying data from a database table?" I was quite sure with my answer. My answer was:`
CREATE TABLE new_table AS (SELECT *FROM old_table);
Was I right or wrong?
CREATE TABLE new_table AS (SELECT * FROM old_table where 0=1);
No. Your answer is incorrect. You can use this SQL query.
CREATE TABLE *new_table* AS (SELECT *FROM *old_table* WHERE *statement=false*);
Like this an example is following:
CREATE TABLE *new_table* AS (SELECT *FROM *old_table* WHERE *1=2*);
I think it will serve your purpose...:P
CREATE TABLE new_table AS SELECT * FROM old_table where 0=1;
Here in where clause we can use any unequality statement like where 1=2,2=3..etc., which should inform optimizer this 'where' condition will definitely return false, thus preventing any data to be copied from old_table.
if you are using workbench it has a option in left side of the screen "Data export"
just click on it select the db you want to copy structure of and there will be a drop down option select "Dump structure only" and export to a folder.
import this file where ever you want using option data import restore and select path and type new schema name and import. you have new schema with the structure you want.
so the code I am working on has this statement executed by PHP (note:This is taken from the PostgreSQL log file so it doesn't include any PHP stuff):
CREATE temporary table IF NOT EXIST temp tablename(id int primary key,
shared int default 0) replace select 1, userid as id
from tablefoo where sharedid = 1337
I don't quite understand what's going on here exactly, I know what a temporary table is, and I can quite accurately guestimate what IF NOT EXIST does, but what is replace doing here? I know replace is like insert but it replaces stuff as well, but in this case, nothing is specified for it to replace with, so does it just replace something with nothing and why the Select 1, I know that pretty much just tells you if your table has rows or something, but what is the point of using it here?
After some research, I found that IF NOT EXIST and replace do not exist in PostgreSQL. Most online sources suggest that SQL functions be used to replace them.
Should I use an SQL function to emulate IF NOT EXIST? If so, what would I write (sorry, I am pretty new to SQL) or should I just use a PHP function.
What about replace?
Sorry for the trouble, thanks for your time, oh and if you guys aren't busy or anything, you could also tell me about how to emulate "ignore", my current solution involves arbitrarily removing it.
Many uses in MySQL for temporary tables can be replaced in PostgreSQL with common table expressions or ordinary subselects.
WITH someCTE AS (
SELECT
...
) SELECT/UPDATE/DELETE ... WHERE sometable.column = someCTE.another_column;
Look into CREATE TABLE documentation. Temporary tables are just as name suggests not permanent:
If specified, the table is created as a temporary table. Temporary
tables are automatically dropped at the end of a session, or
optionally at the end of the current transaction (see ON COMMIT
below). Existing permanent tables with the same name are not visible
to the current session while the temporary table exists, unless they
are referenced with schema-qualified names. Any indexes created on a
temporary table are automatically temporary as well.
In particular temp tables are stored in diffrent (non-public) schema, e.g.:
=> Create Temporary Table someTempTable (value integer);
CREATE TABLE
=> \dt someTempTable
List of relations
Schema | Name | Type | Owner
-----------+---------------+-------+----------
pg_temp_2 | sometemptable | table | postgres
(1 row)
PostgreSQL doesn't have IF NOT EXISTS like in MySQL's CREATE TABLE, so you can't use it. If you want to create some table you need to firstly drop existing one (if it exists). Fortunately you could use SQL command DROP TABLE IF EXISTS to handle this:
=> Drop Table If Exists someTempTable;
DROP TABLE
=> Drop Table If Exists someTempTable;
NOTICE: table "sometemptable" does not exist, skipping
DROP TABLE