Creating Databases and Tables
# CHAPTER 4
Creating Databases and Tables
1. Introduction
You have your local server running, and you understand how tables work conceptually. Now, we must translate those concepts into reality. In SQL, commands that define or modify the structural blueprint of the database are called DDL (Data Definition Language). In this chapter, we will write our first raw SQL commands:CREATE DATABASE and CREATE TABLE, laying the concrete foundation for our applications.
2. Learning Objectives
By the end of this chapter, you will be able to:- Differentiate between DDL and DML in SQL.
-
Write a
CREATE DATABASEstatement.
-
Select an active database using the
USEcommand.
-
Write a
CREATE TABLEstatement with columns and basic data types.
-
Delete databases and tables using the
DROPcommand.
3. DDL vs DML
SQL commands are categorized based on what they do:-
DDL (Data Definition Language): Commands that build the house. (
CREATE,ALTER,DROP). They define the structure (Tables, Columns).
-
DML (Data Manipulation Language): Commands that put furniture in the house. (
INSERT,SELECT,UPDATE,DELETE). They handle the actual data rows.
*In this chapter, we are exclusively focusing on DDL.*
4. Creating a Database
Open your SQL GUI (phpMyAdmin, pgAdmin, or terminal). To create a new database to hold our tables, we use theCREATE DATABASE command.
*(Note: In SQL, two dashes -- are used to write comments that the engine ignores).*
*(Note 2: Every SQL command MUST end with a semicolon ;)*
5. Selecting the Database (USE)
If you have 10 databases on your server, and you tell the server to "Create a Table", it will throw an error because it doesn't know *which* database to put it in. You must select the database first.
*(Note: If you are using PostgreSQL/pgAdmin, you select the database by clicking it in the visual sidebar rather than using the USE command).*
6. Creating a Table
Now we build our first table. TheCREATE TABLE syntax requires the table name, followed by parentheses () containing a comma-separated list of columns. For each column, you must specify its name and its Data Type (e.g., INT for numbers, VARCHAR for text).
Breaking it down:
-
id INT: A column for the student ID number.
-
firstname VARCHAR(50): A column for text, with a maximum limit of 50 characters.
-
enrollmentdate DATE: A column strictly formatted for calendar dates.
7. Destroying Architecture (DROP)
If you make a mistake and want to completely erase a table (or a whole database) and start over, you use the DROP command.
*Warning: There is no "Undo" or "Recycle Bin" in SQL. Once you execute a DROP command, the data is gone forever.*
8. Mini Project: Student Management Database
Let's build a complete, multi-table schema for a school.*(We have successfully created the structural schema! The tables currently have 0 rows of data in them).*
9. Common Mistakes
-
Forgetting the Semicolon: If you type
CREATE DATABASE test CREATE TABLE users(id INT), SQL will crash. It thinks it is one giant sentence. You must separate commands with a semicolon;.
-
Missing Commas in Table Creation: Inside the
CREATE TABLE ()parentheses, every column definition MUST be separated by a comma, EXCEPT the very last one!
10. Best Practices
-
Snake Case Naming: Avoid spaces and capital letters in table and column names. Use
snakecase.
-
*Bad:*
CREATE TABLE Student Data(Will crash)
-
*Bad:*
CREATE TABLE StudentData(Hard to read)
-
*Good:*
CREATE TABLE studentdata
11. Exercises
- 1. What SQL command is used to select an active database before creating tables?
- 2. What is the highly dangerous SQL command used to permanently delete an entire table and its structure?
12. SQL Challenges
Write the SQL code to create a database namedecommercedb. Switch to that database, and create a table named products with three columns: productid (Integer), product_name (Text up to 100 characters), and price (Integer).
13. MCQ Quiz with Answers
Which of the following SQL commands belongs to the Data Definition Language (DDL) category?
When writing a CREATE TABLE statement, what information must be provided for every single column defined inside the parentheses?
14. Interview Questions
- Q: Explain the difference between DDL (Data Definition Language) and DML (Data Manipulation Language). Provide two examples of commands for each.
-
Q: Why does SQL enforce strict Data Types during the
CREATE TABLEprocess? What architectural advantage does this provide over a standard Excel spreadsheet?
15. FAQs
Q: Can I change a table's structure after I create it? A: Yes! If you forget to add anemail column to the students table, you don't have to DROP it. You can use the DDL command ALTER TABLE students ADD COLUMN email VARCHAR(100); to dynamically modify the structure.
16. Summary
You are officially a database architect. By executing DDL commands likeCREATE DATABASE and CREATE TABLE, you have built the physical containers that will hold your application's data. You also learned the grave responsibility of using the DROP command, which destroys architecture permanently.
17. Next Chapter Recommendation
In our table creation, we used terms likeINT and VARCHAR(50). But what do those actually mean? If we want to store money, decimals, or massive paragraphs of text, what do we use? In Chapter 5: SQL Data Types Explained, we will dive deep into how SQL precisely categorizes data.