Creating Databases and Tables
# CHAPTER 4
Creating Databases and Tables
1. Introduction
We have established our local server and learned the theory of relational tables. Now, the real work begins. To instruct the MySQL engine to build our architecture, we use Data Definition Language (DDL)—a subset of SQL used specifically to define database structures. In this chapter, we will write raw SQL code to create our first database, navigate into it, and construct a professionalusers table from scratch.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use the
CREATE DATABASEcommand.
-
Select an active database using the
USEcommand.
-
Understand the syntax of the
CREATE TABLEcommand.
- Define basic columns and their constraints.
- Follow professional SQL naming conventions.
3. Creating and Selecting a Database
Before creating tables, you need a "container" to hold them.Open your MySQL Command Line (mysql -u root -p) or use the SQL tab in phpMyAdmin and execute the following:
*Note: If you forget the USE command, MySQL will throw an error when you try to create a table, because it won't know which database to put it in!*
4. Creating a Table
Creating a table requires defining its exact structure. You must specify the table name, the column names, and exactly what Data Type each column is allowed to hold (e.g., Numbers, Text, Dates).Let's create a table to hold our students.
Breaking down the syntax:
-
CREATE TABLE students: The command telling MySQL to build a table named 'students'.
-
(...): The parentheses encapsulate all the column definitions.
-
studentid INT: A column named 'studentid' that only accepts Integers (whole numbers).
-
firstname VARCHAR(50): A column named 'firstname' that accepts Variable Characters (text/strings) up to a maximum of 50 characters long.
-
enrollment_date DATE: A column that strictly accepts valid calendar dates (YYYY-MM-DD).
5. Verifying the Table Structure
Once the table is created, how do we know it worked? We can ask MySQL to describe the structure back to us.*(The DESCRIBE command will print a neat textual representation of your columns and data types!)*
6. Dropping (Deleting) Databases and Tables
If you make a massive mistake and want to start completely over, you can use theDROP command.
WARNING: DROP is permanent. There is no undo button. It instantly deletes the table and all data inside it.
7. Mini Project: Build an Inventory Table
Let's build a slightly more advanced table for a store inventory system. We will add a few basic rules.8. Common Mistakes
-
Forgetting Commas: Inside the
CREATE TABLEparentheses, every column definition MUST be separated by a comma. But, critically, the *very last* column definition must NOT have a comma after it.
-
Spaces in Names: Never put spaces in database names, table names, or column names.
first nameis invalid and will crash. Use underscores:firstname.
-
Reserved Keywords: Do not name your tables or columns after SQL commands. For example, creating a column named
selectortablewill deeply confuse the MySQL engine.
9. Best Practices
-
Plural Table Names: It is an industry standard to name databases in the singular, but tables in the plural. A database holds many tables, but a
userstable holds many users. (e.g.,CREATE TABLE users;,CREATE TABLE orders;).
-
Lowercase Everything: While SQL commands (like
CREATE TABLE) are often written in UPPERCASE for readability, you should always name your actual databases, tables, and columns in lowercase (firstname,products).
10. Exercises
-
1.
Write the SQL command to completely delete a database named
oldtestdb.
-
2.
Write the SQL command to create a table named
bookswith three columns:bookid(Integer),title(VARCHAR 150), andpublishyear(Integer).
11. MCQ Quiz with Answers
What is the purpose of the USE command in MySQL?
When defining a table structure using CREATE TABLE, what information MUST you provide for every single column?
12. Interview Questions
- Q: Explain the difference between Data Definition Language (DDL) and Data Manipulation Language (DML) in SQL. Give an example of a DDL command.
- Q: Why is it considered a terrible practice to use spaces or SQL reserved keywords when naming database columns?
13. FAQs
Q: I made a typo in a column name when creating the table. Do I have to DROP the whole table and start over? A: No! You can modify an existing table structure without deleting it by using theALTER TABLE command (e.g., ALTER TABLE students RENAME COLUMN fname TO firstname;).
14. Summary
You are now an architect. By mastering theCREATE DATABASE, USE, and CREATE TABLE commands, you possess the ability to construct the foundational blueprint of any software application. However, a table is only as strong as the data types that define its columns.
15. Next Chapter Recommendation
In our table examples, we usedINT, VARCHAR(50), and DATE. What do these mean? How much text can a VARCHAR hold? In Chapter 5: MySQL Data Types Explained, we will dive deep into the specific constraints we place on our columns to guarantee data integrity.