Creating empty table in mySQL - mysql

I want to create an empty table, which will be populated by columns much later in a script.
I've tried
$command =
"CREATE TABLE table ()";
in my script, but it won't work. It will only work if I put a filler column inside.
$command =
"CREATE TABLE table (
LameFiller varchar(30)
)";
But I don't like the idea of putting a filler inside. How do I properly create an empty table?

As others have said, you cannot create an empty table.
The best approach at this point isn't then figuring out how to create empty tables, it's not needing to in the first place.
Create the table once its structure finalized
This is the best option. You really don't want to be inserting data into a table, then altering, then inserting, then altering, etc. That just screams poor design. You should be able to come up with your table structure before you ever add data to it.
Create the table before the first data entry
You'll never insert data into a table with no columns (please let me know if you do, because that would be neat and break 2 impossibilities). As such, create the table when you first need to add data to it. If you plan on altering the table or adding more columns, I'd advise against this. Again, that's just poor design.

In some design cases it is necessary to want an empty table. For example, if you are working on a design project where you want people to create their own custom tables within a database. However, it is not possible to create an empty table. The way around this is to find a single column (usually title or name ect.) and send that variable to your php create table function. from there have a add column button that calls a separate php function that adds another column to the database. Tried to keep this as vague as possible to fit multiple scenarios and just cover a core concept to work around the issue.

Related

Add new columns without using Alter

Is it possible to add new columns to an existing table without using alter statement?
Other people are answering unequivocally "no, it is not possible." This is the answer to your literal question. But I'm wondering why you ask the question.
One of the biggest pain points of MySQL is that using ALTER TABLE locks the table while you're making a change like adding a column, and the more data in your table, the longer this lasts while it restructures the table. I'm guessing this is the issue you have, and you're trying to get an alternative that doesn't block access to the table while you're adding a new column.
(In the future, it would help folks give you the best answers if you explain more about what you're trying to do.)
The answer to this question is yes, there is a solution: pt-online-schema-change is a free tool that accomplishes this.
You use it just like you would use ALTER TABLE, but you use it at the command-line instead of in an SQL query.
pt-online-schema-change --alter "ADD COLUMN c1 INT" D=sakila,t=actor
In this example, the database name is sakila and the table name is actor. The script does a lot of work behind the scenes:
Create a table like the original table, but empty of rows
ALTER TABLE to add the column or whatever other alteration you told it. You can do anything you would normally do with ALTER TABLE. In fact, it's doing ALTER TABLE for you, against the empty copy table.
Copy rows from the original table to the new table in the background.
Create triggers to capture any changes made to the original table while it's gradually copying the bulk of the data.
Swap the names of the new table (with the extra column) and the original table, once all data has been copied.
Drop the original table.
This has a few caveats, like the original table must have a primary key, and must not have existing triggers.
It tends to take longer than doing a traditional ALTER TABLE, but since it's not blocking access to the original table, it's still more convenient.
Does this help?
Is it possible to add new columns to an existing table without using the alter statement?
No.
Is it possible to add new columns to an existing table without using alter statement?
I don't think it's impossible.
However I'm not sure what you want to do.
lets say you have a table
select * from Store
and you want just export the data or perhaps you want to do something with that data like a selection. but you don't want to STORE the data in your Database
you can just fill a value and give it a name
select
'Test' as name,
*
from Store
this will populate your column with the value your entered.
data results

Use Faker to fill table without knowing table structure

Can I use (and if yes -- then how) Yii2 Faker to fill entire table (all columns) with random data for n records without knowing table structure? Can Faker check schema and do this for me or do I have to write my own code, that will use it in this scenario?
I want, for example to test, how large my database will become, when I feed it with let's say millions of records. Since my database contains many tables and each table has different structure, I would like to use something automated rather than writing my own code for each table and each structure.
Is this possible with Faker or possibly any other extension to Yii2?
Take a look at Gii, it goes through all the columns on a table and does some things. You can also figure out that columns are foreign keys and get data from other tables.
I do not know of anything that does this for you automatically it is doable.
1 thing, you have to give it an order to fill in the tables, it will not work unless you fill the tables in a specific way especially with the foreign keys.

Mysql trigger create table name based on users form data

Thanks for reading first of all. I've been searching and trying for a few days now to no avail.
I have a form users fill in which populates a mysql database. A trigger is set for after insert.
I need to use the UserID as a table name for each user. The tables all contain the same colums but need different names.
For example my basic idea of
CREATE TABLE NEW.UiD
(
ID int NOT NULL AUTO_INCREMENT
);
Also tried setting a variable first then using that as the table name.
Unfortunately this is honestly the best way to impliment this database.
As I'm sure you're thinking already "what an idiot". Well indeed I am feeling that way.
Please point me in the right direction.

Prevent Duplicate Table entry in Database

I'm trying to Create new table for record of each company and its information. And for that I'm using simple Create query. Is there any way to prevent duplication of Table?
In every database that I know of, duplicate table names are not allowed. In some, there is a third element, the schema, but I assume everything is in the same schema.
In other words, trying to create a duplicate will cause an error. Try it.
By the way, you should always tag your questions with the database you are using.

MySQL find record through many table

In my case I have many table in my database.
My goal is to create a search engine where user can create all logical search he wants.
So I need to find a solution to generate all join based on user search critera.
In some case table has (1:n) links in other case (n:1).
One solution is to image all links and create all join, but I thinks it's a worse solution.
So if you have an idea, I'll very happy to read that.
Thanks a lot.
You can manage it like this don't know it is good or bad but a solution.
Create a new table containing all the searchable fields from various tables and reference to the record id to that table should also be stored in this table.
Insert the new record in this table whenever a new record inserted in those tables.
Search in this single table containing data from all other tables.
OR
consider to use VIEW