Have you ever wondered how to work with your fellows in one database without interfering with each other? Is it possible to organize the database objects into logical groups which do not collide with the other objects’ names?
A schema addresses these needs:
A schema is a collection of tables. A schema also contains views, indexes, sequences, data types, operators, and functions. Redpanda SQL supports multiple schemas. For example, you can have a database named oxla and have multiple schemas based on your needs, like auth, model, business, etc.
By default, the public schema is used in Redpanda SQL. When unqualified table_name is used, that table_name is equivalent to public.table_name. It also applies to CREATE, DROP, and SELECT TABLE statements.
|
Note
|
Furthermore, you can create multiple schemas per your needs. |
The basic syntax of creating a schema is as follows:
CREATE SCHEMA [IF NOT EXISTS] schema_name;-
schema_nameis the schema name you are going to create. -
IF NOT EXISTSis an optional parameter to avoid errors if the schema already exists.
The syntax to create a table in a specified schema is as follows:
CREATE TABLE schema_name.table_name(
...
);-
schema_nameis the schema that you have created. -
table_nameis the table name you are going to create.
After creating the table and inserting some data, display all rows with the syntax below:
SELECT * FROM schema_name.table_name;-
schema_nameis the name of the schema. -
table_nameis the name of the table you want to display.
Option 1: To drop an empty schema where no objects remain in it, use the command below:
DROP SCHEMA [IF EXISTS] schema_name;-
schema_nameis the schema name you are going to create. -
IF EXISTSis an optional parameter to avoid errors if the schema does not exist.
Option 2: Tables reside in a schema, so it is impossible to drop a schema without also dropping the tables. With the command below, you will also drop the schema with the tables.
DROP SCHEMA schema_name CASCADE;-
First, connect to Redpanda SQL and create a schema as shown below:
CREATE SCHEMA oxlarefs;
-
Next, create a table in the above schema with the following details:
CREATE TABLE oxlarefs.functions( id int, function_name text, active bool ); INSERT INTO oxlarefs.functions(id, function_name, active) VALUES ('1111', 'Numeric', 'TRUE'), ('2222', 'Text', 'TRUE'), ('3333', 'Timestamp', 'TRUE'), ('4444', 'JSON', 'TRUE'), ('5555', 'Boolean', 'TRUE');
-
You can verify and show the table made with the command below:
SELECT * FROM oxlarefs.functions;
-
You will get the following result:
+------+---------------+---------+ | id | function_name | active | +------+---------------+---------+ | 1111 | Numeric | t | | 2222 | Text | t | | 3333 | Timestamp | t | | 4444 | JSON | t | | 5555 | Boolean | t | +------+---------------+---------+
To avoid errors when the schema already exists, use the IF NOT EXISTS option. Here is how it works:
-
First, create the schema without using the
IF NOT EXISTSoption.CREATE SCHEMA oxladb;
Output:
CREATE SCHEMA
-
If you attempt to create the schema again without using
IF NOT EXISTS, it will result in an error.CREATE SCHEMA oxladb;
Output:
ERROR: Schema: oxladb already exists
Use the command below to delete the schema and also the tables in it.
DROP SCHEMA oxlarefs CASCADE;Another case is if there is no table or object created inside the schema, you can use the following command to drop the schema.
DROP SCHEMA oxlarefs;-
First, drop the schema without using the
IF EXISTSoption.DROP SCHEMA oxladb;
Output:
DROP
-
If you attempt to drop the schema again without using
IF EXISTS, it will result in an error.DROP SCHEMA oxladb;
Output:
ERROR: schema "oxladb" does not exist