Adding New Records to Unity Catalog Tables with INSERT

POST_START
# Adding New Records to Unity Catalog Tables with INSERT

In this lesson, we'll learn how to add new records to tables in Unity Catalog using the `INSERT INTO` statement. This operation is essential for populating data in your tables and is a fundamental part of working with Unity Catalog in Databricks.

## Understanding the INSERT Statement

The `INSERT INTO` statement is used to add new rows of data to a table in Unity Catalog. The basic syntax is:

“`sql
INSERT INTO <table_name> VALUES (value1, value2, …);
“`

Where:
– `<table_name>` is the name of the table you want to insert data into.
– `VALUES (value1, value2, …)` specifies the data you want to insert into the table.

Each value corresponds to a column in the table, and the order must match the column order in the table schema.

## Inserting a New Customer Record

Let's start by inserting a new customer record into the `training.sales.customers` table.

### Step 1: Insert a New Customer

Run the following SQL command to insert a new customer record:

“`sql
INSERT INTO training.sales.customers VALUES (1, 'John', 'Germany', 'john@example.com', current_timestamp());
“`

This command inserts a new customer with the following details:
– Customer ID: 1
– Name: John
– Country: Germany
– Email: john@example.com
– Timestamp: The current timestamp (when the record was inserted)

> **Note:** The `current_timestamp()` function ensures that each insert has a unique timestamp, which is useful for tracking when records were added.

## Inserting Another Customer Record

Now, let's insert another customer record into the same table. Run the following SQL command:

“`sql
INSERT INTO training.sales.customers VALUES (2, 'Anna', 'France', 'anna@example.com', current_timestamp());
“`

This command inserts a new customer with the following details:
– Customer ID: 2
– Name: Anna
– Country: France
– Email: anna@example.com
– Timestamp: The current timestamp

## Verifying the Inserted Records

After inserting records, it's a good practice to verify that they were successfully added to the table. You can do this by querying the table.

### Step 2: Select All Customers

Run the following SQL command to retrieve all records from the `training.sales.customers` table:

“`sql
SELECT * FROM training.sales.customers;
“`

This command will return the two records we inserted, along with any existing data in the table.

## Summary

In this tutorial, we've learned how to:
1. Insert new records into a Unity Catalog table using the `INSERT INTO` statement.
2. Use the `current_timestamp()` function to automatically record the time of insertion.
3. Verify the inserted records by querying the table.

These skills are essential for managing and maintaining data in Unity Catalog. In future lessons, we'll explore more advanced operations like updating and deleting records.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies and similar technologies to enhance your experience on wobizdu.com, analyze site traffic, personalize content, and deliver relevant ads. Some cookies are essential for the site to function, while others help us improve performance and user experience. You may accept all cookies, decline optional ones, or customize your settings. Review our Privacy Policy to learn more.