Requirements are the standards upheld on an information sections on table. These are utilized to restrict the sort of information that can go into a table. This guarantees the exactness and unwavering quality of the information in the data set.
Requirements could be section level or table level. Section level imperatives are applied uniquely to one segment, while table level limitations are applied to the entire table.
Following are generally utilized imperatives accessible in SQLite.
- NOT NULL Constraint − Ensures that a segment can't have NULL worth.
- DEFAULT Constraint − Provides a default an incentive for a segment when none is indicated.
- Remarkable Constraint − Ensures that all qualities in a section are unique.
- Essential Key − Uniquely distinguishes each line/record in an information base table.
- CHECK Constraint − Ensures that all qualities in a segment fulfills certain conditions.
NOT NULL Constraint
Of course, a segment can hold NULL qualities. In the event that you don't need a section to have a NULL worth, at that point you need to characterize such imperative on this segment indicating that NULL is currently not took into account that segment.
A NULL isn't equivalent to no information, rather, it addresses obscure information.
Example
For instance, the accompanying SQLite articulation makes another table called COMPANY and adds five sections, three of which, ID and NAME and AGE, determines not to acknowledge NULLs.
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
DEFAULT Constraint
The DEFAULT requirement gives a default an incentive to a section when the INSERT INTO articulation doesn't offer a particular benefit.
Example
For instance, the accompanying SQLite explanation makes another table called COMPANY and adds five sections. Here, SALARY segment is set to 5000.00 as a matter of course, along these lines in the event that INSERT INTO articulation doesn't offer a benefit for this section, at that point naturally, this segment would be set to 5000.00.
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL DEFAULT 50000.00
);
UNIQUE Constraint
The UNIQUE Constraint keeps two records from having indistinguishable qualities in a specific section. In the COMPANY table, for instance, you should keep at least two individuals from having an indistinguishable age.
Example
For instance, the accompanying SQLite articulation makes another table called COMPANY and adds five segments. Here, AGE segment is set to UNIQUE, so you can't have two records with a similar age −
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL UNIQUE,
ADDRESS CHAR(50),
SALARY REAL DEFAULT 50000.00
);
PRIMARY KEY Constraint
The PRIMARY KEY imperative particularly distinguishes each record in a data set table. There can be more UNIQUE segments, however just a single essential key in a table. Essential keys are significant when planning the information base tables. Essential keys are interesting IDs.
We use them to allude to table lines. Essential keys become unfamiliar keys in different tables, while making relations among tables. Because of a 'longstanding coding oversight', essential keys can be NULL in SQLite. This isn't the situation with different data sets.
An essential key is a field in a table which extraordinarily distinguishes each columns/records in a data set table. Essential keys should contain extraordinary qualities. An essential key section can't have NULL qualities.
A table can have just a single essential key, which may comprise of single or different fields. At the point when numerous fields are utilized as an essential key, they are known as a composite key.
In the event that a table has an essential key characterized on any field(s), at that point you can't have two records having a similar estimation of that field(s).
Example
You as of now have seen different models above where we have made COMPANY table with ID as an essential key.
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CHECK Constraint
CHECK Constraint empowers a condition to check the worth being gone into a record. On the off chance that the condition assesses to bogus, the record abuses the requirement and isn't gone into the table.
Example
For instance, the accompanying SQLite makes another table called COMPANY and adds five segments. Here, we add a CHECK with SALARY section, so you can't have any SALARY Zero.
CREATE TABLE COMPANY3(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL CHECK(SALARY > 0)
);
Dropping Constraint
SQLite upholds a restricted subset of ALTER TABLE. The ALTER TABLE order in SQLite permits the client to rename a table or add another segment to a current table. It is unimaginable to expect to rename a section, eliminate a segment, or add or eliminate requirements from a table.