How To Create A Table In MySQL Database

1. The Syntax Of Creating Data Table In MySQL Database.

  1. First, connect and login to the MySQL database server with username and password.
    $ mysql -h localhost -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 8.0.23 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
  2. Before creating a table, you need to use the use databasename command to select a database, then you can create a table in this selected database.
    mysql> show databases
        -> ;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.03 sec)
    
    mysql> 
    mysql> use mysql
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
  3. Under the mysql > prompt, it’s easy to create a MySQL table. Use the SQL command create table to create a table like below.
    CREATE TABLE table_name (column_name column_type);
    mysql> CREATE TABLE account_tbl(
    
       -> id INT NOT NULL AUTO_INCREMENT,
    
       -> username VARCHAR(100) NOT NULL,
    
       -> passwd VARCHAR(100) NOT NULL,
    
       -> on_board_date DATE,
    
       -> PRIMARY KEY ( id )
    
       -> );
    
    Query OK, 0 rows affected (0.16 sec)
    
    mysql>

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky