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;
Related
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`
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 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'"
I have a table that contains millions of sales records and looks like this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NULL);
The first three columns are populated with data. I would like to insert data into the company_id column that will identify each company with an auto_incremented integer. I plan to use the company_id field as a foreign key referencing another table that will contain each company's details. Many companies have multiple transactions, so the code needs to assign the same company_id to each row in the sales table with a matching company_name.
Is there a way to do this using only MySQL?
First, I'd recommend creating the company table:
CREATE TABLE company (
company_id INT NOT NULL AUTO_INCREMENT,
company_name VARCHAR(45),
PRIMARY KEY(company_id));
Then insert the companies from your sales data:
INSERT INTO company (company_name)
SELECT distinct company_name
FROM sales;
Finally, update your sales table with a join to get the company_id:
UPDATE sales s
JOIN company c ON s.company_name = c.company_name
SET s.company_id = c.company_id;
SQL Fiddle Demo
You should also remove the company_name field from the sales table since this is now stored in the company table.
To define an auto incremented integer, you just use the AUTO_INCREMENT keyword. However, if you define any columns as auto_increment, you must also make that column your primary key. Which, in this case, would make sense in order for it to be a foreign key elsewhere.
Try this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(company_id);
SQL Fiddle
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).