SQL CREATE SCHEMA
Makes a named container for tables, views and the rest of a database. PostgreSQL and SQL Server scope it inside one database; MySQL's schema is the database; Oracle's is a user.
- Catégorie
- Tables
- In this category
- 13 of 16
Syntaxe
CREATE SCHEMA [IF NOT EXISTS] name [AUTHORIZATION role] -- PostgreSQL: a namespace inside the current database
CREATE SCHEMA name -- SQL Server: must be the only statement in its batch
CREATE {DATABASE | SCHEMA} name -- MySQL: two spellings of one statement
CREATE USER name IDENTIFIED BY password -- Oracle: creating the user creates the schema
ATTACH DATABASE 'file.db' AS name -- SQLite: named attached files instead of schemas
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Oui | can also create tables, views and grants in the same statement, but only as a standalone batch |
| PostgreSQL | Oui | IF NOT EXISTS and AUTHORIZATION role; search_path decides which schema receives unqualified CREATE TABLE |
| MySQL | Oui | CREATE SCHEMA is a synonym for CREATE DATABASE, so the smallest container is the database |
| SQLite | Different syntax | no schemas: ATTACH DATABASE names extra files on one connection, alongside the defaults main and temp |
| Oracle | Different syntax | a schema never exists without its user; the CREATE SCHEMA statement only creates objects inside your own schema |
Exemples
PostgreSQL
CREATE SCHEMA IF NOT EXISTS billing; CREATE TABLE billing.invoices (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);
SQL Server
-- own batch, or the parse fails GO CREATE SCHEMA billing; GO
MySQL
-- synonym for CREATE DATABASE CREATE DATABASE billing CHARACTER SET utf8mb4;
Oracle
-- the user is the schema CREATE USER billing IDENTIFIED BY Str0ngPass QUOTA 1G ON users;
SQLite
-- named databases per connection, not schemas ATTACH DATABASE 'billing.db' AS billing; CREATE TABLE billing.invoices (id INTEGER PRIMARY KEY, total_cents INTEGER NOT NULL);
Bon à savoir
- SQL Server runs CREATE SCHEMA only as the first statement of a batch: put GO on the line before it in SSMS, and give it its own batch inside stored procedures. Anything earlier in the same batch fails at parse time.
- MySQL has no namespace below the database: what the other engines call a schema is MySQL's whole database. Migration scripts that create one schema per tenant end up creating one database per tenant, with all the connection-string changes that follow.
- Oracle's schema needs a user with a password and, to hold objects, a quota on a tablespace. There is no statement that conjures an empty container first; users are containers there, and dropping the user drops everything in it.
- PostgreSQL's default search_path ($user, public) decides where an unqualified CREATE TABLE lands: a fresh schema is invisible to unqualified queries until you add it to search_path or write schema.table explicitly.
Questions about CREATE SCHEMA
What is the difference between a schema and a database?
PostgreSQL and SQL Server: schemas are namespaces inside one database, many per database. MySQL: schema is a documented synonym for database, one and the same. Oracle: a schema is what a user owns, created and destroyed with the user. SQLite: neither, you attach files and prefix table names with the attach name.
Why does SQL Server reject CREATE SCHEMA in my script?
It must open its batch. Any statement before it in the same batch makes the parser reject the whole thing; separate with GO or run it alone. Inside a stored procedure, run it through its own dynamic batch (EXEC).
How does SQLite cope without schemas?
Each connection gets main and temp plus attached files: ATTACH DATABASE 'billing.db' AS billing, with the name then working as a prefix (billing.invoices). The default limit is 10 attached databases, set by SQLITE_MAX_ATTACHED and raiseable to 125 at build time.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.