drop the old table DROP TABLE old_table copy data from old table to the new one INSERT INTO new_table SELECT * FROM old_table create a new table CREATE TABLE new_table ( To make all statements above transaction-safe, you should execute all of them within a transaction like this: BEGIN Third, drop the old table: DROP TABLE old_table Code language: SQL (Structured Query Language) ( sql )įourth, rename the new table to the old one: ALTER TABLE new_table RENAME TO old_table Code language: SQL (Structured Query Language) ( sql ) INSERT INTO new_table SELECT * FROM old_table Code language: SQL (Structured Query Language) ( sql ) Second, copy data from the old table to the new table. Check out the SQLite DESCRIBE table tutorial for more information. To get the structure of the old table, you can use the. The new table should also include the CHECK constraint: CREATE TABLE new_table ( VALUES( 'New XFactor', 1000, -10) Code language: SQL (Structured Query Language) ( sql ) Adding CHECK constraints to an existing tableĪs of version 3.25.2, SQLite does not support adding a CHECK constraint to an existing table.įirst, create a new table whose structure is the same as the table that you want to add a CHECK constraint. The following statement also violates the CHECK constraint because the discount is negative: INSERT INTO products(product_name, list_price, discount) VALUES( 'New Product', 900, 1000) Code language: SQL (Structured Query Language) ( sql ) INSERT INTO products(product_name, list_price, discount) The following statement violates the CHECK constraint because the discount is higher than the list price. The CHECK constraint ensures that list price is always greater or equal to discount and both discount and list price are greater or equal to zero. List_price >= 0) Code language: SQL (Structured Query Language) ( sql ) In this example, the CHECK constraint is defined at the table level: CHECK (list_price >= discount AND The following statement creates a new table named products: CREATE TABLE products (ĭiscount DECIMAL ( 10, 2) NOT NULL DEFAULT 0, VALUES( 'John', 'Doe', '(408)-123-456') Code language: SQL (Structured Query Language) ( sql ) 2) Using SQLite CHECK constraints at the table level example The following statement should work because the value in the phone column has 13 characters, which satisfies the expression in the CHECK constraint: INSERT INTO contacts(first_name, last_name, phone) The reason was that the phone number that you attempted to insert just has 9 characters while it requires at least 10 characters. Here is the error message: Result: CHECK constraint failed: contacts VALUES( 'John', 'Doe', '408123456') Code language: SQL (Structured Query Language) ( sql ) If you attempt to execute the following statement, you will get a constraint violation error: INSERT INTO contacts(first_name, last_name, phone) This CHECK constraint ensures that the values in the phone column must be at least 10 characters. In the contacts table, the phone column has a CHECK constraint: CHECK ( length(phone) >= 10) Phone TEXT NOT NULL CHECK ( length(phone) >= 10) The following statement creates a new table named contacts: CREATE TABLE contacts ( 1) Using SQLite CHECK constraint at the column level example Let’s take some examples of using the CHECK constraints. Note that the expression of a CHECK constraint cannot contain a subquery. If the result is a non-zero value or NULL, it means no constraint violation occurred. If the result is zero, then a constraint violation occurred. In this syntax, whenever a row is inserted into a table or an existing row is updated, the expression associated with each CHECK constraint is evaluated and returned a numeric value 0 or 1. The following statement shows how to define a CHECK constraint at the column level: CREATE TABLE table_name(Ĭode language: SQL (Structured Query Language) ( sql )Īnd the following statement illustrates how to define a CHECK constraint at the table level: CREATE TABLE table_name( SQLite allows you to define a CHECK constraint at the column level or the table level. The CHECK constraints allow you to define additional data integrity checks beyond UNIQUE or NOT NULL to suit your specific application. If the values do not meet the criteria defined by the expression, SQLite will issue a constraint violation and abort the statement. SQLite CHECK constraints allow you to define expressions to test values whenever they are inserted into or updated within a column. Summary: in this tutorial, you will learn how to use SQLite CHECK constraint to validate data before insert or update.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |