Unlike its peers, BigQuery is serverless and hence provides services dynamically, which means that no provisioning or hardware management is required from the user.
This blog will introduce you to Bigquery and will explain the BigQuery INSERT and UPDATE command in detail. These are SQL queries that can help you in managing your data in Google BigQuery.
BigQuery INSERT and UPDATE
1) BigQuery INSERT and UPDATE: INSERT Command
- Out of the BigQuery INSERT and UPDATE commands, you must first learn the basic INSERT statement constructs to interact with the above table definitions.
- INSERT query follows the standard SQL syntax. The values that are being inserted should be used in the same order as the columns. The below image shows an example of INSERT command.
- You can execute a basic INSERT query with columns specified as given in the INSERT INTO example below.
INSERT INTO dataset.StockDetails(name, price) VALUES(‘Samsung Galaxy Note 10,’999’’)
An INSERT query without specifying columns can be executed as below.
INSERT INTO dataset.StockDetails VALUES(‘SG Note 10’,’Mystic Black’,’999’,’24’)
An INSERT query using a subquery can be executed as follows.
INSERT INTO dataset.stockDetails(product, color) VALUES(‘SG Note 10’, SELECT color from dataset.StockDetails WHERE product=’Samsung Galaxy Note 20’)
- This query adds a new product, “SG Note 10”, to the
stockDetails
table.
- Instead of specifying the color, it copies the color of “Samsung Galaxy Note 20”.
- The result is “SG Note 10” with the same color as “Samsung Galaxy Note 20” added to the table.
BigQuery also supports the WITH keyword in the QUERY syntax.
- A point to note here is the implicit data typing mapping that is executed by BigQuery in the case of INSERT statements.
- This can lead to corrupted Databases in case the user is not careful.
- BigQuery will raise an error for data type mismatch only in cases where the incoming column cannot be converted to the target column data type.
Pro Tip: With just a few clicks, connect any data source to Google BigQuery and watch your data load in real-time.
Integrate MongoDB to BigQuery
Integrate Oracle to BigQuery
Integrate PostgreSQL to BigQuery
Integrate Salesforce to BigQuery
2) BigQuery INSERT and UPDATE: UPDATE Command
- In the case of BigQuery, each UPDATE statement must include a WHERE clause.
- This offers protection against the bulk erroneous updates if the developer accidentally forgets the WHERE clause.
- To update the complete table, you have to specifically mention WHERE TRUE.
- The most basic UPDATE example will look like below.
UPDATE dataset.StockDetails SET color = ‘Mystic Green’ WHERE product=’SG Note 20 Ultra’
To update records by using a string match operation, the query will be as follows.
UPDATE dataset.StockDetails SET color = ‘Mystic Green’ WHERE product LIKE ‘%Note%’
The above statement will update the color of all records where the product contains ‘Note’ in its name.
An Update statement using a JOIN clause can be executed as below.
UPDATE dataset.StockDetails a SET color = ‘black’ FROM dataset.Stock b WHERE a.product = b.product AND b.outlet = ‘central_park’
- This query sets the
color
to “black” in the StockDetails
table.
- It matches products in
StockDetails
(a
) with products in the Stock
table (b
).
- The update applies where the product matches and the outlet is “central_park.”
- The color of matching products in the “central_park” outlet is updated to black.
The above query updates the color of all the records that belong to a particular store.
An explicit JOIN keyword has to be used in case the JOIN is between tables that are not getting updated. An example will be as follows.
UPDATE dataset.StockDetails SET quantity=’10’ FROM dataset.Stock INNER JOIN dataset.Store on Stock.outlet = Outlet. outlet_name WHERE StockDetails.product = Stock.product and Outlet.location = ‘New York’
- This query sets the
quantity
to 10 in the StockDetails
table.
- It uses data from the
Stock
and Store
tables, connecting them based on the outlet name.
- The update happens where the product in
StockDetails
matches the product in Stock
, and the store is located in New York.
- The quantity for matching products in New York stores is updated to 10.
The above command will set the quantity as 10 for all items in all outlets belonging to the location ‘New York’.
- That covers the basics of INSERT and UPDATE statements in BigQuery.
- While implementing an ETL pipeline to synchronize the data between your transactional databases and data warehouse, an automated scheduled script using the above constructs can be used.
- Implementing such a script has a lot more additional complexities because of the existence of many factors like duplicate rows, data type translations, etc.
But there is an alternative.
- It is to use a completely managed ETL tool like Hevo that can accomplish data transfer between various sources and destinations easily.
- That’s it! You can now try to implement the BigQuery INSERT and UPDATE commands by yourself.
Learn More About:
How to Set Up BigQuery JDBC Connection
Conclusion
- This article teaches you how to work with Google BigQuery INSERT and UPDATE statements with ease.
- It also provides in-depth knowledge about the concepts behind every step to help you understand and implement them efficiently.
- Integrating complex data from a diverse set of data sources can be a challenging task and this is where Hevo saves the day!
- Learn how to transfer data from Mixpanel to BigQuery to enhance your data analysis. Our guide provides clear steps for effective integration.
FAQ on BigQuery INSERT and UPDATE Commands
How to update data in a BigQuery table?
BigQuery does not support traditional UPDATE operations directly on tables. Instead, you typically perform updates using MERGE statements or by overwriting data with new values.
How do I INSERT a new column in a table in BigQuery?
In BigQuery you cannot directly add a new column instead you can use ALTER TABLE query to create a new table with the desired schema and remove the original table.
How to use into in BigQuery?
The SELECT INTO statement is used to select data from one or multiple tables and insert into a new table.
How to INSERT data into table in Oracle?
To insert a table in Oracle you use the INSERT statement.
How do I INSERT data into a BigQuery table in Excel?
To insert data into a BigQuery table from Excel you can follow these steps:
Export excel data to CSV
Upload CSV to drive
Create or open google sheets
Import CSV into google sheets
Copy data to BigQuery
With over a decade of experience, Sarad has been instrumental in designing and developing Hevo's fundamental components. His expertise lies in building lean solutions for various software challenges. Sarad is passionate about mentoring fellow engineers and continually exploring new technologies to stay at the forefront of the industry. His dedication and innovative approach have made significant contributions to Hevo's success.