MySQL selecting columns from a specific table - mysql

I'm learning MySQL and this is probably the most basic of the basic questions I could ask, but I want to make sure I understand the syntax.
I have a MySQL script that created three different databases. If I wanted to select all fields from a specific database's table, I would use
SELECT * from database1.table1
correct? Or would it just be
SELECT * from table1
and if I wanted to select only two fields from another table, would it be
SELECT field1, field2 from database1.table2
or again just the table name?

Both forms are actually fine. In SQL you can qualify names to specify explicitly where a db object is located. This is useful for instance if you have a table2 in 2 different schemas (which is the correct term instead of "database", btw).
As it has been mentioned you can set a default schema to avoid having to add a schema to a reference (like your table2) all the time. If there is no schema given then the default schema (set with the USE schema command) will be taken instead. You still can use an explicit schema (either what is set as default or any other) if you like. This will help when you want to access an object which is not in the default schema.

Related

Skipping or Ignoring Temp tables when using mysqldump

Wondering if there is a way to skip / ignore all temp tables using mysqldump. In our instance, these tables are prefixed as tmp{guid}.
These temp tables have a very short lifespan, they are used for building some sort of reports in its parent application. Lifetime may be up to 1 minute.
EDIT:
It has been suggested that I use the ignore-tables parameter, unfortunately this doesn't provide a way for me to specify a wildcard as the table name (tmp*).
You are not talking about tables from CREATE TEMPORARY TABLE ..., correct? Instead, you are talking about a set of tables with a particular naming convention?
Instead of trying to do it with table names, do it with a DATABASE:
CREATE TABLE TempTables;
CREATE TABLE TempTables.abcd (...);
And reference them via the db name:
INSERT INTO TempTables.abcd ...
SELECT ... FROM TempTables.abcd JOIN ...
Then use the suitable parameters on mysqldump to avoid that oneDATABASE` (or pick all the other databases to dump).

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 copy values from one MySQL database to another

I have 2 databases with different structures.
I need to copy information from database A to database B.
Database A has 1 table while database B has 2 related ones.
It is a Q&A site so the old database (A) has a table that contains both the question and the answer.
In the new database these are separate and the answer must contain a field with the id of the question.
Please help me make a SQL request.
Something like
"INSERT INTO table1 (field1,field3,field9)
SELECT table2.field3,table2.field1,table2.field4
FROM table2"
One more thing .. some values in the new database are known (will be hardtyped .. not taken from the old database)
You can simply use the below and specify manually where needed or grab it from the old database/table you are copying from. Also helps for if the columns are named differently or not as many in the new database.
USE `old_database`;
INSERT INTO `new_database`.`new_table`(`column1`,`column2`,`column3`)
SELECT `old_table`.`column2`, `old_table`.`column7`, `old_table`.`column5`
FROM `old_table`
you need to specify database in statement...
insert into database1.table1
select from database2.table2

Use different schema name than the username

We have a Business Objects X14 universe and we have 2 oracle database schemas (BoDB, BoDB_CONNECT)
BoDB_CONNECT has to execute the queries using the BoDB schema.
I dont have password for BoDB schema.
So, basically I want to login with BoDB_CONNECT username and execute the reports using BoDB schema (by appending BoDB.TABLENAME).
But while I am creating the connection there is no separate schemaname in the Universe. It just have the username and password.
I dont want to hardcode the ownername of every table with BoDB. Is there any way to dynamically do this?
First, you have to be given access to the tables or nothing will work. That is, BoDB_CONNECT needs to have SELECT permissions to all of the tables in the BoDB schema that will be referenced in the universe.
Once that is done, you have a few options to implement your requirement. The most straightforward way is to simply include the schema owner with the table name. This happens automatically when you drag in a table to your model in UDT or IDT, and is the recommended solution.
You can also easily switch the owners, if, for example, the tables are moved to a new schema. Select all of the tables to move, and right-click. In UDT, select "Rename Table" and in IDT select "Change Qualifier/Owner". You can then set the new owner name and that will be applied to all selected tables.
If, for some reason, you won't want the schema name associated with the table, there are two options:
Create a private synonym in the BoDB_CONNECT schema for each table to be referenced in BoDB (ex. create synonym foo for bodb.foo). Thus, the universe will just have a reference to foo. Note, however, that BI4.1 does not currently support private synonyms in UDT/IDT. If you create objects that reference private synonyms, they will work correctly in WebI, but they will not parse in UDT/IDT. I believe this is a bug (since it worked in all prior versions), and I have a support case open with SAP currently.
Switch the default schema. You can change the BEGIN_SQL parameter to set the default schema. In UDT this is done via File->Parameters-Parameter tab; in IDT it's Data Foundation->Properties->Parameters. In either case, you'd set the value of BEGIN_SQL to ALTER SESSION SET CURRENT_SCHEMA=bodb. This statement will be executed at the start of each query session, so references to foo will resolve to bodb.foo. Note, however, that this does not apply to actions within IDT/UDT itself; so you will get parse errors on objects that don't have an owner specified, but the queries will work in WebI.

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