SQL Tutorial



SCHEMA IN SQL


πŸ“‚ Understanding Schemas in SQL

In SQL, a schema is a collection of database objects, such as tables, views, indexes, and more. A schema helps you organize and manage your data efficiently.

πŸ“‘ What is a Schema?

A schema is like a blueprint for your database, organizing various objects into a logical group. It provides a way to define the structure of the database without storing data directly in the schema.

Schemas allow users to manage access control and ensure that only authorized users can access specific parts of the database.

πŸ“Š Schema Structure

Here’s a simple structure of a schema:

  • Tables - Store data in rows and columns.
  • Views - Virtual tables that present data from one or more tables.
  • Indexes - Help speed up data retrieval.
  • Functions - Store prewritten SQL code that you can reuse.
  • Stored Procedures - Predefined SQL commands that can be executed with a single call.

πŸ“‹ Example: School Database Schema

Table Columns Purpose
Students student_id, name, age, grade Stores information about students
Teachers teacher_id, name, subject Stores information about teachers
Courses course_id, course_name Stores information about courses

In this schema, we have three tables: Students, Teachers, and Courses, each with its own set of columns that define the data stored in them.

πŸ’» SQL Query to Create a Schema

To create a schema in SQL, you can use the following command:


CREATE SCHEMA school_db;
    

This creates a new schema called school_db.

πŸ—οΈ SQL Query to Create a Table in Schema

To create a table inside the school_db schema, use this SQL command:


CREATE TABLE school_db.Students (
  student_id INT PRIMARY KEY,
  name VARCHAR(100),
  age INT,
  grade CHAR(1)
);
    

This command creates the Students table inside the school_db schema.

🎯 Try It Yourself!

Can you write an SQL query to create a table inside a schema named company_db with the columns employee_id, employee_name, and salary?

(Try this in your SQL environment and see the result!)


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review