Schemas in SQL: Definitions and Design Fundamentals, Explained

UPI Study › Courses › Database Programming › Free lessons › Lesson 3.3 Free with a free account

Schemas in SQL: Definitions and Design Fundamentals

Database Programming · Chapter 3: Data Definition Language and Schema DesignSection 3.3 · lesson 17 of 768 min · free with a free account

← A Complete Guide to DDL CommandsNext lesson: Character Data Types: CHAR vs. VARCHAR →

A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

In this lesson

How MySQL and SQL Server use schemas to organize and address objects.

  • Define a schema as a namespace for related database objects
  • Contrast MySQL, where schema means database, with SQL Server's nested schemas
  • Write qualified two-part and three-part object names
  • Organize schemas by purpose and keep application tables out of system schemas

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (912 words). Read along, or search it.

Opening Problem

Two Teams Want a Table Named Orders

Amina Diallo, database administrator at Lantern Books, receives two requests for a table named orders. One team handles current sales; another keeps archived records. Must one team give up the name? A namespace lets an object have a short name within a clearly named home. The challenge is that MySQL and SQL Server use the word schema differently. Each names a different container level.

Definitions

Schemas as Named Containers

A schema is a named container for related database objects. A namespace separates identical short names, avoiding a name clash between unrelated tables. In MySQL, schema and database mean the same thing. A database called shop contains its customer and order tables. In SQL Server, a database contains schemas, and each schema contains objects. The shop database could contain sales and archive schemas. Those arrangements organize objects differently. A dotted name can therefore point to different container levels, depending on the product.

MySQL

Schema Equals Database in MySQL

This MySQL listing creates a database using CREATE SCHEMA. CREATE DATABASE would create the same kind of object. USE selects that database for the current session. An unqualified table name has no container prefix, so it depends on this session context. SELECT DATABASE reports the current database; before one is selected, it returns null. SHOW SCHEMAS lists databases. The guard avoids an already-exists error, but does not change an existing schema's design. Creating tables still needs a deliberately chosen destination. These commands are a non-run MySQL listing. SQLite opens files or in-memory databases through its own shell interface.

SQL Server

Schemas Inside a SQL Server Database

SQL Server first chooses a database, then creates a schema within it. Here, sales is a custom schema: one created for the application. CREATE SCHEMA must execute as a separate batch. In tools such as SQL Server Management Studio, GO separates that batch from surrounding commands. The table name sales dot notes explicitly places notes in sales. USE selects a database; it does not select a schema. SQL Server includes dbo, a common default schema. A user's configured default can differ, so an unqualified creation need not land in dbo. Amina checks the configured context rather than assuming the last person used the same default.

Qualified Names

Two-Part and Three-Part Addresses

A qualified name includes a container prefix. In MySQL, shop dot customers identifies customers in the shop database. In SQL Server, sales dot orders means orders in the sales schema of the current database. Add the database for a three-part SQL Server address: shop dot sales dot orders. That fixes the database as well as the schema. A two-part SQL Server name still depends on the current database. Qualification reduces ambiguity only for the parts you actually specify. Unqualified names leave those choices to resolution rules. A name that worked in one session may fail, or find a different table, in another.

SQLite Demo

Attached Sales and Archive Databases

SQLite can demonstrate the addressing idea with attached databases. This is its own mechanism, rather than SQL Server's nested schema system. Attach creates two independent in-memory databases named sales and archive. Each receives a table called orders. The qualified queries return different order identifiers. These are disposable partitions using two identifiers from our fictional shop baseline. The prefix tells SQLite which attached database to read. It does not move the table, copy its data, or switch the main database. A short name alone can be ambiguous to a reader. Explicit prefixes make the intended source visible before the query runs.

Cross-Database Access

Reaching a Table in Another Database

Suppose Amina is connected to company, while the customer table lives in shop. An unqualified customers reference may look in the wrong place. In MySQL, shop dot customers reaches the intended database without changing the current one. In SQL Server, include the database, schema, and table. Alternatively, select the correct database with USE, then specify the right schema where the product requires it. If nothing is found, inspect your context and the catalog. Do not create a duplicate table merely to silence a missing-object error. The address solves a location problem; it does not grant access. Reading another container still requires the appropriate permissions.

Quick Check

Which Products Table Answers

A fresh SQLite stock-check file attaches an inventory database. Both main and inventory contain a table named products. Read the final query carefully. Which product identifier does its qualified name select? Pause, and decide. Product twelve. The inventory prefix selects the attached inventory database, whose products table contains that identifier. Run the file to confirm. The same short table name in main contains a different row, but the query does not address it. Product eleven comes from ignoring the prefix. Both rows would need a query that reads both tables.

Organizing Schemas

Grouping Objects by Purpose

Group schemas by purpose when the database grows. Sales can hold customer-facing work, while inventory holds stock and supplier records. Human resources can keep staff records together. An archive can organize older data, though archival policy requires more than a container name. Choose short, consistent names, and qualify them in shared scripts. Keep application tables out of built-in system schemas. MySQL reserves areas such as mysql and information schema for system use. SQL Server exposes sys and information schema, with system databases such as master. Amina can keep both order tables, because their full addresses differ. She documents the product, database, schema where applicable, and object name.

Key Takeaways

A Named Home for Every Object

A schema gives objects a named home. It separates identical short names and keeps related work organized. In MySQL, that home is a database. In SQL Server, schemas live inside databases, and a user's default may vary. Read each dotted part according to the product. Specify enough of the address to locate the intended object, then verify your current context.

About Database Programming at UPI Study

Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Schemas in SQL: Definitions and Design Fundamentals

What is a schema in SQL?
A schema is a named container, or namespace, that groups related database objects such as tables and views. What it means in practice depends on the product. In MySQL, a schema is the same thing as a database, while in SQL Server a database holds several schemas, such as dbo and a custom sales schema. Two tables named orders can coexist when their containers differ.
Is a schema the same as a database?
In MySQL, yes: CREATE SCHEMA and CREATE DATABASE create the same kind of object, and SHOW SCHEMAS lists databases. In SQL Server, no: a database contains schemas, and each schema contains objects such as tables. That is why SQL Server can use a three-part name like shop.sales.orders, while a MySQL name like shop.customers needs just the database and the table.
What is the dbo schema in SQL Server?
The dbo schema is a built-in schema found in each SQL Server database, and it is a common default for objects created without a schema prefix. A user's configured default schema can differ, though, so an unqualified CREATE TABLE might not land in dbo. Also note that USE selects a database, not a schema. Qualify names in shared scripts, as in dbo.orders or sales.orders.
How do you query a table in another database?
Qualify the table name with the database that holds it. In MySQL, shop.customers reaches the customers table in the shop database without changing the current database. In SQL Server, use a three-part name such as shop.sales.customers, giving database, schema, and table. Alternatively, switch databases with USE first. A qualified name solves the location problem but does not grant access, so the account still needs permission.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Database Programming.

76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Database Programming
3 credits · NCCRS
Enroll →