Create table with TIMESTAMP column
Stay organized with collections
Save and categorize content based on your preferences.
Create a table with a TIMESTAMP column.
Code sample
C++
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
voidCreateTableWithTimestamp(
google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,std::stringconst&instance_id,
std::stringconst&database_id){
google::cloud::spanner::Databasedatabase(project_id,instance_id,
database_id);
autometadata=client
.UpdateDatabaseDdl(database.FullName(),{R"""(
CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate Date,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL OPTIONS
(allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE)"""})
.get();
google::cloud::spanner_testing::LogUpdateDatabaseDdl(//! TODO(#4758)
client,database,metadata.status());//! TODO(#4758)
if(!metadata)throwstd::move(metadata).status();
std::cout << "`Performances` table created, new DDL:\n"
<< metadata->DebugString();
}
C#
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
usingGoogle.Cloud.Spanner.Data ;
usingSystem.Threading.Tasks;
publicclassCreateTableWithTimestampColumnAsyncSample
{
publicasyncTaskCreateTableWithTimestampColumnAsync(stringprojectId,stringinstanceId,stringdatabaseId)
{
stringconnectionString=$"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
usingvarconnection=newSpannerConnection (connectionString);
// Define create table statement for table with commit timestamp column.
stringcreateTableStatement=
@"CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate Date,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE";
varcmd=connection.CreateDdlCommand (createTableStatement);
awaitcmd.ExecuteNonQueryAsync();
}
}
Go
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
import(
"context"
"fmt"
"io"
database"cloud.google.com/go/spanner/admin/database/apiv1"
adminpb"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
)
funccreateTableWithTimestamp(wio.Writer,dbstring)error{
ctx:=context.Background()
adminClient,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferadminClient.Close ()
op,err:=adminClient.UpdateDatabaseDdl(ctx,&adminpb.UpdateDatabaseDdlRequest{
Database:db,
Statements:[]string{
`CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate Date,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE`,
},
})
iferr!=nil{
returnerr
}
iferr:=op.Wait(ctx);err!=nil{
returnerr
}
fmt.Fprintf(w,"Created Performances table in database [%s]\n",db)
returnnil
}
Java
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
staticvoidcreateTableWithTimestamp(DatabaseAdminClientdbAdminClient,DatabaseIdid){
OperationFuture<Void,UpdateDatabaseDdlMetadata>op=
dbAdminClient.updateDatabaseDdl(
id.getInstanceId().getInstance(),
id.getDatabase(),
Arrays.asList(
"CREATE TABLE Performances ("
+" SingerId INT64 NOT NULL,"
+" VenueId INT64 NOT NULL,"
+" EventDate Date,"
+" Revenue INT64, "
+" LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)"
+") PRIMARY KEY (SingerId, VenueId, EventDate),"
+" INTERLEAVE IN PARENT Singers ON DELETE CASCADE"),
null);
try{
// Initiate the request which returns an OperationFuture.
op.get();
System.out.println("Created Performances table in database: ["+id+"]");
}catch(ExecutionExceptione){
// If the operation failed during execution, expose the cause.
throw(SpannerException)e.getCause();
}catch(InterruptedExceptione){
// Throw when a thread is waiting, sleeping, or otherwise occupied,
// and the thread is interrupted, either before or during the activity.
throwSpannerExceptionFactory.propagateInterrupt(e);
}
}
Node.js
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// Imports the Google Cloud client library
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// Creates a client
constspanner=newSpanner ({
projectId:projectId,
});
// Gets a reference to a Cloud Spanner Database Admin Client object
constdatabaseAdminClient=spanner.getDatabaseAdminClient ();
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they
// must be converted to strings before being inserted as INT64s
constrequest=[
`CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate DATE,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE`,
];
// Creates a table in an existing database
const[operation]=awaitdatabaseAdminClient.updateDatabaseDdl({
database:databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId,
),
statements:request,
});
console.log(`Waiting for operation on ${databaseId} to complete...`);
awaitoperation .promise();
console.log(`Created table Performances in database ${databaseId}.`);
PHP
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\UpdateDatabaseDdlRequest;
/**
* Creates a table with a commit timestamp column.
* Example:
* ```
* create_table_with_timestamp_column($projectId, $instanceId, $databaseId);
* ```
*
* @param string $projectId The Google Cloud project ID.
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function create_table_with_timestamp_column(string $projectId, string $instanceId, string $databaseId): void
{
$databaseAdminClient = new DatabaseAdminClient();
$databaseName = DatabaseAdminClient::databaseName($projectId, $instanceId, $databaseId);
$statement = 'CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate DATE,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers on DELETE CASCADE';
$request = new UpdateDatabaseDdlRequest([
'database' => $databaseName,
'statements' => [$statement]
]);
$operation = $databaseAdminClient->updateDatabaseDdl($request);
print('Waiting for operation to complete...' . PHP_EOL);
$operation->pollUntilComplete();
printf('Created Performances table in database %s on instance %s' . PHP_EOL,
$databaseId, $instanceId);
}
Python
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
defcreate_table_with_timestamp(instance_id, database_id):
"""Creates a table with a COMMIT_TIMESTAMP column."""
fromgoogle.cloud.spanner_admin_database_v1.typesimport spanner_database_admin
spanner_client = spanner.Client()
database_admin_api = spanner_client.database_admin_api
request = spanner_database_admin.UpdateDatabaseDdlRequest(
database=database_admin_api.database_path(
spanner_client.project, instance_id, database_id
),
statements=[
"""CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate Date,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL
OPTIONS(allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE"""
],
)
operation = database_admin_api.update_database_ddl(request)
print("Waiting for operation to complete...")
operation.result(OPERATION_TIMEOUT_SECONDS)
print(
"Created Performances table on database {} on instance {}".format(
database_id, instance_id
)
)
Ruby
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
# project_id = "Your Google Cloud project ID"
# instance_id = "Your Spanner instance ID"
# database_id = "Your Spanner database ID"
require"google/cloud/spanner"
require"google/cloud/spanner/admin/database"
database_admin_client=Google::Cloud::Spanner::Admin::Database.database_admin
db_path=database_admin_client.database_pathproject:project_id,
instance:instance_id,
database:database_id
job=database_admin_client.update_database_ddldatabase:db_path,
statements:[
"CREATE TABLE Performances (
SingerId INT64 NOT NULL,
VenueId INT64 NOT NULL,
EventDate Date,
Revenue INT64,
LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY (SingerId, VenueId, EventDate),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE"
]
puts"Waiting for update database operation to complete"
job.wait_until_done!
puts"Created table Performances in #{database_id}"
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.