Jump to content

ALTER TABLE NAME


XJTRy

Recommended Posts

Well I am successfully creating a table with the script below.

 

$query = "CREATE TABLE user

 

(

Id int(6) NOT NULL auto_increment,

Date varchar(10) NOT NULL,

AC_Make varchar(15) NOT NULL,

AC_ID varchar(10) NOT NULL,

Legs varchar(15) NOT NULL,

Route varchar(30) NOT NULL,

Duration varchar(10) NOT NULL,

Day_Land varchar(5) NOT NULL,

Night_Land varchar(5) NOT NULL,

Instrument varchar(5) NOT NULL,

Approaches varchar(5) NOT NULL,

Night varchar(5) NOT NULL,

XC varchar(5) NOT NULL,

PIC varchar(5) NOT NULL,

Dual varchar(5) NOT NULL,

Remarks varchar(30) NOT NULL,

PRIMARY KEY (id)

)";

 

$result = mysql_query($query);

 

 

However, now I want to change the table name as well. I'm changing the name because I am working step by step to eventually creating custom tables. With the script above the same script is created time after time. Here's the script I tried that returns an error:

 

$query = "CREATE TABLE user

 

(

Id int(6) NOT NULL auto_increment,

Date varchar(10) NOT NULL,

AC_Make varchar(15) NOT NULL,

AC_ID varchar(10) NOT NULL,

Legs varchar(15) NOT NULL,

Route varchar(30) NOT NULL,

Duration varchar(10) NOT NULL,

Day_Land varchar(5) NOT NULL,

Night_Land varchar(5) NOT NULL,

Instrument varchar(5) NOT NULL,

Approaches varchar(5) NOT NULL,

Night varchar(5) NOT NULL,

XC varchar(5) NOT NULL,

PIC varchar(5) NOT NULL,

Dual varchar(5) NOT NULL,

Remarks varchar(30) NOT NULL,

PRIMARY KEY (id)

)";

 

$result = mysql_query($query);

 

$query = 'ALTER TABLE user change user1'  <-----------------------------------------Heres the added query

$result = mysql_query($query);

 

include 'closedb.php';

Link to comment
Share on other sites

I just tried the following with no success: It returns an error stating Parse error: parse error, unexpected T_STRING.

 

$query = "CREATE TABLE user

 

(

Id int(6) NOT NULL auto_increment,

Date varchar(10) NOT NULL,

AC_Make varchar(15) NOT NULL,

AC_ID varchar(10) NOT NULL,

Legs varchar(15) NOT NULL,

Route varchar(30) NOT NULL,

Duration varchar(10) NOT NULL,

Day_Land varchar(5) NOT NULL,

Night_Land varchar(5) NOT NULL,

Instrument varchar(5) NOT NULL,

Approaches varchar(5) NOT NULL,

Night varchar(5) NOT NULL,

XC varchar(5) NOT NULL,

PIC varchar(5) NOT NULL,

Dual varchar(5) NOT NULL,

Remarks varchar(30) NOT NULL,

PRIMARY KEY (id)

)";

 

$result = mysql_query($query);

 

$query = 'ALTER TABLE user RENAME user1'

$result = mysql_query($query);

 

include 'closedb.php';

Link to comment
Share on other sites

I figured it out at dev.mysql.com.

 

I was using the wrong syntax. ALTER TABLE is valid for many functions but not renaming the table. Instead there is a RENAME TABLE [] TO [] syntax as well.

 

Here's the correct syntax abbreviated FYI..

 

 

$query='CREATE TABLE testing

(

Date varchar(10) NOT NULL

)';

$result= mysql_query($query);

 

$query='RENAME TABLE testing TO testingone';

$result= mysql_query($query);

 

include 'closedb.php';

 

Link to comment
Share on other sites

Now I am having trouble with renaming the table via variable.

 

The following yeilds a table called '$name', but I wanted it to call the $name variable which should name is 'ryan'. Any ideas?

 

$query='CREATE TABLE testing

(

Date varchar(10) NOT NULL

)';

$result= mysql_query($query);

 

$name= 'ryan';

 

$query='RENAME TABLE testing TO $name';

$result= mysql_query($query);

 

include 'closedb.php';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.