What should be the command in MySQL For showing table content?

132    Asked by Diyatomar in SQL Server , Asked on Jun 13, 2024

 I am a database administrator and I am currently working on managing a MySQL database for a particular company. One day, a developer asks for a detailed description of the structure of the “orders” table along with information about the columns, data types and any constrain such as primary keys or even foreign keys. What command should I use by using MySQL to display the structure of the table called “orders”? 

Answered by Dominic Poole

 In the context of SQL, you can easily display the structure of the “orders” table in MySQL by using the command line by using the following SQL command:-

  “DESCRIBE orders”

Alternatively, you can also use the following command for this purpose:-

  “SHOW COLUMNS FROM orders;”

Expected output

The output would possibly be a table that would provide detailed information about each column in the table called “orders” along with its column name, data types, whether it can be NULL if is part of an index, its default value, and any other additional attribute. Here is a sample of output may look like:-

+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| order_id | int(11) | NO | PRI | NULL | auto_increment |
| customer_id | int(11) | NO | MUL | NULL | |
| order_date | datetime | NO | | NULL | |
| total_amount | decimal(10,2)| YES | | NULL | |
| status | varchar(50) | YES | | ‘pending’ | |
+--------------+--------------+------+-----+---------+----------------+
In this output:-
Field: It is the name of the columns.
Type: It is the data type of the columns.
Null: It refers to whether the columns can contain NULL values or not.
Key: It shows if the column is indexed for a multiple key or not.
Default: The default value of the columns if no values are provided during the time of insertion.
Extra: It would show the additional information about the columns such as “auto_increment” for columns which are automatically incremented with each new row.
Here is a java based example given below which would demonstrate how you can use Java to connect to a MySQL database and then retrieve the structure of the “orders” table by using the DESCRIBE command:-
However, you should have ensured that you have installed MySQL JDBC driver in your classpath of the project. You have also update the database URL, Username, and even password according to your MySQL server setup:-
Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.ResultSet;
Import java.sql.Statement;
Import java.sql.SQLException;
Public class DescribeTable {
    Public static void main(String[] args) {
        // Database connection details
        String jdbcUrl = “jdbc:mysql://localhost:3306/your_database”;
        String username = “your_username”;
        String password = “your_password”;
        // Establish a connection
        Try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
            // Create a statement
            Statement statement = connection.createStatement();
            // Execute the DESCRIBE command
            String describeQuery = “DESCRIBE orders;”;
            ResultSet resultSet = statement.executeQuery(describeQuery);
            // Process the result set
            System.out.println(“+--------------+--------------+------+-----+---------+----------------+”);
            System.out.println(“| Field | Type | Null | Key | Default | Extra |”);
            System.out.println(“+--------------+--------------+------+-----+---------+----------------+”);
            While (resultSet.next()) {
                String field = resultSet.getString(“Field”);
                String type = resultSet.getString(“Type”);
                String isNull = resultSet.getString(“Null”);
                String key = resultSet.getString(“Key”);
                String defaultValue = resultSet.getString(“Default”);
                String extra = resultSet.getString(“Extra”);
                System.out.printf(“| %-12s | %-12s | %-4s | %-3s | %-7s | %-14s |%n”,
                                  Field, type, isNull, key, defaultValue, extra);
            }
            System.out.println(“+--------------+--------------+------+-----+---------+----------------+”);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Your Answer

Interviews

Parent Categories