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.
Related
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.
I have a table users that has an auto-incrementing id column. For every new user, I essentially need to insert three rows into three different tables, which are 1. user, 2. user_content, and 3. user_preferences. The rows inserted into user_content and user_preferences are referenced by their id's which correspond to each user's id (held in user)
How do I accomplish this?
Should I do the INSERT INTO user query first, obtaining that auto-incremented id with last_insert_id(), and then the other two INSERT INTO queries using the obtained user id? Or, is there a more concise way to do this?
(note: I am using MySQL and PHP, and if it makes a difference, I am using a bigint to store the id values in all three tables.)
Thank you!
The approach that you've described (insert into user first, take the result of last_insert_id(), and use that to insert to the other two tables) is perfectly reasonable; I see nothing wrong with it.
It might be technically possible to combine the three queries and use the LAST_INSERT_ID() MySQL function to insert values to the other two tables, but this would be significantly more complex without any corresponding benefits. Not really worth doing, in other words.
I see 3 options:
PHP side using some *_last_insert_id (as you describe)
Create a trigger
Use a stored procedure.
I want to delete a row from one of my .sql tables by a user defined integer (1 through rowCount()). The below is pseudocode, but it illustrates what I want I think.
$i = 1; //example
$quedb = $db->query("
DELETE *
FROM table
WHERE ROWNUMBER = '$i'
");
Is there a way to do this within the SQL environment? I don't want to delete a row based on a specific element (a friend of mine suggested querying for an element in the row I define, but I just want to delete the nth row, where n is user defined).
You shouldn't do that.
"nth row" is a nonsense in the context of databases.
Database is something different from lists you're familiar to. They have no predefined order at all.
Database is an abstract heap of rows whose take order only at select time, always different, based on the field(s) chosen to order.
To identify a row you have to use unique identifier, which invented to serve the very purpose.
So, add id auto_incremented primary key field to your table and use it to identify the row.
"Other tables" are not only reason to keep consistency. Your own links on the site require consistent addressing too, no matter if you added or deleted some rows.
If you want to enumerate your output, do it at select time, using PHP. That's the only proper way.
Please, before inventing your own wheel, learn the very basics of database design.
Or at least follow good advises from more experienced people.
You can do that using a nested query. Like so...
Delete from table where id in company(Select id from table limit 5,1)
But it is really not recommended as the behavior is not very consistent.
There's this project I am working on. This is like a social network where we can have users, posts, pictures etc and then this problem came up. We are used to Mysql and the "almost magical" auto-increment field and now we cannot count on it anymore. I know the _id object in Mongo gives an easy way for identifying a document as it guarantee uniqueness. But the key is not user friendly and that's what we need, so we can make urls like:
http://website.com/posts/{post_id}
http://website.com/{user_id}
I developed a solution but I don't think this is the best way of doing this. I first create a mysql table with only one column. This column stores the user_id and it's an auto-increment field. For every new record on mongo I insert a new row in this mysql table and get the user_id with "LAST_INSERT_ID" function, now I can insert my data in my mongo collection with a numeric ID. And other benefit is that I can erase my mysql table let's say, after a million rows because the id's are already stored in mongo.
Am I doing it wrong?
Why not using slugs for posts and usernames for users? That should be human readable.
First, I don't see any benefit to using an arbitrary auto incrementing number over the generated id mongo provides. Not only is not again just a arbitrary id, but you have to maintain the sequence.
That said, why not let mongo manage the id, and use another unique identifier for your URLs. If your users have a 'username', I'm assuming you've already made sure that's unique across the collection. Just query by that unique property, instead of finding by id.
That also allows the user to change their unique identifier, without you having to remap associations in the database.
And for the post, just generate a unique slug from the title.
You can also create the id's in Mongo instead of MySQL, ...here's some documentation and articles on how to achieve it
http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field
http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb
What would be the best way to check if tables exist in a MySQL DB? I was looking at a few code examples and have seen it done a couple of ways.
What I am trying to do is create tables like tag_tagnamehere so each tag has their own table with a link to the post/page ID.
So what I'm thinking is when people inset a list of TAGS I would loop through them and if the table is not found, create it and make and entry to that post/page ID.
CREATE TABLE IF NOT EXISTS
Maybe I didn't understand your design/table structure, although each tag shouldn't reside in its own table. You should have a table of tags, and a normalised scenario where other tables (if need be) define other tag attributes
Create table has that built in.
Create table Foo if not exists
But you probably shouldn't do this. Note how its done here with a PostTag table
CREATE TABLE IF NOT EXISTS <table_name>...
How about directly from a MySQL Query?
CREATE TABLE someTable IF NOT EXISTS