How to Create a Database in MySQL

Download ArticleDownload Article

This How.com.vn teaches you how to create a database in MySQL. In order to create a database, you'll have to open the "mysql" command line interface and enter your database commands while the server is running.

Part 1
Part 1 of 3:

Opening the MySQL Command Line

Download Article
  1. How.com.vn English: Step 1 Make sure that your MySQL server is connected.
    If your MySQL server isn't currently online, you can't create a database.
    • You can check the server's status by opening MySQL Workbench, selecting your server, and looking at the "Server Status" indicator on the "Administration - Server Status" tab.
  2. How.com.vn English: Step 2 Copy the installation folder's path.
    This path will vary depending on whether you're using a Windows computer or a Mac:
    • Windows — Copy C:/Program Files/MySQL/MySQL Workbench 8.0 CE/ making sure to replace the last folder's name with the most current MySQL name.
    • Mac — Copy /usr/local/mysql-8.0.13-osx10.13-x86_64/ making sure to replace the last folder's name with the most current MySQL folder name.
    Advertisement
  3. How.com.vn English: Step 3 Open your computer's command line.
    You'll use Command Prompt on a Windows computer, while Mac users will open Terminal.
  4. How.com.vn English: Step 4 Change to the MySQL installation folder's directory.
    Type cd and a space, paste in the path to the installation folder, and press Enter. For example, you'd do the following on most Windows computers:
    cd C:\Program Files\MySQL\MySQL Workbench 8.0 CE
  5. How.com.vn English: Step 5 Open the MySQL login command.
    For example, to open the login command for a user named "me", you'd type in the following and press Enter:
    mysql -u me -p
  6. How.com.vn English: Step 6 Enter your account password.
    Type in the password for your MySQL user account, then press Enter. This will log you in and connect your command line application to the MySQL prompt.
    • You should see the "MySQL>" tag appear your command line application. From this point on, any commands you enter will be processed through the MySQL command line app.
    • Understand how to enter MySQL commands. MySQL commands must be entered with a semicolon (;) immediately after the last part of the command, though you can also enter the command, type a semicolon, and press Enter again.
  7. Advertisement
Part 2
Part 2 of 3:

Creating a Database

Download Article
  1. How.com.vn English: Step 1 Create your database's file.
    You'll do this by typing in the "create database" command create database, adding your database's name and a semicolon, and pressing Enter. For a database named "Pet Records", for example, you'd enter the following:
    create database Pet_Records;
    • Your database's name cannot have any spaces in it; if you want to add a space to the name, you'll have to use an underscore (for example, "Friends of Mine" would become "Friends_of_Mine").
    • Every MySQL command must end in a semicolon. If you miss the semicolon the first time, you can type it in next to the ... which appears and then press Enter again.
  2. How.com.vn English: Step 2 Display the current databases.
    You can bring up a list of current databases by typing in the following and then pressing Enter:
    show databases;
  3. How.com.vn English: Step 3 Select your database.
    You can select your database from the list by typing use name where "name" is the database's name. For example, for your "Pet Records" database, you would type the following and press Enter:
    use Pet_Records;
  4. How.com.vn English: Step 4 Wait for the confirmation message.
    Once you see the phrase "Database changed" appear below your last-typed command, you're free to proceed with creating the database's content.
  5. Advertisement
Part 3
Part 3 of 3:

Creating a Table

