Syntax: CREATE DATABASE <database_name>;
Creates a new database.
Example:
CREATE DATABASE my_app;
A quick reference guide to CockroachDB's SQL dialect, covering data definition, manipulation, querying, and transaction management. Useful for both beginners and experienced users looking to quickly recall syntax and best practices.
|
Syntax: Creates a new database. Example:
|
|
Creating a database if it does not exist: Creates a new database only if one doesn’t already exist. Example:
|
|
Syntax: Creates a new table. Example:
|
|
Data Types: Common data types include Constraints: |
|
Creating a table with foreign key:
|
|
Syntax: Adds a new column to an existing table. Example:
|
|
Syntax: Removes a column from an existing table. Example:
|
|
Syntax: Renames a column in an existing table. Example:
|
|
Syntax: Inserts new rows into a table. Example:
|
|
Inserting multiple rows:
|
|
Inserting all columns: If inserting into all columns, you can omit the column names.
|
|
Syntax: Updates existing rows in a table. Example:
|
|
Updating multiple columns:
|
|
Syntax: Deletes rows from a table. Example:
|
|
Deleting all rows (truncate): Use with caution!
|
|
Syntax: Retrieves data from a table. Example:
|
|
Selecting all columns:
|
|
Using aliases:
|
|
WHERE Clause:
|
|
ORDER BY Clause:
|
|
LIMIT and OFFSET:
|
|
Common functions: Example:
|
|
GROUP BY Clause:
|
|
HAVING Clause: Filters groups based on a condition.
|
|
Starting a transaction: Marks the beginning of a transaction block. Example:
|
|
Committing a transaction: Saves all changes made during the transaction. |
|
Rolling back a transaction: Discards all changes made during the transaction. |
|
Savepoints:
|
|
CockroachDB supports It prevents phenomena like dirty reads, non-repeatable reads, and phantom reads. |
|
CockroachDB uses optimistic concurrency control, which assumes that multiple transactions can frequently complete without interfering with each other. |