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.
Related
This question already has answers here:
MySQL query to get column names?
(22 answers)
How do I count columns of a table
(8 answers)
Closed 5 years ago.
I want to list the number of columns in a MySQL database table with PHP. How can I achieve this?
The INFORMATION_SCHEMA built into every MySQL server can be used to find this information.
Try this query to get a list of each table in your current database along with its number of columns.
SELECT TABLE_NAME, COUNT(*) AS column_count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=DATABASE()
GROUP BY TABLE_NAME
If you want just one table's results, try something like this.
SELECT COUNT(*) AS column_count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mytable'
If you want to count number of columns then use
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'database_name'
AND table_name = 'tbl_name';
This question already has answers here:
SQL WHERE condition is not equal to?
(10 answers)
Closed 2 years ago.
I have an html table with checkboxes. Users are permitted to delete rows from that html table except one specific one, whose value is hard coded in the DB.
If a user accidently checks it regardless, I want my server code (php) or even better the MySqL to run a delete query for all the rows they checked EXCEPT for that 1 specific row. So,something like this( which is wrong):
DELETE FROM table_name
WHERE row_id = some_value
EXCEPT row_id = some_value;
Many thanks !
I think the logic you want is AND:
DELETE FROM table_name
WHERE row_id = some_value AND row_id <> some_value;
Of course, some_value should be different most of the time.
DELETE FROM table_name WHERE row_id NOT IN (do_not_delete_this_value );
do_not_delete_this_value will be the id of the row you don't want to delete, rest all records will get deleted.
In fact there are a couple of ways to do it, for deleting all row's except one specific ID, type the following command in SQL query:
In between parentheses, type the ID number that you want to keep.
DELETE FROM `table_name` WHERE `ID` NOT IN (1)
Also, to delete all records except some id's, use this command:
DELETE FROM `table_name` WHERE `ID` NOT IN (1,2,6,10)
This question already has an answer here:
How to select from MySQL where Table name is Variable
(1 answer)
Closed 9 years ago.
In my application I want use dynamic table name.
In my application I have two queries.
1) SELECT `table_name` FROM `data_tables` WHERE `table_id`="1"
From this query I get a table name and I am saving it in a variable; for e.g: $tab
I have another query:
2) SELECT * FROM `'.$tab.'`;
I want to know is there anyway to club these queries? Is it a good practice?
My application is currently working fine, but I would like some insight.
DECLARE #Name varchar(max)
SELECT #Name =`table_name` FROM `data_tables` WHERE `table_id`="1"
SELECT * FROM `#Name`
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.
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