Posts Tagged ‘schema’

Oracle: Get database, tables and instances access permissions

Thursday, January 30th, 2014

Oracle user get permissions of table database and instances access
If you are logged in to Oracle SQL server with sqlplus and you're not sure to which Database, Tables, Object instances you have permissions to below 2 queries will be of use:

SQL> SELECT DISTINCT OWNER, OBJECT_NAME  FROM ALL_OBJECTS  WHERE OBJECT_TYPE = 'TABLE';

Query lists all queries in a Oracle table schema. Alternavite shorter way to do the query is via:

SQL> SELECT table_name FROM user_tables;

SQL> SELECT * FROM TAB;

Shows your own schema's all tables and views.

Other oracle useful query is the Oracle equivalent of MySQL SHOW TABLES;

SQL> SELECT table_name FROM user_tables;

It will also output info only for logged in user credentials, if you're logged in as oracle database administrator (DBA role) account and you would like to check what Instances are owned by any user lets say user GEORGI query should be;

SQL> SELECT DISTINCT OWNER, OBJECT_NAME  FROM ALL_OBJECTS  WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'GEORGI;

Other way to do it is via:

SQL> SELECT Table_Name from All_Tables WHERE OWNER = 'YOURSCHEMA';


How to list and exclude table names from a database in MySQL (exclude table names from an show tables in MySQL) by using information_schema

Wednesday, March 30th, 2011

Listing all table names from a MySQL database is a very easy and trivial task that every sql or system administrator out there is aware of.

However excluding certain table names from a whole list of tables belonging to a database is not that commonly used and therefore I believe many people have no clue how to do it when they have to.

Today for one of my sql backup scripts it was necessary that certain tables from a database to be excluded from the whole list of tables for a database I’m backupping.
My example database has the sample name exampledatabase and usually I do list all the table contents from that database with the well known command:

mysql> SHOW tables from exampledatabase;

However as my desire was to exclude certain tables from the list (preferrably with a certain SQL query) I had to ask around in irc.freenode.net for some hints on a ways to achieve my exclude table goals.

I was adviced by some people in #mysql that what I need to achieve my goal is the information_schema mysql structure, which is available since MySQL version 5.0.

After a bit of look around in the information_schema and the respective documentation on mysql.com, thanksfully I could comprehend the idea behind the information_schema, though to be honest the first time I saw the documentation it was completly foggy on how to use this information_schema;
It seems using the information_schema is very easy and is not much different from your normal queries syntax used to do trivial operations in the mysql server.

If you wonder just like I did what is mysql’s information_schema go and use the information_schema database (which I believe is a virtual database that is stored in the system memory).

For instance:

mysql> use information_schema;
Database changed
mysql> show tables
+---------------------------------------+
| Tables_in_information_schema |
+---------------------------------------+
| CHARACTER_SETS |
| COLLATIONS |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS |
| COLUMN_PRIVILEGES |
| KEY_COLUMN_USAGE |
| PROFILING |
| ROUTINES |
| SCHEMATA |
| SCHEMA_PRIVILEGES |
| STATISTICS |
| TABLES |
| TABLE_CONSTRAINTS |
| TABLE_PRIVILEGES |
| TRIGGERS |
| USER_PRIVILEGES |
| VIEWS |
+---------------------------------------+
17 rows in set (0.00 sec)

To get a general view on what each of the tables in the information_schema database contains I used the normal SELECT command for example

mysql> select * from TABLES limit 10;

I used the limit clause in order to prevent being overfilled with data, where I could still see the table fields name to get general and few lines of the table to get an idea what kind of information the TABLES table contains.

If you haven’t got any ecperience with using the information_schema I would advice you do follow my example select and look around through all the listed tables in the information_schema database

That will also give you a few hints about the exact way the MySQL works and comprehends it’s contained data structures.

In short information_schema virtual database and it’s existing tables provides a very thorough information and if you’re an SQL admin you certainly want to look over it every now and then.

A bit of playing with it lead me to a command which is actually a good substitute for the normal SHOW TABLES; mysql command.
To achieve a SHOW TABLES from exampledatabase via the information_schema info structure you can for example issue:

select TABLE_NAME from TABLES where TABLE_SCHEMA='exampledatabase';

Now as I’ve said a few words about information_schema let me go back to the main topic of this small article, which is How to exclude table names from a SHOW tables list

