What are tables in SQL

What are tables in SQL

12-Mar-2025
| |
Image Carousel

Table of Contents

S.no Contents-topics
1 What Are Tables in SQL
2 Creating a Table
3 Inserting Data into a Table
4 Querying Data from a Table
5 Modifying a Table Structure

1:What Are Tables in SQL

Tables in SQL are the core structure for storing data in a relational database. Think of them as spreadsheets with rows and columns, where each column has a specific data type (e.g., INT, VARCHAR) and each row represents a record. For example, a table named users might store names and ages, like "John, 25". Tables organize data efficiently, allowing you to query, update, or delete it using SQL commands.

2:Creating a Table

To create a table in SQL, you use the CREATE TABLE command, defining columns and their data types. Let’s create a users table as an example:
Copy this SQL command:Copy

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    age INT
);

Run this in a database tool like MySQL Workbench or phpMyAdmin to create the table. Here, id auto-increments, name holds up to 50 characters, and age stores integers.

3:Inserting Data into a Table

To add data, use the INSERT INTO command. Let’s add a user to the users table:
Copy this SQL command:Copy

INSERT INTO users (name, age) VALUES
('John Doe', 25);

Execute this to insert a record. The id auto-increments (e.g., 1), and 'John Doe', 25 is added as a row.

4:Querying Data from a Table

To retrieve data, use the SELECT command. Here’s how to fetch all users:
Copy this SQL query:Copy

SELECT * FROM users;

Run this to see all rows, like id: 1, name: 'John Doe', age: 25. Use conditions (e.g., WHERE age > 20) for specific data.

5:Modifying a Table Structure

To change a table, use ALTER TABLE. Let’s add an email column to users:
Copy this SQL command:Copy

ALTER TABLE users
ADD email VARCHAR(100);

Execute this to add the email column. Now, users can store emails up to 100 characters, enhancing its structure.

Tags: SQL tables, database tables, relational tables, SQL table structure, SQL table creation, SQL table schema, SQL table relationships, SQL table types, primary key, foreign key, SQL constraints, SQL normalization, SQL indexes, SQL joins, SQL table operations, create table SQL, alter table SQL, drop table SQL, truncate table SQL, SQL data types, SQL table design,,sql , what is sql , how to install sql,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter