Creating First Table
Q1. What is a table in SQL? Explain with an example.
A table in SQL is a structured collection of data organized into rows and columns. It is the place where actual data is stored inside a database. Each table is designed to store information about a specific entity such as students, employees, products, or orders.
In a table:
Tables make data easy to store, retrieve, update, and analyze, which is why they are the core component of any relational database.
In a table:
- Rows (records) represent individual entries
- Columns (fields) represent attributes of those entries
Tables make data easy to store, retrieve, update, and analyze, which is why they are the core component of any relational database.
Q2. Why is the USE command important before creating a table?
The USE command is important because it selects the database in which the table will be created. SQL servers can have multiple databases, and without selecting one, SQL does not know where to place the table.
When you run:
you are telling MySQL:
If you skip this step:
When you run:
USE SchoolDB;you are telling MySQL:
All my upcoming operations like creating tables or inserting data should happen inside SchoolDB.
If you skip this step:
- Table creation may fail
- Tables might be created in the wrong database
- SQL may throw an error saying no database selected
Q3. Explain the CREATE TABLE command with syntax.
The CREATE TABLE command is used to create a new table inside a selected database. While creating a table, you must define:
Syntax:
Example:
This command creates a table named Students with four columns. Each column stores a specific type of data, ensuring data consistency and structure.
- Table name
- Column names
- Data types for each column
Syntax:
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype);Example:
CREATE TABLE Students ( StudentID INT, Name VARCHAR(50), Age INT, City VARCHAR(50));This command creates a table named Students with four columns. Each column stores a specific type of data, ensuring data consistency and structure.
Q4. What is the purpose of SHOW TABLES and DESCRIBE commands?
These commands help developers verify whether tables are created correctly and understand the table schema before inserting or querying data.
- SHOW TABLES is used to display all tables present in the currently selected database.
- DESCRIBE table_name is used to view the structure of a table, including column names, data types, null constraints, and default values.
These commands help developers verify whether tables are created correctly and understand the table schema before inserting or querying data.