What is a Constraint 

constraint is a rule that’s applied to a column or set of columns in a database table with the goal of preserving the data integrity. Constraints enforce limits on the data type and the range of values that can be used within columns. If any action violates a constraint, that action is aborted.

Constraint_Description_
NOT NULLEnsures that a column cannot have NULL value
DEFAULTProvides a default value for a column when none is specified
UNIQUEEnsures that all values in a column are unique
PRIMARY KEYA combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
CHECKEnsures that all values in a column satisfies certain conditions

sqlite> CREATE TABLE Employees (
    ID INT PRIMARY KEY,
    Name TEXT NOT NULL,
    Age INT CHECK(Age >= 18),
    Email TEXT UNIQUE,
    City TEXT DEFAULT 'Unknown'
);

Crow’s Foot Notation

Insert Into

-- Add one row
INSERT INTO table (column1,column2 ,..)
VALUES( value1,	value2 ,...);


-- Add multiple rows
INSERT INTO table1 (column1,column2 ,..)
VALUES 
   (value1,value2 ,...),
   (value1,value2 ,...),
    ...
   (value1,value2 ,...);

Update

UPDATE table
SET column_1 = new_value_1,
    column_2 = new_value_2
WHERE
    search_condition 

Delete

DELETE
FROM customers
WHERE customer_id = 2;

Drop Table

The DROP TABLE statement removes a table added with the CREATE TABLE statement.

DROP TABLE customers;



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *