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.
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.
Hereβs a simple structure of a 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.
To create a schema in SQL, you can use the following command:
CREATE SCHEMA school_db;
This creates a new schema called school_db.
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.
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!)
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!