This question already has an answer here:
Add a new column in all mysql tables
(1 answer)
Closed 9 years ago.
I want to add a common column in all tables in a database
For example:
I want to insert the column TENANT_ID in all tables in database
How can i do that.
You use the ALTER TABLE SQL command as in:
ALTER TABLE table_name
ADD column_name datatype
If you have multiple tables you'll need to apply this to each table in succession.
More here: http://www.w3schools.com/sql/sql_alter.asp
yes you can add it
if you have list of table in array
$column_name = 'TENANT_ID';
$size = '';
foreach($all_table_name as $each_table_name) {
mysql_query("ALTER TABLE {$each_table_name} ADD {$column_name} VARBINARY({$size})");
}
above code i just skilton you have to set accourding to your code.
Related
This question already has answers here:
Drop All Tables Except One in MySql
(6 answers)
Closed 6 years ago.
I was wonder if there is any way to delete all the table of my bdd exept the one named "keep" in php. Something like this:
$sql = 'DROP TABLE * EXEPT 'keep' ';
Also is there any command in php to create a new bdd (not a table) or do I have to do it in phpmyadmin ?
Thanks in advance !
You can create a "drop table" sql file by accessing the information_schema.tables table and selecting from it for the appropriate schema.
select concat('drop table ','table_name',';')
from information_schema.tables
where table_schema = '[my_schema_name]'
and table_name != '[the_table_I_don't_want_to_drop]'
INTO OUTFILE '[FILESPEC]';
If you would like to list a group of tables not to drop, simply change the and clause to "and table_name not in ('table1','table2',...'tableN')".
You could do the same thing in PHP. You would simply loop through the results and execute the resultant string.
This question already has answers here:
MySQL select where column is not empty
(13 answers)
Closed 8 years ago.
I have a table that contains thousands of rows. I'm trying to show only rows that have information in a specific column. The column name is "vhf" the table name is "p_loc". The majority of the rows contain no info in the column "vhf" but, when I try the code below it wants to echo all my rows.
I just need the rows that contain any information in the column "vhf"
$sql = mysql_query("SELECT vhf FROM p_loc WHERE vhf IS NOT NULL");
Sounds like there are two cases you want to filter out:
the vhf column is NULL
the vhf column is empty, or an empty string
This means you need two clauses in your SELECT:
$sql = mysql_query("SELECT vhf FROM p_loc WHERE vhf IS NOT NULL AND vhf != ''");
This question already has answers here:
Get column name dynamically by Specific row value
(2 answers)
Closed 8 years ago.
I'm begginer in MYSQL.
I need to retrieve the COLUMN NAMES of columns which have value "TRUE"?
I found this code but I don't understand how to user it:
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME`='tablename'
Thanks in advance
I think this will help you MySQL query to get column names?
With the following you will get all information about the specific column
SELECT *
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename';
AND `COLUMN_NAME` = 'namecolumn'
If you want the names of all columns which have the value true in a row you could use foreach if you use php and write all keys in an array for example where the value is true
$list = array();
foreach($rows as $key => $value)
{
if($value === true) $list[] = $key;
}
This code shows all column names from a specific table, the schema INFORMATION_SCHEMA stores information about the items created on your database server(schemas, tables, columns etc.)
The statement above will show you alle columns from tablename, theres also a more simple syntax to do this SHOW COLUMNS FROM tablename (MySQL Manual)
How do you want to determine that the value of a column is true? Maybe there a two rows in your table with the values TRUE and FALSE for one column (TRUE in one row and FALSE in the other row)
With one Row in PHP
If there is only one row in your table execute the SHOW COLUMNS FROM tablename do a fetch_array on the result and then foreach and save only the keys where the value is TRUE.
If you try to store something like a configuration use a key value table and query it using where clause, this will be more effective.
This question already has answers here:
How to SELECT DEFAULT value of a field
(3 answers)
Closed 9 years ago.
I can get the column names for a table but is there a way to retrieve the default data value for each column?
Here is what I'm using to get the tables column names:
$q = $dbh->query("DESCRIBE tablename");
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
print_r($table_fields);
This prints an array of column names, but I'm trying to get the default data value for each column name also.
Another option is to go to the data dictionary and find the value in Information_Schema.Columns. This allows you to limit results to a single column.
$query = <<< endsql
SELECT Column_Default
FROM Information_Schema.Columns
WHERE Table_Schema = '$yourSchema'
AND Table_Name = '$yourTableName'
AND Column_Name = '$yourColumnName'
endsql;
Try this:
$query = "SHOW FULL COLUMNS FROM tableName";
// ...
Column Default.
I hope I helped.
Just give an index after your FETCH_COLUMN. I didn't try your code but based on DESCRIBE manual it will return 6 description columns. So if you have read this you may got the solution. I have read them and I think just by adding 4th index on your code like below will be your solution.
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN,4);
This question already has answers here:
Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?
(6 answers)
Closed 9 years ago.
mysql database.
Table have index on field "Code".
I need to insert to table new rows.
What works faster?
1)
simple index on field Code - for fast select
befor insert check rows : SELECT COUNT(*) FROM table WHERE Code = 'NewCode';
simple insert(if rows not found): Insert into table values ('NewCode')
2)
unique index on field Code - for insert
Insert IGNORE into table values ('NewCode')
For me more secure (and can be a backup of changes - better way) is the first, but I think that the action is similar.
Consult http://dev.mysql.com/doc/refman/5.5/en/insert.html for more detali
paladinux