Default Database From SQL Script in MySQL - mysql

Is it possible to get a table with a sql script from a specific schema, so I don't have to double click the schema in order to make it like the default/currently active schema in mysql?

This is easily achieved by simply specifying the schema name as part of your SQL statement.
For example, if your schema is named foobar and your table is named myTable:
SELECT *
FROM `foobar`.`myTable`;
The syntax for this is simply:
-- With backticks
`schema_name`.`table_name`
-- Without backticks
schema_name.table_name

Related

Using a pre made sql query to get table structure

I'm rather new at database management, so this might not be feasible, but I got a handful of SQL select queries, rather long ones at that. What I'd like is to get the table column names and structure, without access to the actual database, so as to get a map of all this queries.
context: All we have are the queries used to output tables that will be given to us latter.
This need not be done with actual SQL code, maybe a short script in other language or a utility somebody knows of (but I do have MySQL workbench)
You can add a CREATE TABLE statement in front of your select queries to get the column names.
You cannot infer data types or keys from select queries.
For column names do something like:
drop table if exists your_table_name;
create table your_table_name
select *
from ...
where the select * portion is replaced by the select queries you have.
Then to see the column names in a friendlier way you can do:
show create table your_table_name;
or
desc your_table_name;

how to remove special characters from mysql field name

After importing an Excel table that contained some special characters (like carriage returns or line feeds) in the headers row, it seems that the phpMyAdmin utility handled this situation silently by inserting those chars in the field's name.
The problem arose later when I tried to import the table into other environments/tools like data integrators, etc. For example, the column "Date Start" was imported into the table as "Date\nStart", with a LINE FEED in the middle.
The field rename operation through phpMyAdmin fails with this error:
**\#1054 - Unknown column 'Date Start' in 'mytable'**
The obvious workaround would be to edit the original Excel file by hand (removing LF's) then reimporting the table in MySql as before, but I'm in the position of needing to refresh the schema while preserving the data in the table.
Next I tried this from an SQL panel in phpMyAdmin (note the \n in the field name, VARCHAR(16) is just an example, DATETIME or INT should work as well):
ALTER TABLE mytable CHANGE `Date\nStart` `Date Start` VARCHAR(16)
but again it gives error #1054 - Unknown column 'Date\nStart' in 'mytable'
I also checked the INFORMATION_SCHEMA db, but as #Steve stated below, it's a read-only database.
I'm using MySql 5.5.32 and phpMyAdmin 4.0.4.1 with a Win7 desktop. Any suggestions?
First of all, by reading the MySql manual you can appreciate (or hate) the extreme flexibility allowed by the naming rules, details on the special characters that are/aren't allowed in a table and column names can be found in this manual page:
https://dev.mysql.com/doc/refman/5.5/en/identifiers.html
After several attempts escaping the CR character I've found a solution that works from the phpMyAdmin SQL pane, I think it should work on command-line sessions as well (didn't try that).
In case you inadvertently created or imported columns with CR's in the name, it is possible to fix it by typing the ENTER key within the column name, inside the SQL ALTER TABLE statement (you MUST enclose names in backticks for this trick to work).
Example: To replace the unwanted 'Date\nStart' column name with 'Date Start' you should type this (please note, the CR/Enter at the end of the first line!):
ALTER TABLE mybuggytable CHANGE `Date
Start` `Date Start` VARCHAR(16)
As explained above, you can spot columns with CR's embedded with this statement:
USE INFORMATION_SCHEMA; SELECT * FROM COLUMNS WHERE COLUMN_NAME like '%\n%'
I typed the ALTER TABLE command in the my phpMyAdmin SQL pane, and it just worked fine.
I thought you couldn't write to INFORMATION_SCHEMA because of a permission issue, but after reading the MySQL Manual I realise this is expected behavior as the manual states:
Although you can select INFORMATION_SCHEMA as the default database with a USE statement, you can only read the contents of tables, not perform INSERT, UPDATE, or DELETE operations on them.
To achieve a table rename by using the RENAME TABLE command, first run a select query to find all the tables that need changing and then rename them replacing the carnage return with a space character.
To rename just a column from within a table the ALTER TABLE command can be used with the CHANGE COLUMN parameters, for example:
ALTER TABLE table_name CHANGE COLUMN 'Date\nStart' 'Date Start' DATETIME
I know you've already said that is the command you need, so I've tested this myself by firstly selecting the tables and then running the ALTER TABLE command and it worked fine. I was using the command line interface, so maybe the problem lies with phpMyAdmin - can you confirm it isn't encoding or escaping \n?
Here is what I tested via the command line and worked OK:
SELECT COLUMN_NAME
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE TABLE_SCHEMA = 'test_345'
AND TABLE_NAME LIKE '%\n%';
ALTER TABLE test_table1 CHANGE COLUMN 'Date\nStart' 'Date Start' DATETIME;
Either of these could be wrapped up into a routine should you think this would be useful in the future.

SQL command to automatically create table based on data being inserted

I have to load some data into a temporary table, but the data is never uniform, the datatype and number of columns will always be different.
Is there an SQL command that will automatically create table specifications based on data that will be loaded into it?
Assuming that you're populating it from a query, you can use the syntax CREATE TABLE tablename SELECT ...; see ยง12.1.14.1. CREATE TABLE ... SELECT Syntax in the MySQL 5.6 Reference Manual.
SELECT ... INTO should do what you want.

sql developer mysql to oracle migration

I successfuly migrate mysql to oracle. But the only problem is case sensitive on table name and fieldname.Some pages in web said go to tools and option in sql developer and tick the ansi but i cannot find it.
On oracle website forum said it part of migration .
Anybody had new version of sql developer and migrate from mysql ?
E.g
calendarColor become CALENDARCOLOR
I really don't see how this is a problem. Since Oracle's objects are case-insensitive by default, you can continue to query them using SELECT * FROM calendarColor.
If you need them to be case sensitive, use quotes, like:
CREATE TABLE "calendarColor" ( ... );
SELECT * FROM TABLE "calendarColor";
See also: Schema Object Names and Qualifiers
If the table was created using
CREATE TABLE calendarcolor ( calendarColorId NUMBER(10,0) NOT NULL );
then the table name is stored in uppercase internally. When you run a statement like this:
select * from "calendarColor"
then you are telling Oracle: The table name should be treated case-sensitive but as there is not table named calenderColor, only one named CALENDARCOLOR your statement fails.
The fix is very easy: remove the quotes and change the select to
select * from calendarColor

Generate table DDL via query on MySQL and SQL Server

Is there an easy way to extract table DDL information, via a query, using either Ms or My SQL server? (preferably both?)
For example, using MySQL Administrator / Navicat for MySql, there is a "DDL" function, which generates the "create table foo (....)" script.
Is there any way to get this information from a query itself, such as:
Select DDL from foo where table_name='bar';
Any have the "Create table bar (.....)" returned to me?
If not - any suggestions?
it's mysql-specific, but SHOW CREATE TABLE <table-name> gives you the DDL for a table.
You have to create that yourself.
You can query INFORMATION_SCHEMA.COLUMNS for the column name and data type.
You can't get the CREATE Table text in a cross platform way, but you can get enough information to build it yourself from the INFORMATION_SCHEMA views.