This document describes how to manage tables.
Overview
A BigQuery table is a standard, two-dimensional table with individual records organized in rows, and a data type assigned to each column (also called a field). Individual fields within a record may contain nested and repeated children fields.
Every table is described by a schema that describes field names, types, and other information. If you need to change the schema later on, you can update the schema. You can specify the schema of a table during the initial table creation request, or you can create a table without a schema and declare the schema in the query or load job that first populates the table.
For more information about allowed types and data formats, see preparing data for BigQuery.
List tables in a dataset by calling bigquery.tables.list. You must have access to the specified dataset to list its tables.
Example
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Creating a table
BigQuery offers various ways to create a new table:
- You can load a table from a CSV or JSON data file (compressed or uncompressed), from an Avro file, or from a Cloud Datastore backup.
- You can create a table from a query result.
- You can copy a table
- You can define a table over a file in Cloud Storage
- You can create an empty table by using the command line tool's
bq mkcommand or by using the BigQuery APItables.insert()method.
All tables defined over data in managed BigQuery storage support appending data to the table.
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Exporting a table
You can export a table in CSV, JSON or Avro format, into a Cloud Storage bucket. For more information, see exporting data from BigQuery.
Appending data
You can load additional data into a table either from source files or by appending query results. Note that the schema of the loaded data must match the schema of the existing table, but you can update the schema before appending.
To append data from a source file:
- Create additional source files
- Upload the files, using either code or the web UI, and
- Load them into to your existing table by referencing
the existing table. In code, set
writeDisposition=WRITE_APPEND.
To append data from a query result:
- Run an asynchronous query, pass in the name
of your existing table, and set
writeDisposition=WRITE_APPEND.
Updating table schema
Once your table is created, you can update the schema by calling the tables.update
or tables.patch functions. For example, using the BigQuery command-line tool:
bq update -t <schema>.
Allowed operations include:
- Adding
NULLABLEorREPEATEDcolumns at the end - Making
REQUIREDfieldsNULLABLE
Browsing through table data
Browse through a table's data by calling
bigquery.tabledata.list,
specifying the name of the table and an optional row offset. This method lets
you specify the maximum number of rows to return per page. If you request a row index beyond the
last row, the method will return successfully, but without a rows property.
Values are returned wrapped in a JSON object that you must parse, as described in the reference documentation.
Tip: You can find out how many rows are in
your table either by querying for one page of results and examining the totalRows
property, or by running the query
SELECT COUNT(*) from myProject:mydataset.myTable
Browsing Through Table Data Examples
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
The Google Cloud Client Library for Go automatically paginates by default, so you do not need to implement pagination yourself, for example:
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
The Google Cloud Client Library for Node.js automatically paginates by default, so you do not need to implement pagination yourself, for example:
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Pagination happens automatically in the Google Cloud Client Library for PHP
using the generator function rows, which fetches the next page of results during iteration.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Pagination happens automatically in the Google Cloud Client Library for Ruby
using Table#data and Data#next.
Modifying or deleting rows or data
BigQuery tables are append-only. The query language does not currently support either updating or deleting data. In order to update or delete data, you must delete the table, then recreate the table with new data. Alternatively, you could write a query that modifies the data and specify a new results table.
Deleting a table
When you delete a table, the table and all its data are removed immediately.
Example
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Undeleting a Table
It's possible to undelete a table within two days of table deletion by using table decorators and the copy command. Undeletions are performed on a best-effort basis and are not guaranteed. Tables from deleted datasets and deleted tables replaced with a new table of the same name cannot be undeleted.
To undelete a table, determine a UNIX timestamp of when the table existed (in milliseconds). Then, copy the table at that time to a new table. The new table must have a different name than the deleted table.
For example, the following statement copies mydataset.mytable at the time
1418864998000 into a new table mydataset.newtable:
bq cp mydataset.mytable@1418864998000 mydataset.newtable
Copying an existing table
There are three different ways you can copy a table: using the web UI, the command-line tool, or programmatically through the API:
To copy a table using the API:
You can copy an existing table through the API by calling the
bigquery.jobs.insert method, and configuring
a copy job. You must specify the following in your job configuration:
"copy": {
"sourceTable": { // Required
"projectId": string, // Required
"datasetId": string, // Required
"tableId": string // Required
},
"destinationTable": { // Required
"projectId": string, // Required
"datasetId": string, // Required
"tableId": string // Required
},
"createDisposition": string, // Optional
"writeDisposition": string, // Optional
},
where sourceTable provides information about the table to be copied,
destinationTable provides information about the new table,
createDisposition specifies whether to create the table if it doesn't exist, and
writeDisposition specifies whether to overwrite or append to an existing table.
Example
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.