How to create table structure without copying data from database table? - mysql

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.

Related

Is there any way to import database table schema only

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.

Microsoft MS SQL System Tables

How I see into MsSql sys.sysprocesses
can I modify it?
Example :
select count(spid) from master.dbo.sysprocesses
The result will be what I want
As Dan has stated, sys schema objects cannot be modified, you will get an error if you try to do so.
The sys.sysproccesses view can be queried using the statement you have posted from a query window and will return results
select count(spid) from master.dbo.sysprocesses
If you are really wanting to "modify" which I assume means you want to update values, drop rows and such, you can add to a temp table and modify that but the data is a copy so anything you do will not affect the underlying view
SELECT *
INTO #MyTable
FROM sys.sysprocesses
sysprocesses is a view and a legacy one at that. One cannot modify system objects.
You can create your own view to encapsulate DMV queries:
CREATE VIEW dbo.YourView
SELECT COUNT(session_id) AS SessionCount
FROM sys.dm_exec_sessions;
GO

How to clone SQL table with index

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

Is there a way to copy the structure of a table (fields but no data) to a new table in MySQL?

For example, I have a table named movies. It has the fields/columns title VARCHAR(100) and runtime INT(5). It's loaded with 10,000 rows of data.
I want to create another table, let's call it movies_custom, that has all of the same columns, but with none of the data.
Is there a quick SQL statement to do this?
EDIT: Sorry, I noticed the SQL Server tag on this and assumed that was the technology you were using until I saw MySQL in the question title.
You can use the syntax CREATE movies_custom LIKE movies in MySQL
Sure!
In SQL Server you can do this query:
select top 0 * into movies_custom from movies
That creates the table structure without inserting any rows.
Very easy in MySQL:
CREATE newtable LIKE oldtable;
The other answers led me in the right direction, but I had to add the keyword TABLE in order to make it work with MySQL Workbench 6.
CREATE TABLE movies_custom LIKE movies;

mysql inserting data from one schema to another

Is there a way to insert data from a table in schema1 to a table in schema2 in mysql.
Also , I assume there will be any access/privilege issues.
My environment is Joomla using Fabrik extension, PHP, MySQL
Kindly share some tips
Thanks in advance
This query does that:
INSERT INTO db2.table1 SELECT * FROM db1.table1;
Not tested but should do the job.
If you do this as root user, you will have no permission issues.
Backup your data first, though.
You can always preface the table name with the database name and as long as the user has the appropriate permission you can do:
insert into db1.users( first, middle, last )
select a.first, a.middle, a.last from db2.users a
See the following for the documentation insert .. select