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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Character Data Types: CHAR vs. VARCHAR →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
Is a schema the same as a database?
What is the dbo schema in SQL Server?
How do you query a table in another database?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →