SCBL004
Overview
In this lab, you create a database with multiple tables, and use both primary-foreign key contraints and interleaved tables to manage relationships.
Objectives
In this lab, you learn how to:
- Create a relational database with proper primary keys and relationships optimized for Spanner.
Setup and Requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
- Time to complete the lab. Remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell
at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:
Your Cloud Platform project in this session is set to YOUR_PROJECT_ID
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
gcloud auth list
-
Click Authorize.
-
Your output should now look like this:
Output:
ACTIVE: *
ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net
To set the active account, run:
$ gcloud config set account `ACCOUNT`
- (Optional) You can list the project ID with this command:
gcloud config list project
Output:
[core]
project = <project_ID>
Example output:
[core]
project = qwiklabs-gcp-44776a13dea667a6
Note: For full documentation of gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Creating an Orders database
-
In previous labs, you have learned how to create Spanner instances, databases, and tables using the Google Cloud Console, the gcloud CLI, and Terraform. Using the method of your choice, create an instance in the region and a database meeting the following criteria.
-
Note: The objective of this lab is for you to create the database and tables without detailed instructions, please see previous labs if you get stuck.
Database name: orders-db
Tables: Customers, Orders, OrderDetails
Customers table
Fields |
Data Type |
CustomerID |
UUID |
CompanyName |
STRING |
Name |
STRING |
Region |
STRING |
Address |
STRING |
Orders table
Fields |
Data Type |
OrderID |
UUID |
OrderDate |
DATE |
CustomerID |
STRING |
OrderDetails table
Fields |
Data Type |
OrderID |
UUID |
Product |
UUID |
Qty |
INT64 |
Price |
FLOAT64 |
Primary keys
Ensure you create primary keys for each table.
Set default values for the primary keys using auto-generated UUIDs.
Relationships
Customers have 0 or more Orders
- Use a primary-foreign key relationship between the Customers and Orders table.
Orders have 1 or more Details
- Use an interleaved table for the relationship between the Orders and OrderDetails table.
Assistance
Below are sample DDL statements for the Pets database you used earlier. These statements can guide you in your database creation.
Owners table
CREATE TABLE Owners (
OwnerID STRING(36) NOT NULL DEFAULT (GENERATE_UUID()),
OwnerName STRING(MAX) NOT NULL
) PRIMARY KEY (OwnerID);
Pets table with primary-foreign keys
CREATE TABLE Pets (
PetID STRING(36) NOT NULL DEFAULT (GENERATE_UUID()),
OwnerID STRING(36) NOT NULL,
PetType STRING(MAX) NOT NULL,
PetName STRING(MAX) NOT NULL,
Breed STRING(MAX) NOT NULL,
CONSTRAINT FK_OwnerPet FOREIGN KEY (OwnerID) REFERENCES Owners (OwnerID),
) PRIMARY KEY (PetID);
Pets table interleaved with Owners
CREATE TABLE Pets (
OwnerID STRING(36) NOT NULL,
PetID STRING(36) NOT NULL DEFAULT (GENERATE_UUID()),
PetType STRING(MAX) NOT NULL,
PetName STRING(MAX) NOT NULL,
Breed STRING(MAX) NOT NULL
) PRIMARY KEY (OwnerID,PetID)
, INTERLEAVE IN PARENT Owners ON DELETE CASCADE
;
Congratulations! You have created a database with multiple tables, and used both primary-foreign key contraints and interleaved tables to manage relationships.
End your lab
When you have completed your lab, click End Lab. Your account and the resources you've used are removed from the lab platform.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
- 1 star = Very dissatisfied
- 2 stars = Dissatisfied
- 3 stars = Neutral
- 4 stars = Satisfied
- 5 stars = Very satisfied
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.