I have a table in database naming "Customers". The table have total 83 columns. I want to create another "NewCustomers" table like "Customers" using php and mysql query. If the "NewCustomers" table exists in the database then it is no need to create the table if it is not then it will create the "NewCustomers" table.
I know there is a query
CREATE TABLE 'NewCustomers' LIKE 'Customers';
But it only creates the table like Customers if the New Customers not exists. How can I use
IF NOT EXISTS
in this regard?? I don't want to write all 83 columns name again like
CREATE TABLE IF NOT EXISTS `NewCustomers` (
`id` int(11) NOT NULL auto_increment,
'name` varchar(250) NOT NULL,
`data` varchar(100) NOT NULL default '',
.
.
.
PRIMARY KEY (`id`)
);
simple do like this
CREATE TABLE IF NOT EXISTS `NewCustomers` like `Customers`
Note :
And also it's safe to refer the DB name like below
CREATE TABLE IF NOT EXISTS `NewCustomers` like `DB_name`.`Customers`
Related
I try to insert Data in the temporary Table, but when i call select last_insert_id() it return allways the same number. I have set the autoincement setting.
In the normal Table works, but when i Copy the normal Table to a temporary Table, then i have the problem with the select last_insert_id().
This is my code:
CREATE TEMPORARY TABLE IF NOT EXISTS suchergebnisse_temp (SELECT * from suchergebnisse);
INSERT INTO suchergebnisse_temp SET datensatzid='2865', datum='2015-05-13 00:00:00', tabelle='task', sortierung1='1';
SELECT LAST_INSERT_ID();
The Normal Table:
CREATE TABLE IF NOT EXISTS `suchergebnisse` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tabelle` varchar(100) NOT NULL DEFAULT '0',
`datensatzid` bigint(20) NOT NULL DEFAULT '0',
`datum` datetime NOT NULL,
`sortierung1` int(11) NOT NULL COMMENT 'Priorität',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Temporäre Tabelle.';
Can you help me? Thanks :)
I forgot to add the key explicit to the temporary Table. It needs to execute the following code:
ALTER TABLE `suchergebnisse_temp`
CHANGE COLUMN `id` `id` BIGINT(20) NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`id`);
Because with:
CREATE TEMPORARY TABLE IF NOT EXISTS suchergebnisse_temp (SELECT * from suchergebnisse);
copys only the Field Data but not the Key Data. The Key Data must be declared separately.
I want to create table if not exists,else update it.
this code is for create table:
CREATE TABLE Book
ID INT(10) PRIMARY KEY AUTOINCREMENT,
Name VARCHAR(60) UNIQUE,
TypeID INT(10),
Level INT(10),
Seen INT(10)
how can I change it to support update too?
//EDIT
I mean if I add a column,only add a column...not remove last data
If I remove a columns (for example remove TypeID INT(10) from the command) just that columns be remove...not all data
You can use INFORMATION_SCHEMA.TABLES to check the existence of tables
IF EXISTS(SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'Databasename'
AND table_name = 'tablename')
THEN
....
ALTER TABLE Tablename...
....
ELSE
....
CREATE TABLE tablename...
....
END IF;
I am using
$row = mysqli_fetch_row(mysqli_query($conx, "SHOW CREATE TABLE $table"));
in a loop to grab my schema data, that works fine.
I also need put the column names in an array.
Is there a way to pull them from that $row array?
Or do I need to run a separate SHOW COLUMNS to do that?
If you take the create table result whic will look like this:
CREATE TABLE `TableName` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`message` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
You could create a regex that looks for "`[a-zA-Z0-9_-]*`" and besides the first match, they will be column names.
Why not use the information_schema?
"SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = '$table'"
Hey how would I be able to duplicate my only auto increment key to another key, basically I want my (' id ') to display the same information on my (' user_id '), here is the code:
CREATE TABLE IF NOT EXISTS `".$db_table_prefix."users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`user_name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`id`)
How would I be able to input the same information from my id to my user_id?
Not sure what you mean but if you want to have the same value repeted two times in the same record It's pointless and redundant.
You can use the SQL aliases to achive what you want:
SELECT id as user_id FROM ...
If you really need to sync up the two field of your table you can do:
UPDATE table SET user_id = id WHERE user_id != id
Not sure why you would want to do this, but if you want to duplicate the information after an INSERT you would need to fetch the new ID and then perform an UPDATE
// get the newly inserted ID
$new_id = $db->insert_id;
// perform the update on the table
$db->query("UPDATE users SET user_id=".$db->escape($new_id)." WHERE id=".$db->escape($new_id));
Also, in your table definition the fields don't match: int(11) vs. int(10).
I have set up four additional tables for my plugin to use what I am trying to do is take a name and assign it a ID then use this data to populate drop down menus with a name and the same for class and position I am unsure as to how to do this correctly this is what i have so far.
$sql = "CREATE TABLE $tableName (
recordID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(recordID),
driverID int,
driverName varchar(30),
classID int,
driverClass varchar(20),
posID,
driverPosition varchar(6),
trackName varchar(30),
raceDate date
);";
$sql = "CREATE TABLE $driverTableName (
driverID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(driverID),
driverName varchar(30)
);";
$sql = "CREATE TABLE $classTableName (
classID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(classID),
className varchar (20)
);";
$sql = "CREATE TABLE $posTableName (
posID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(posID),
posName varchar(6)
);";
The bottom three tables will store the data I want to populate the drop down boxes to create a record with I am unsure as to how to link them to the top table where this record will be stored.
This is pretty much a indexing issue. If you are going to access the database separately to the standard calls that Wordpress provides, you should at the very least use http://codex.wordpress.org/Class_Reference/wpdb as it will save you some coding time.
The rest of it is a MySQL question. (Assuming you are using MySQL) In how to properly index the data together and then parsing the data as it comes in.