Program No.11 AIM: Types of SQL Constraints: NOT NULL Constraint
Program No.11 AIM: Types of SQL Constraints: NOT NULL Constraint
11
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any
violation between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to
a column, and table level constraints apply to the whole table.
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means
that you cannot insert a new record, or update a record without adding a value to
this field.
Example:
The following SQL enforces the "P_Id" column and the "LastName" column to not
accept NULL values:
CREATE TABLE PersonsNotNull
FirstNamevarchar(255),
Address varchar(255),
City varchar(255)
The following SQL creates a UNIQUE constraint on the "P_Id" column when the
"Persons" table is created:
FirstNamevarchar(255),
Address varchar(255),
City varchar(255)
)
The following SQL creates a PRIMARY KEY on the "P_Id" column when the
"Persons" table is created:
FirstNamevarchar(255),
Address varchar(255),
City varchar(255),
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in
the "Persons" table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons"
table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into
the foreign key column, because it has to be one of the values contained in the
table it points to.
Example:
The following SQL creates a FOREIGN KEY on the "P_Id" column when the
"Orders" table is created:
P_Idint,
o CHECK Constraint:
o The CHECK constraint is used to limit the value range that can be placed
in a column.
FirstNamevarchar(255),
Address varchar(255),
City varchar(255),
CHECK (P_Id>0)
o DEFAULT Constraint:
o The default value will be added to all new records, if no other value is
specified.
o Example:
FirstNamevarchar(255),
Address varchar(255),
)
PROGRAM NO.12
This reference contains string, numeric, date, conversion, and some advanced functions in SQL
Server.
Function Description
QUOTENAME Returns a Unicode string with delimiters added to make the string a valid SQL
identifier
REPLACE Replaces all occurrences of a substring within a string, with a new substring
REPLICATE Repeats a string a specified number of times
STUFF Deletes a part of a string and then inserts another part into the string, starting a
TRANSLATE Returns the string from the first argument after the characters specified in the s
translated into the characters specified in the third argument.
TRIM Removes leading and trailing spaces (or other specified characters) from a strin
UNICODE Returns the Unicode value for the first character of the input expression
Function Description
Function Description
DATEADD Adds a time/date interval to a date and then returns the date
DATEFROMPARTS Returns a date from the specified parts (year, month, and day va
GETUTCDATE Returns the current database system UTC date and time
MONTH Returns the month part for a specified date (a number from 1 to
Function Description
CURRENT_USER Returns the name of the current user in the SQL Server database
ISNULL Return a specified value if the expression is NULL, otherwise retu
SESSION_USER Returns the name of the current user in the SQL Server database