Download Article
  1. How.com.vn English: Step 1 Understand the different table commands.
    There are a few main aspects of your table that you'll want to know before creating one:
    • Title — Your title will go directly after the "create table" command, and must follow the same rules as your database's name (e.g., no spaces).
    • Column Heading — You can determine column headings by typing different names into a set of parentheses (see the next step's example).
    • Cell Length — When determining cell length, you'll use either "VARCHAR" (variable characters, meaning that you can type in between one and the VARCHAR's limit number of characters) or "CHAR" (requires no more and no less than the specified number of characters; for example, CHAR(1) requires one character, CHAR(3) requires three characters, and so on).
    • Date — If you want to add a date to your chart, you'll use the "DATE" command to indicate that the column's contents will be formatted as a date. The date should be entered in YYYY-MM-DD format.
  2. How.com.vn English: Step 2 Create the table outline.
    Before you can input data for your chart, you'll need to create the chart's structure by typing in the following and then pressing Enter:
    create table name (column1 varchar(20), column2 varchar(30), column3 char(1), column4 date);
    • For example, to create a table called "Pets" with two VARCHAR columns, a CHAR column, and a date column, you might write the following:
    • create table Pets (Name varchar(20), Breed varchar(30), Sex char(1), DOB date);
  3. How.com.vn English: Step 3 Add a line to your table.
    Using the "insert" command, you can enter your database's information line-by-line:
    insert into name values ('column1 value', 'column2 value', 'column3 value', 'column4 value');
    • For the "Pets" table example used previously, your line might look like this:
      insert into Petsvalues ('Fido', 'Husky', 'M', '2017-04-12');
    • You can enter the word NULL for a column's contents if the column is blank.
  4. How.com.vn English: Step 4 Insert the rest of your data if possible.
    If your database is relatively small, you can insert the rest of the data line-by-line by using the "insert" code. If you elect to do this, skip the next step.
  5. How.com.vn English: Step 5 Upload a text file if needed.
    If you have a database which requires more lines of information than are practical to insert by hand, you can reference a text file that contains the data by using the following code:[1]
    load data local infile '/path/name.txt' into table namelines terminated by '\r\n';
    • For the "Pets" example, you would write something like the following:
      load data local infile 'C:/Users/name/Desktop/pets.txt' into table Petslines terminated by '\r\n';
    • On a Mac computer, you'll need to use the "lines terminated by" command with '\r' instead of '\r\n'.
  6. How.com.vn English: Step 6 View your table.
    Enter the show databases; command, then select your database by typing in select * from name; where "name" is the database's name. For example, if using the "Pet Records" database, you'd enter the following:
    show databases;select * from Pet_Records;
  7. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What are the basics of MySQL?
    How.com.vn English: Community Answer
    Community Answer
    (1) Getting started with MySQL; (2) querying data; (3) filtering data; (4) sorting data; (5) joining tables; (6) grouping data; (7) MySQL sub query; (8) using set operators; (9) modifying data in MySQL; (10) MySQL transaction; (11) managing MySQL databases and tables; (12) MySQL indexes; (13) MySQL data types; 14. MySQL constraints; (15) MySQL globalization; (16) MySQL import and export.
  • Question
    How do I create a new database and place that database in a specific directory location?
    How.com.vn English: Community Answer
    Community Answer
    in 'phpmyadmin,' create a database with its table name and put it in a php directory.
  • Question
    How do I use Oracle in a database?
    How.com.vn English: Community Answer
    Community Answer
    Oracle is a type of database, not something you use in a database. The cost of an Oracle database instance is generally $100,000 per core (that's a processor). To get started using Oracle, go to Oracle.com.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • Some commonly used data types include the following:
        • CHAR(length) - fixed length character string
        • VARCHAR(length) - variable length character string with max length length
        • TEXT - variable length character string with max length of 64KB of text
        • INT(length) - 32-bit integer with max length digits (the '-' is counted as a 'digit' for a negative number)
        • DECIMAL(length,dec) - Decimal number up to total length display characters; the dec field indicates the maximum number of decimal places allowed
        • DATE - Date value (year, month, date)
        • TIME - Time value (hours, minutes, seconds)
        • ENUM("value1","value2", ....) - List of enumerated values
      • Some optional parameters include the following:
        • NOT NULL - A value must be provided. The field cannot be left blank.
        • DEFAULT default-value - If no value is given, the default-value is assigned to the field.
        • UNSIGNED - For numeric fields, ensures that the number is never negative.
        • AUTO_INCREMENT - The value will be incremented automatically each time a row is added to the table.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

      • If your MySQL server isn't running when you attempt to log into the "mysql" command line, you won't be able to proceed.
      • As with any coding, make sure that your commands are spelled and spaced exactly right before you attempt to enter them.
      Advertisement

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about programming, check out our in-depth interview with Tyrone Showers.

      About This Article

      How.com.vn English: Tyrone Showers
      Written by:
      Technologist
      This article was written by Tyrone Showers and by How.com.vn staff writer, Jack Lloyd. Tyrone Showers is a Technologist and the Co-owner of Taliferro Group, an IT consulting company based in Seattle, Washington. With over 35 years of professional experience, he specializes in API Design, e-Commerce, Operational Efficiency, and website development. He has a B.S. in Computer Science from DeVry Institute of Technology. This article has been viewed 1,211,843 times.
      How helpful is this?
      Co-authors: 35
      Updated: February 21, 2024
      Views: 1,211,843
      Categories: Software
      Thanks to all authors for creating a page that has been read 1,211,843 times.

      Reader Success Stories

      • How.com.vn English: Wallace Howery

        Wallace Howery

        May 25, 2017

        "Helped me get a MySQL database created and running as part of a hibernate test project. Note that it is important..." more
      Share your story

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement