Using a simple primary key

Use a simple primary key to create a single column that you can use to query and return results. This example creates a cyclist_name table storing an ID number and a cyclist’s first and last names in columns. The table uses a UUID as a Primary_key. This table can be queried to discover the name of a cyclist given their ID number.

A NULL value cannot be inserted into a PRIMARY KEY column. This restriction applies to both partition keys and clustering columns.

A simple primary key table can be created in multiple ways, as shown in the following examples.

Procedure

  1. Create the table cyclist_name in the cycling keyspace, making id the primary key. Insert the PRIMARY KEY keywords after the column name in the CREATE TABLE definition. Before creating the table, set the keyspace with a USE statement.

    CREATE TABLE cyclist_name (
      id UUID PRIMARY KEY, 
      lastname text, 
      firstname text
    );

*.This same example can be written with the primary key identified at the end of the table definition. Insert the PRIMARY KEY keywords after the last column definition in the CREATE TABLE definition, followed by the column name of the key. The column name of the primary key is enclosed in parentheses, which is id in the example below.

+

CREATE TABLE cycling.cyclist_name (
  id UUID, 
  lastname text, 
  firstname text, 
  PRIMARY KEY (id)
);
  1. The keyspace name can be used to identify the keyspace in the CREATE TABLE statement instead of the USE statement.

    CREATE TABLE cycling.cyclist_name (
      id UUID PRIMARY KEY, 
      lastname text, 
      firstname text
    );

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com