Is it possible to pull a table's create syntax via mysql? - php

I'm looking to pull a table's create syntax via mysql and php. Is it possible?
I need it for a file that creates table_x automatically every 10 days. Since I update the site constantly and create new fields I'd like the file to be dynamic and use the previous table (instead of me updating it manually each time).

Yes you can - use
SHOW CREATE TABLE tablename;
Actually if you want to 'reset' your database I would use TRUNCATE instead
TRUNCATE TABLE tablename

SHOW CREATE TABLE table_x;
...will output one record with the CREATE syntax. Is that what you're looking for?

Related

Create auto_incremented table name?

I am wondering how I could, using PHP and mysql, create a table with a unique name every time.
So example if i click submit, a table will be created that is named "1".
then if i do it again another table is added and it is named "2"
I searched around but could only find answers to how to auto_increment the columns inside the table so I hoped it would be the same code, I tried this:
mysql_query("CREATE TABLE INT NOT NULL AUTO_INCREMENT PRIMARY KEY(TestColumn CHAR(30))");
It did not work.
So how do you create an auto_incremented table ???
Create a simple file which store a serial number. Then when your script creates a table, you increment the counter in the file with one. Next time, you read the number and use that for the table name. Naturally, you could do this in a table or a flat file.
Just for knowing which tables exist, and what they are for, you'd best create one master table storing not just the latest, but all tables created.
I am lost as to why you would want to do this.. I see no good reason for wanting this.
Create a table of tables and store the number or the number name in that table. Then you can look up MAX number there.
First at all, this function don't exist in PHP or in MySQL. Or maybe I don't know it.
There is 2 solutions to your problem :
Solution number 1 :
As AlanChavez said, you can use this request :
CREATE TABLE IF NOT EXIST ....
But, if you have to create 1000000000 table (it's an example), it will not be optimized.
Solution number 2 :
You can create a table with a single row, where is stored the last name used for your table.
I don't know if it's really optimized, but I think it can work.
I will never recommend to name a database table just with a digit. To keep track of number of click / page-load you can use file, session or another table.

Create table for each id existent in another table

I was wondering if there is a way to create a MySQL table for each ID that exists in another table. I think that would be fairly easy doing it with PHP, but I'm not sure if that can be done with MySQL.
So for instance I have a table called users which has an X amount of columns. One column is the IDs column. So I would like to iterate through that column and grab the IDs and create for each of those IDs a new table which will have the name of "user_specific_id_ " + ID. So for the ID 1 the name of the newly created table would be user_specific_id_1.
Could the above be done just with MySQL, or is it necessary to use PHP ? And if I need to use PHP what would be the approach ?
I'm not familiar with a pure MySQL way. Using PHP you'll select all id's from your table, and then in a foreach loop issue a CREATE TABLE user_specific_id + $id query
That being said, creating a separate table for each user doesn't sound like the correct way of handling a DB.
This sounds like an awfully bad idea.
It is a bad idea because you cannot decently JOIN the tables using mysql.
Instead of a table for each user, consider having a table with a multi-column primary key for all users.
You can, of course do what you described with PHP, for example using PDO.

Monitoring MySQL database using PHP

I have a mysql database with 12,000 entries, what i want setup is the ability to monitor a column in the database and if/when the column is altered for any entry it sends me an email with the details.
EDIT: I have access to mysql db, but not the script which works with it. So it should monitor it for changes...
You could create some triggers on the table, if your version of MySQL has them. A trigger can then invoke any function you care to create. A trigger has the advantage that any insertion or deletion or any update of the column will cause it to fire; you wouldn't have to change any other code to make it happen. See here for more... http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Create a trigger on update
Create another table (lets call it cron_table), where the trigger will insert information of the updated row (may be old value, new value etc)
Setup a cron, which will call a script which will check the cron_table and send email if any entry is found. Cron interval can be setup according to need.
--- If you could send email from trigger, there would be no need for a separate table and cron ---
try something similar to this , you can edit the function to send you and email if the query has insert and TABLE_NAME or COLUMN_NAME in it
set up one column to be a datetimestamp.
This will update on every change of the row. There you can run a sql query either via a cron job or after every few php queries to return you the list of changed rows since the last check.
Select * from tbl_myentries where EntryUpdated > '$TimeSinceLastCheck'
you need to understand Data Manipulation Language (DML) triggers
in my sql: use
CREATE TRIGGER salary_trigger
BEFORE UPDATE ON table_name
REFERENCING NEW ROW AS n, OLD ROW AS o
FOR EACH ROW
IF n.columnName <> o.columnname THEN
END IF;
;
Create a trigger on a change on your column, then insert it to another table as log table.
Run cron job on your table that will send you an email.

MySQL Insert date log

I am trying to find out when something was added to a database I maintain but the script that adds the date was working.
Is there a way to retrieve the date of the original INSERT command?
Something like this?
http://dev.mysql.com/doc/refman/5.0/en/query-log.html
make a new table with the key of the table to watch create a after insert trigger that inserts a new line into the watchout table with the id and the time inserted

Create trigger in MySQL or not (via PHP)?

Table in MySQL have 1 problem column: creation_date.
During inserting a new row through PHP, I thought that there would be correct to insert the date directly in the query, MySQL has to do it himself.
Do I need to do the trigger, or it would be better to use PHP for this, as intended?
How would you have done to?
PS: MySQL: How to create trigger for setting creation date for new rows
Use TIMESTAMP DEFAULT CURRENT_TIMESTAMP field
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
If you create a trigger for your table (or adopt the suggestion above) you won't have to remember to do it in your PHP. This advantage will show when someone else creates another PHP to insert into the same table.

Categories