Here is how exclude a number of tables from a complete list of tables belonging to a database:

select TABLE_NAME from TABLES where TABLE_SCHEMA='exampledatabase'
AND TABLE_NAME not in
('mysql_table1_to_exlude_from_list', 'mysql_table2_to_exclude_from_list', 'table3_to_exclude');

In this example the above mysql command will list all the tables content belonging to exampledatabase and instruct the MySQL server not to list the table names with names mysql_table1_to_exlude_from_list, mysql_table2_to_exclude_from_list, table3_to_exclude

If you need to exclude more tables from your mysql table listing just add some more tables after the …’table3_to_exclude’, ‘new_table4_to_exclude’,’etc..’);

Of course this example can easily be adopted to a MySQL backup script which requires the exclusion of certain tables from a backed up database.

An example on how you can use the above table exclude command straight from the bash shell would be:

debian:~# echo "use information_schema; select TABLE_NAME from TABLES where
TABLE_SCHEMA='exampledatabase' AND TABLE_NAME not in
('mysql_table1_to_exlude_from_list', 'mysql_table2_to_exclude_from_list', 'table3_to_exclude',);"
| mysql -u root -p

Now this little bash one-liner can easily be customized to a backup script to create backups of a certain databases with a certain tables (e.g. with excluded number of tables) from the backup.

It’s seriously a pity that by default the mysqldump command does not have an option for a certain tables exclude while making a database dump.
I’ve saw the mysqldump exclude option, being suggested somewhere online as a future feature of mysqldump, I’ve also seen it being reported in the mysql.com’s bug database, I truly hope in the upcoming releases we will see the exclude option to appear as a possible mysqldump argument.
 

How to move only database and tables structure from MySQL server to another – Dump only empty SQL Schema table structure

Thursday, January 17th, 2013

 

mysql sql dump empty database and tables re-create only SQL structure from one host to another Linux

For web development purposes it is necessery to copy MySQL SQL database schema structure without copying the filled in data. A typical case where a replicate of SQL server structure is needed to be installed on another server is on whether a client is bying a new website and it is planned his website Database Design is similar or same like another already working productive website.

Thanksfully, one doesn't have to script in perl or bash cause  mysqldump dump tool has already integrated option for that (–no-data).

Here what mysqldump man page says of  –no-data;

 

  –no-data, -d

           Do not write any table row information (that is, do not dump table contents). This is useful if you want to dump only the
           CREATE TABLE statement for the table (for example, to create an empty copy of the table by loading the dump file).

 

1. Moving SQL data scheme for all databases in MySQL Server

 On host with SQL containing productive data, to dump only the structure of databases / tables and table type, fields rows etc.:

host1# mysqldump -d -h localhost -u root -p'your_password' >sql-all-dbs-tables-empty-structure.sql

Then on the secondary MySQL server, where empty SQL structure (without any filled in info) is needed run:

host2# mysql -u root -p'your_password' < sql-all-dbs-tables-empty-structure.sql

 

2. Moving SQL data structure for only concrete database

On Linux host1 shell issue;

host1# mysqldump -d -h localhost -u root -p'your_password'  database_name>sql-database-empty-structure.sql

On host2 server type;
host2# mysql -u root -p'your_password' < sql-database-empty-structure.sql

3. Moving SQL data structure for few databases

Lets say you have a user (new_user), who has privileges over a number of databases and you want to dump a dump copy of those empty databases;
Same like with one table, just include names of all databases scheme to dump;

host1# mysqldump -d -h localhost -u new_user -p'your_password'  database_name atabase_name2 database_whatever >sql-only-some-databases-structure.sql
 

Then to import on host2 again;

host2# mysql -u new_user -p'your_password' < sql-only-some-databases-structure.sql

4. Dumping and copying only database names from one MySQL to another

Though the case might be rary you might need to dump and copy only list of all databases existing without recreating table database sub-structure. This is doable like so:

On SQL node host1 run;

host1# for i in $(echo "show databases;" | mysql -u root -p|grep -v -E 'Database$' |grep -v information_schema); do echo $i >> structure.txt; done

host2# for i in $(cat structure.txt); do echo "create database $i;" | mysql -u root -p; done

Though I've tested all this and it is safe to use, if you're re-creating SQL database / tables structure make sure you have a working copy of data from SQL.
Well that's it hope this little article helps someone 🙂