Your cart is currently empty!
Category: sql
-
Khóa học miễn phí SQL – Show Tables nhận dự án làm có lương
SQL – Show Tables (Listing Tables)
Table of content
There are several instances when you need to retrieve a list of tables from your database. This could be done for testing purposes, to identify any existing tables before adding or removing any, or for any other reason. This tutorial will discuss how we can list down all the table in MySQL, SQL Server and Oracle using simple SQL commands.
MySQL – Listing Tables
You can use SQL SHOW TABLES statements in MySQL to list down all the tables available in a selected database.
Syntax
Following is the syntax to list all the tables in SQL in MySQL −
SHOW TABLES;
Example
Following is an example which will list down all the tables from a testDB database.
USE testDB; SHOW TABLES;
This will display the following output depending on the number of tables available in your database.
Tables_in_testDB CALENDAR CUSTOMERS COMPANIES SALARY SQL Server – Listing Tables
SQL Server does not provide SHOW TABLE command in an SQL Server. Instead, we can use the “SELECT” statement to retrieve information about tables in a database. We have three different commands to use with the SELECT statement to list all the tables in a database −
-
sys.tables
-
information_schema.tables
-
sysobjects
The SYS.TABLES View
Following is the syntax to list down all the tables in SQL using the SYS.TABLES view −
SELECT * FROM SYS.TABLES;
Following is the output of the above query −
name object_id principal_id schema_id CUSTOMER 4195065 NULL 1 ORDERS 68195293 NULL 1 COMPANIES 100195407 NULL 1 SALARY 2107154552 NULL 1 The INFORMATION_SCHEMA.TABLES View
Following is the syntax to list down all the tables in SQL using the INFORMATION_SCHEMA.TABLES view −
SELECT table_name, table_type FROM INFORMATION_SCHEMA.TABLES;
Following is the output of the above query −
table_name table_type CUSTOMER BASE TABLE ORDERS BASE TABLE COMPANIES BASE TABLE SALARY BASE TABLE The SYSOBJECTS View
You can use SYSOBJECTS view to retrieve the information of all the objects created in SQL Server database, including stored procedures, views, system tables and user-defined tables. Following is the basic syntax of using sysobjects view −
SELECT name, id, xtype FROM sysobjects WHERE xtype = ''U
Value Meaning AF Aggregate function (CLR) C CHECK constraint D Default or DEFAULT constraint F FOREIGN KEY constraint L Log FN Scalar function FS Assembly (CLR) scalar-function FT Assembly (CLR) table-valued function IF In-lined table-function IT Internal table P Stored procedure PC Assembly (CLR) stored-procedure PK PRIMARY KEY constraint (type is K) RF Replication filter stored procedure S System table SN Synonym SQ Service queue TA Assembly (CLR) DML trigger TF Table function TR SQL DML Trigger TT Table type U User table UQ UNIQUE constraint (type is K) V View X Extended stored procedure This will produce following result −
name id xtype CUSTOMER 4195065 U ORDERS 68195293 U COMPANIES 100195407 U SALARY 2107154552 U Oracle – Listing Tables
There are following three SQL SELECT statements which you can use to list down the tables available in Oracle.
Listing ALL Tables
Following is the SQL SELECT statement which will list down all the available tables in an Oracle Database.
SELECT owner, table_name FROM ALL_TABLES
Listing DBA Tables
Following is the SQL SELECT statement which will list down all the DBA related tables in an Oracle Database.
SELECT owner, table_name FROM DBA_TABLES
Listing USER Tables
Following is the SQL SELECT statement which will list down all the USER created tables in an Oracle Database.
SELECT owner, table_name FROM USER_TABLES
Listing ALL Views
Following is the SQL SELECT statement which will list down all the views available in an Oracle Database.
SELECT view_name FROM ALL_VIEWS;
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí SQL – Rename Database nhận dự án làm có lương
SQL – Rename Database
There can be several reasons to rename a database name. One of the reasons could be to avoid naming conflicts or to separate different types of data into different databases. Another reason can be to arrange them in an organized way which makes them more descriptive and easier to manage.
The ALTER DATABASE…MODIFY Statement
The ALTER DATABASE…MODIFY statement in SQL is used to rename the name of an existing database name in SQL Database Server. Please note that this command does not work in MySQL database.
Syntax
Following is the syntax of the ALTER DATABASE…MODIFY command −
ALTER DATABASE OldDatabaseName MODIFY NAME = NewDatabaseName;
Example
Following is the SQL command in SQL Server to rename the database testDB to tutorialsDB:
ALTER DATABASE testDB MODIFY NAME = tutorialsDB ;
Rename Database using Dump and Reimport
If you are willing to rename a database name in , then simple way is to dump the complete database in an SQL file and then re-import it into a new database. This is three step process which we will follow in this tutorial:
Step 1 – Dump Old Database
Consider you want to rename testDB database to tutorialsDB. So first we will dump it in a simple SQL file using MySQL mysqldump command. This operation will be performed from command line and will require a database user name and password, preferably admin privilege.
$ mysqldump -u username -p"password" -R testDB > testDB.sql
We are using the -p flag immediately followed by our password to connect to the database with no space between. The -R is required to tell mysqldump to copy stored procedures and functions along with the normal data from the database.
Step 2 – Create New Database
Next step is to using mysqladmin prompt command as follows:
$ mysqladmin -u username -p"password" create tutorialsDB;
Step 3 – Import Old Database
The final step is to import old database into new database as follwing:
$ mysql -u username -p"password" tutorialsDB < testDB.sql;
Step 4 – Verification (Optional)
Now you can verify the changes by listing down all the available databases:
SHOW DATABASES;
Output
The output will be displayed as −
Database |
---|
performance_schema |
information_schema |
mysql |
testDB |
tutorialsDB |
Step 5 – Verification (Optional)
Once you are satisfied with your changes, you can delete your old database as follows:
DROP DATABASE testDB;
Rename Database in SQL using RENAME DATABASE…TO (obsoleted)
SQL provides a simple RENAME DATABASE…TO statement to rename an existing database. If you want to rename a database, make sure there is no active transaction in progress otherwise the complete operation might halt once you rename the database.
Note: The RENAME DATABASE…TO is obsoleted.
Syntax
Following is the syntax of the RENAME DATABASE…TO statement −
RENAME DATABASE OldDatabaseName TO NewDatabaseName;
Example
Before renaming a database, let us list down all the available databases −
SHOW DATABASES;
The output will be displayed as −
Database |
---|
performance_schema |
information_schema |
mysql |
testDB |
Now, issue the following command to rename the database testDB to tutorialsDB:
RENAME DATABASE testDB TO tutorialsDB;
There used to be a simple RENAME DATABASE command in older versions of MySQL which was intended to rename database but RENAME DATABASE command has been removed from all newer versions to avoid security risks.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc