Working with Schema Objects in Practice, Explained

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

Working with Schema Objects in Practice

Database Programming · Chapter 9: Subqueries, Views, and IndexesSection 9.9 · lesson 57 of 769 min · free with a free account

← Creating Indexes in SQL: Steps and ExamplesNext lesson: Building and Using Temporary Tables in SQL →

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

Inspect catalogs and use views, sequences and synonyms with care.

  • Inspect schema metadata through catalogs such as INFORMATION_SCHEMA and sqlite_schema
  • Explain how ordinary views behave and when WITH CHECK OPTION rejects a write
  • Compare SQL Server sequences with MySQL AUTO_INCREMENT, and synonyms with views
  • Trace dependencies between schema objects before changing them

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 (1,069 words). Read along, or search it.

Opening Problem

Who Depends on This Report

Leo Park, Lantern Books' developer, wants to rename part of a customer report. Amina Diallo, the database administrator, asks which objects and applications depend on it. A table name is only one part of the schema. How can they inspect what exists, change a view deliberately, and avoid surprising its callers? We will follow the catalog from stored tables to views, number generators, aliases, and dependencies.

Sample Database

Reset Shop Data for Schema Work

Our examples use Lantern Books, a fictional campus bookstore. This disposable database starts with the shared course data. Customers and products hold people and inventory. Prices and quantities are illustrative, and each lesson resets its own copy. Orders link to customers, while order items link orders to products. Foreign key checks are enabled for this connection. Save the setup file, then run it against a fresh shop database. Successful creation prints no rows here. Later commands reopen that database file. These examples use the real SQLite shell, and server dialect differences will be labeled.

Catalog Metadata

Inspecting Tables and Columns

A catalog stores metadata: descriptions of the database's objects and columns. SQLite exposes sqlite_schema and table-info pragmas, which we query here. MySQL and SQL Server offer information schema tables, columns, and views for common inspection tasks. Filter by schema as well as object name when names can repeat. MySQL treats schema as a database, while SQL Server schemas are namespaces inside a database. SQLite's catalog vocabulary differs, even when the inspection question is the same. Run these SQLite inspections. We show three table names and the first three customer-column definitions, keeping the display small. MySQL also offers show and describe commands. SQL Server supplies sys catalogs and help procedures; its sys schema holds different objects from MySQL's performance-oriented sys schema.

Ordinary Views

A Rewards View as a Saved Query

An ordinary view is a named saved select, rather than a stored snapshot. Our rewards view exposes customers meeting an illustrative points threshold. Views can hide a complex join, present a limited slice, and provide a stable column interface. Permissions still determine who may use that interface. Name columns explicitly, give expressions aliases, and sort in the outer query. A view definition does not guarantee the row order a caller receives. Run the definition and read it. In SQL Server, create view must start its batch; the client batch separator go belongs outside the select statement. Asha, Chen, and Elena qualify. SQLite ordinary views require instead-of triggers for writes; these ordinary views hold no independent row copy.

Updatable Views

Writes Through Views and CHECK OPTION

An updatable server view must let the engine trace each change to appropriate base columns. Aggregates, grouping, and distinct results generally prevent direct changes to those results. Computed expressions are not directly writable. Join-view changes have engine-specific limits, so do not assume every simple-looking view accepts every insert, update, or delete. With check option rejects through-view changes that would leave a row outside the view's predicate. It does not police changes made directly to the base table. These are non-run alternatives. Both servers support alter view; MySQL also offers create or replace, while SQL Server supports create or alter from 2016 SP1. Required base columns omitted by a view can make inserts fail without defaults. Schema binding in the SQL Server definition blocks incompatible underlying changes, using explicit two-part object names.

Sequences

Shared Number Generators and Gaps

A sequence is a database-level number generator that can serve several tables. SQL Server's next value for requests a number independently of an insert. That independence means a number can be obtained before a row is stored. A rollback does not put an allocated sequence number back, so gaps are normal. Cycling or restarting can repeat values, and a sequence is not a universal uniqueness constraint. Use appropriate table constraints for the rule your data requires. This server listing is not run in SQLite. MySQL instead commonly uses auto increment tied to a table column, then last insert id to retrieve a generated value. A table-bound counter and a shared sequence solve different allocation needs. Choose by ownership and sharing, rather than expecting either one to produce a gapless business ledger.

