Project: YugabyteDB (App Definition and Development / Database)
File: primary-key-ycql.md

title: Primary keys in YugabyteDB YCQL headerTitle: Primary keys linkTitle: Primary keys description: Defining Primary key constraint in YCQL headContent: Explore primary keys in YugabyteDB using YCQL menu: v2.18: identifier: primary-key-ycql parent: explore-indexes-constraints weight: 201 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../primary-key-ysql/" class="nav-link"> <i class="icon-postgres" aria-hidden="true"></i> YSQL </a> </li> <li > <a href="../primary-key-ycql/" class="nav-link active"> <i class="icon-cassandra" aria-hidden="true"></i> YCQL </a> </li> </ul> The Primary Key constraint is a means to uniquely identify a specific row in a table via one or more columns. In YCQL, it should be defined either under the `columnconstraint` or the `tableconstraint`, but not under both: column_constraint: Columns can be either STATIC or declared as the PRIMARY KEY. Declaring a column as STATIC results in the same value being shared for all those rows that belong to the same partition (rows with the partition key). Declaring a column as a PRIMARY KEY makes that individual column its sole component. table_constraint: PRIMARY KEY defined as the tableconstraint takes columns to form one or more partition keys and zero or more clustering keys. Syntactically, the order is to have the `partitionkeycolumnlist` first, followed by the `clusteringkeycolumn_list`. Refer to the Grammar section for in YCQL. The section includes details about the , , and . {{% explore-setup-single %}} Create a `users` table with `user_id` as the primary key. ```cql CREATE TABLE users(userid INT PRIMARY KEY, fullname TEXT); ``` Insert two rows into the table and check the entries. ```cql INSERT INTO users(userid , fullname) VALUES (1, 'John'); INSERT INTO users(userid , fullname) VALUES (1, 'Rose'); SELECT * FROM users; ``` ```output userid | fullname +-- 1 | Rose ``` The second entry with `Rose` as the `fullname` overrides the first entry because the `userid` is the same. Create a `devices` table with `supplierid` and `deviceid` as the partitioning columns and `model` year as the clustering column. ```cql CREATE TABLE devices(supplier_id INT, device_id INT, model_year INT, device_name TEXT, PRIMARY KEY((supplierid, deviceid), model_year)); ``` Insert three rows into the table and view the contents. ```cql INSERT INTO devices(supplierid, deviceid, devicename, modelyear) VALUES (1, 101, 'iPhone', 2013); INSERT INTO devices(supplierid, deviceid, devicename, modelyear) VALUES (1, 102, 'Pixel', 2011); INSERT INTO devices(supplierid, deviceid, devicename, modelyear) VALUES (1, 102, 'Samsung S3', 2001); SELECT * FROM devices; ``` ```output supplierid | deviceid | modelyear | devicename -+--++- 1 | 101 | 2013 | iPhone 1 | 102 | 2001 | Samsung 1 | 102 | 2011 | Pixel (3 rows) ``` Insert another entry with `supplierid`, `deviceid`, and `model_year` as 1, 102, and 2011 respectively. ```cql INSERT INTO devices(supplierid, deviceid, devicename, modelyear) VALUES (1, 102, 'MotoRazr', 2011); SELECT * FROM devices; ``` ```output supplierid | deviceid | modelyear | devicename -+--++- 1 | 101 | 2013 | iPhone 1 | 102 | 2001 | Samsung 1 | 102 | 2011 | MotoRazr ``` The row with `device_name` Pixel is replaced with MotoRazr.