in mysql, there is some pre-created databases, in information_schema database contains a lot of stuff, among them :
information_schema.TABLES contains table_name
which contains every table name in databases, and it's linked to table, when clicked it select the table for you
my question is, can i do the same thing to table?
i did (foreign key) or (indexes) for columns before but never tried for table, is it possible?
thanks!
Related
I have 5 schemas, each of which has its own number of tables, but I have one table common for all the schemas(ex: Student). Can anyone tell me SQL query on how to add a field to all the Student table present in all the schema's.
I have a bunch of tables in my "stats" database.
tcl20151w1d1
tcl20151w1d2
tcl20151w2d1
tcl20151w2d2
tcl20151w3d1
tcl20151w3d2
tcl20151w4d1
eu20151w1d1
eu20151w1d2
eu20151w2d1
eu20151w2d2
eu20151w3d1
eu20151w3d2
eu20151w4d1
..
How can i select all tables that starts with "tcl" in "stats" database. Is it possible? Do I have to union them manually?
You can query information_schema.tables table to get a list of tables where the table name start with tcl.
You can use the list to dynamically create a union query in a stored procedure using string concatenation and prepared statements.
If those tables are all myisam tables with the same structure, you may consider creating a merge table on them:
The MERGE storage engine, also known as the MRG_MyISAM engine, is a
collection of identical MyISAM tables that can be used as one.
“Identical” means that all tables have identical column and index
information.
I have a database with hundreds of the same tables however not all tables have the same column order. I don't know which tables and I don't know what order they are in, only that the columns are the same for every table. Is there a way to rearrange all columns in a table? I know with the Alter statement you can rearrange one column but I would like to to all columns at once for a table.
EDIT:
I would need to do this for all tables using the table_schema and then create a query for all. So exporting the database and changing the create table isn't an option since I would need to change it for all tables.
Thanks to CBroe here is a solution:
make an export of only the data of the database and create a new database.
Then run this query in the information_schema:
SELECT CONCAT("Create Table `db_new`.`", TABLE_NAME, "` LIKE `db_old`.`default` ;") as MySQLCMD FROM TABLES where TABLE_SCHEMA = 'db_old'
Then execute all these queries which are generated for you to create all the tables with the same structure (default table).
Then import the data in the new database.
I'm trying to create relational database. I have 2 columns in 2 tables that I would like to connect, both are varchars.
Both relations are one to many. One column Patch_No and second Champion. I've added to both indexes.
First table:
Second Table:
Here is also relational view:
http://i.imgur.com/G5MHVsD.png
I can "copy" a table using:
CREATE TABLE copy LIKE original_table
and
CREATE TABLE copy as select * from original_table
In the latter case only the data are copied but not e.g primary keys etc.
So I was wondering when would I prefer using a select as?
These do different things. CREATE TABLE LIKE creates an empty table with the same structure as the original table.
CREATE TABLE AS SELECT inserts the data into the new table. The resulting table is not empty. In addition, CREATE TABLE AS SELECT is often used with more complicated queries, to generate temporary tables. There is no "original" table in this case. The results of the query are just captured as a table.
EDIT:
The "standard" way to do backup is to use . . . . backup at the database level. This backs up all objects in the database. Backing up multiple tables is important, for instance, to maintain relational integrity among the objects.
If you just want a real copy of a table, first do a create table like and then insert into. However, this can pose a challenge with auto_increment fields. You will probably want to drop the auto_increment property on the column so you can populate such columns.
The second form is often used when the new table is not an exact copy of the old table, but contains only selected columns or columns that result from a join.
"Create Table as Select..." are most likely used when you have complex select
e.g:
create table t2 as select * from t1 where x1=7 and y1 <>2 from t1;
Now, apparently you should use Create Like if you don't need such complex selects. You can change the PI in this syntax also.