Synonyms

Alternative Names for Other Objects

A synonym is an alternative name for another object. SQL Server can point that name at an object in another database, as this listing illustrates. The target is not fully validated at creation, so a missing target can become an error only when used. Creating an alias does not establish that its target works. Synonyms support appropriate data operations and routine execution, but are not a way to redirect every schema command such as alter table. MySQL has no equivalent synonym object. A cross-database view can supply a limited query interface, but it does not reproduce all synonym capabilities. These non-run examples assume appropriately authorized target objects. Catalog inspection should retain the full target name, so an innocent-looking alias does not hide a surprising dependency.

Dependencies

Prerequisites, Dependents and Naming Rules

A dependency means one object needs another: an order foreign key needs a customer key, and a view needs the tables or views it reads. Create prerequisites before dependents, and remove dependents before their prerequisites. Search application queries too; a database catalog cannot list every external caller. Information schema view table usage exposes view references in SQL Server and modern MySQL. SQL Server also offers expression-dependency and foreign-key catalogs; MySQL lists key-column usage. Use explicit column lists, rather than assuming a view defined with select star refreshes safely. SQL Server may require refreshing view metadata after underlying changes. Choose consistent case, singular or plural forms, object prefixes, and key-column names. Avoid spaces and reserved words, and use schema qualifiers where the engine expects them.

Quick Check

Asha Drops Below the View Threshold

A new change lowers Asha's points through the base table. The existing rewards view still filters for at least one hundred points. How many Asha rows does the view return: zero, one, or an error? Pause, and predict. Zero rows are returned for Asha. The base row still exists, but its updated points no longer satisfy the saved view predicate. Run the script, and the count confirms zero. This update targets customers directly, so SQLite's ordinary-view write restriction does not reject it. One confuses a view with a stored snapshot. An update error would need a violated constraint or an unsupported target, neither of which occurs here.

Key Takeaways

Inspect Before Changing Any Object

Leo and Amina can now inspect more than table contents. Catalogs describe objects, and ordinary views expose current query results through a named interface. Sequences allocate numbers, synonyms supply alternative names, and dependencies connect those objects to their prerequisites. Check the engine's capabilities, use explicit names, and test callers whenever a schema object changes.

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 Working with Schema Objects in Practice

How do you list the tables and columns in a database with SQL?
Query the database's catalog. In MySQL, SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'shop' lists one database's tables, and INFORMATION_SCHEMA.COLUMNS describes columns; SQL Server offers the same views plus sys catalog views. Filter by schema as well as name, because names can repeat. SQLite uses sqlite_schema and PRAGMA table_info, and MySQL also provides SHOW and DESCRIBE.
What does WITH CHECK OPTION do on a view?
WITH CHECK OPTION rejects an insert or update made through the view when the resulting row would no longer satisfy the view's WHERE condition. For a view of customers with points >= 100, an update through the view that sets points to 50 is refused. It does not police changes made directly to the base table, which can still move rows out of the view. MySQL and SQL Server support it.
What is the difference between a sequence and AUTO_INCREMENT?
A sequence is a standalone number generator that can serve several tables, while AUTO_INCREMENT is a counter tied to one MySQL table column. In SQL Server, NEXT VALUE FOR order_seq returns a number before any row is inserted. MySQL has no sequence object and retrieves generated values with LAST_INSERT_ID(). Neither guarantees gapless numbers, since a rolled-back allocation is not returned, so enforce uniqueness with constraints.
What is a synonym in SQL Server?
A synonym is an alternative name that points to another object, such as a table in another database: CREATE SYNONYM dbo.Clients FOR SalesDb.dbo.Customers. The target is not checked when the synonym is created, so a missing target fails only when the synonym is used. Synonyms work for data operations and procedure calls but not for schema commands such as ALTER TABLE. MySQL has no synonym object.

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 →