473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I CREATE TABLE form with indexes ?

290 100+
Hi,

I am using :

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE T_update SELECT * FROM temp  
to copy all data to the new T_update table. But I noticed that
my the indexes were not copied.

Apparently I can add them into the above code, but I am
not sure about the syntax

Is it like this ?


Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE T_update  
  2. KEY `id` (`id`),
  3. KEY `start_date` (`start_date`),
  4. PRIMARY KEY (cb_id) 
  5. SELECT * FROM temp 
Also what about the AUTO_INCREMENT column - cb_id ?

If it is auto_increment in the "temp" table will it be created that way in the
"T_update " table - and filled up with incrementing numbers ?

Thanks for any help on this one :)



.
Feb 11 '10 #1
4 3921
Atli
5,058 Expert 4TB
Hey.

You can try the CREATE TABLE ... LIKE syntax. It should preserve indexes. Only downside I can see is that it doesn't preserve foreign key restraints.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `tbl_new` LIKE `tbl`;
  2. INSERT INTO `tbl_new` SELECT * FROM `tbl`;
(Note that these are two separate statements.)
Feb 11 '10 #2
jeddiki
290 100+
Thanks for replying.

As the tables have 20,000 rows, I think this will take a lot longer
than doing the job with the CREATE FROM query - but only if I can get the indexes done as well.

One idea I had was to create the two standard indexes as part of the CREATE and then add a new column which would be the auto-increment primary key for the table. BUT using the extra ALTER TABLE query would only be necessary if it can not all be done with one CREATE FROM query.

If anyone knows how to write this in the correct syntax I would appreciate your input.


I currently have this:

Expand|Select|Wrap|Line Numbers
  1. $sql = "CREATE TABLE tbl_new  
  2. KEY `id` (`id`),
  3. KEY `start_date` (`start_date`),
  4. PRIMARY KEY (cb_id) 
  5. SELECT * FROM tbl"; 
  6.  

Thanks


.
Feb 12 '10 #3
Atli
5,058 Expert 4TB
Ok, although 20,000 rows aren't really that much. Should only take a second to clone such a table using the syntax I suggested. (Unless it has an extraordinary amount of columns, of course.)

I tested it on my local server. Took a table with a PK, two text columns and a date column only, and with 21.005 rows, about 0.5 - 0.8 seconds to execute each statement. (Where both text columns were indexed.)

But anyhow...
@jeddiki
It's all explained in the manual.

In this case, it would go something like this:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `table_name`
  2.    (create table definitions...)
  3.    [table options]
  4.    [select statement]
And note that if the select statement returns a column that is not defined in the create table definitions, it will simply be added. Which means you can define any special columns (auto_increment, primary key, indexes, etc...) in the create table definitions, but leave out those that need no special attention and have them created automatically.

Here you will also have to define the table options, such as the storage engine, the charset and the auto_increment position. (The LIKE clause in my previous suggestion takes care of that automatically, but this syntax does not.)

Your query is missing the parenthesis around the create table definitions (the KEY statements). Otherwise, besides the table options, it should work.
Feb 12 '10 #4
jeddiki
290 100+
Well if it will only take a second or two then that is fine.

I will go with your two line suggestion:


1. CREATE TABLE `tbl_new` LIKE `tbl`;
2. INSERT INTO `tbl_new` SELECT * FROM `tbl`;
Thanks again.
Feb 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Tuhin Kumar | last post by:
Hi, Oracle give the error ORA-01418 when I try to do the following; Create unique index t1_pk on TABLE1(EntryId DESC) ; If the I try to add primary key Contraint using the above index t1_pk...
0
by: Phil Powell | last post by:
Retracing my problem leads me to believe I never successfully created fulltext indexes for MySQL 3.23.58 MyISAM tables. I went to the MySQL manual and was able - or so I thought - to create them,...
4
by: Melissa | last post by:
I have a frontend file named CustomerApp and backend file named CustomerData. CustomerApp is at C:\Customer Database and CustomerData is at S:\Customer Database. Could someone help me with the code...
8
by: Brian S. Smith | last post by:
Hi gang, Please help. I've been through the Access help, searched the Web, and I can't seem to get a straight answer. As the Subject line suggests, I want to run a fairly simple VB/Access Sub...
3
by: blindsey | last post by:
Is there a tool that can take an Access database and generate SQL "CREATE TABLE" statements for all the tables in it?
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
2
by: bobby_b_ | last post by:
I have a table where fields 1 and 2 make up the primary key. Because of this, I have a unique composite index on fields 1 and 2 (as required by DB2). Now my question is: Fields 1 and 2 are also...
5
by: shelleybobelly | last post by:
Hi, I have a new job. It needs to drop and re-create (by insert) a table every night. The table contains approximately 3,000,000 (and growing) records. The insert is fine, runs in 2 minutes. The...
2
by: Tom_F | last post by:
To comp.databases.ms-access: In the VBA code behind a form, I would like to create a string variable, composed of the names of the field(s) in the primary key of the table which is the...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.