How to Create a Table in MySQL

Part 1 of 2:

Creating the Table

  1. How to Create a Table in MySQL Picture 1
    Open your database. In order to create a table, you must have a database to house it in. You can open your database by typing USE database at the MySQL command prompt.
    1. If you don't remember your database's name, type SHOW DATABASES; to list the databases on the MySQL server.
    2. If you don't have a database yet, you can create one by typing CREATE DATABASE database;. The database name cannot contain spaces.
  2. How to Create a Table in MySQL Picture 2
    Learn the basic data types. Every entry in the table is stored as a certain type of data. This allows MySQL to interact with them in different ways. The data types you will use will depend on the needs of your table. There are many more types than these, but you can use these to make a basic, useful table:
    1. INT - This data type is for whole numbers, and is often used for ID fields.
    2. DECIMAL - This data type stores decimal values, and is defined by the total number of digits and after the number after the decimal. For example: DECIMAL(6,2) would store numbers as "0000.00".
    3. CHAR - This is the basic data type for text and strings. You would normally define a limit for the number of characters stored, such as CHAR(30). You can also use VARCHAR to vary the size based on input. Phone numbers should also be stored using this data type, as they often contain symbols and do not interact with numbers (they are not added, subtracted, etc.)[1].
    4. DATE - This data type stores dates in the format YYYY-MM-DD. Use this if you need to store someone's age as opposed to their actual age, otherwise you will need to update the entry every year.
  3. How to Create a Table in MySQL Picture 3
    Create your table. To create your table in the command line, you will be creating all of your fields in one command. You create tables using the CREATE TABLE command, followed by your table's information. To create a basic employee record, you would enter the following command:
    CREATE TABLE employees (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, lastname VARCHAR(20), firstname VARCHAR(20), phone VARCHAR(20), dateofbirth DATE) 
    1. INT NOT NULL PRIMARY KEY AUTO_INCREMENT creates an ID number for each employee that is added to the record. The number increases each time automatically. This allows you to easily reference employees with other functions.
    2. Although VARCHAR allows you to trim the size based on input, you can set a limit for it so that the user can't input strings that are too large. In the above example, both first name and last name are limited to 20 characters each.
    3. Note that the phone number entry is stored as VARCHAR so that symbols are handled correctly.
  4. How to Create a Table in MySQL Picture 4
    Verify that your table was created properly. Once you create your table, you will receive a message that it was successfully made. You can now use the DESCRIBE command to ensure that you included all the fields you wanted to and that they have the right data types. Type DESCRIBE database; and refer to the chart that appears to check your table's structure.
  5. How to Create a Table in MySQL Picture 5
    Create a table using PHP. If you are using PHP to administer your MySQL database through a webserver, you can create a table using a simple PHP file. This assumes that the database already exists on your MySQL server. Enter the following code to create the same table as Step 3, replacing the connection information with your own:
     
Part 2 of 2:

Adding Entries to Your Table

  1. How to Create a Table in MySQL Picture 6
    Add a single entry to your table. You can enter data into your table directly from the command line. You can use one command to enter all of the related fields for an entry using INSERT INTO:
    INSERT INTO employees (id, lastname, firstname, phone, dateofbirth) VALUES (NULL, 'Smith', 'John', '(555)555-5555', '1980-01-31'); 
    1. By entering NULL for the ID, the value will increase by 1 from the last entry, resulting in the next ID number.
    2. Make sure that each value you enter has single quotes (') around it.
  2. How to Create a Table in MySQL Picture 7
    Add multiple entries at once. If you have all of the data in front of you, you can insert multiple entries with one INSERT INTO command. Simply separate the value sets with a comma:
    INSERT INTO employees (id, lastname, firstname, phone, dateofbirth) VALUES (NULL, 'Smith', 'John', '(555)555-5555', '1980-01-31'), (NULL, 'Doe', 'Jane', '(555)555-5551', '1981-02-28'), (NULL, 'Baker', 'Pat', '(555)555-5554', '1970-01-31'); 
  3. How to Create a Table in MySQL Picture 8
    Display your table. Once you've inserted a few entries, you can display your table to see how everything looks. This will let you see if you've missed any information or if something is in the wrong spot. To display the table created above table, type SELECT * FROM employees.
    1. You can perform more advanced displays by adding filters to the search. For example, to return the table sorted by date of birth, you would type SELECT lastname, firstname, dateofbirth FROM employees ORDER BY dateofbirth
    2. Reverse the order of the results by adding DESC to the end of the command.
  4. How to Create a Table in MySQL Picture 9
    Enter data using an HTML form. There are other ways to enter data into your new table. One of the most common is through using a form on a web page. To see how to create a basic form to fill out your table, see this guide.
5 ★ | 1 Vote

May be interested

  • MS Access 2003 - Lesson 15: Create an initial tableMS Access 2003 - Lesson 15: Create an initial table
    in this section, you are ready to create a table for the new database. in chapter 2, you used the table wizard to create tables easily. this not only shows that creating a table manually is difficult; but it is a sure way to understand the table.
  • How to create automatic table of contents in Word 2016How to create automatic table of contents in Word 2016
    you can create your own table of contents manually, but that takes a lot of time and effort. the following article details how to create a table of contents automatically in word 2016, with older versions of word you can do the same.
  • How to Check Database Size in MySQLHow to Check Database Size in MySQL
    this wikihow teaches you how to check the size of a mysql database. you can check the size of a database using mysql workbench, or by running a query in mysql. open mysql workbench. it has a blue icon that with an image that resembles a...
  • CREATE TABLE command in SQL to create a database tableCREATE TABLE command in SQL to create a database table
    what does the create table command in sql do? in this article, let's learn everything you need to know about the create table statement in sql!
  • 35 tools, scripts and plugins to build HTML Table35 tools, scripts and plugins to build HTML Table
    there are many ways to visualize data, ie you can design a beautiful infographics or create interactive charts. it all depends on your data and how you want to present them.
  • How to create an image table of contents in Word?How to create an image table of contents in Word?
    in reports, dissertations, graduation topics; the writer needs to create a table of contents and link to the text page. in this article, dexterity software will guide you how to create a table of contents for images in word.
  • CREATE TABLE command in SQL ServerCREATE TABLE command in SQL Server
    in sql server (transact-sql), the create table statement is used to create and define tables.
  • Table in HTMLTable in HTML
    what does it take to create a table in html? is it complicated? want to add color to the table border, how to add the background color to the text in the table? in this article tipsmake.com will answer those questions and guide you to basic operations with tables on html, in addition to adding alternate color schemes for rows in the html table. invite you to follow along.
  • How to install MySQL Workbench Community Edition on Windows 10How to install MySQL Workbench Community Edition on Windows 10
    detailed instructions on how to install mysql workbench community edition on windows 10 to manage operations with mysql database management system more easily ..
  • How to create cell spacing in a Word tableHow to create cell spacing in a Word table
    adding space between cells in a table will make the table easier to see and not stick together. the following article will guide you on how to create space between cells in a word table.