Sybase Constraints

Sybase Constraints: Constraints are used prevent the invalid data into the tables. Available constraints are:

  • NOT NULL
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
  • CHECK

Not null: This does not allow null values in the respective column. But, this allows duplicate null values. If we dont specify not null or anything, Sybase allocates default null on the column.

Example:

create table accounts(account_id int not null, account_type char)
go

Primary Key:

  • Primary key uniquely identifies a record in tables.
  • Primary key does not accept duplicate or null values.
  • There can be only one primary key in a table. If primary key created in more than one column is called composite primary key.

Example:

create table accounts(account_id int not null, account_name varchar(50) not null, account_type char,
primary key(account_id))
go

Foreign Key:

  • If we want to establish relationship between two tables, then we must establish referential integrity constraint FOREIGN KEY.
  • One table foreign must belongs to anther table primary key. These two columns datatype must be same.
  • Foreign key accepts duplicate and null values.

Example:

create table emp(Empno int primary key,
Deptno int references DEPT(Deptno),
LastName varchar(30),
FirstName varchar(30),
Salary int)
go

create table DEPT(Deptno int primary key
DeptName varchar(20),
Mgr int,
foreign key MGR_EMPNO (Mgr) references EMPLOYEE(EmpNo))
go

UNIQUE Constraint:

  • Unique constraints do not allow duplicate values.
  • Unique constraints creates unique nonclustered index on table column.

Example:

create table accounts(account_id int, account_pan varchar(20), account_type char, account_name varchar(20)
unique(account_pan))
go

Check Constraint:

  • A check constraint is simply applying logical condition on table column.
  • Check constraint used to check a limited, specific range of values.

Example:

create table accounts(account_id int not null,
account_name varchar(50) not null, account_type char,min_balance int CHECK(min_balance>500)
)
go

Powered by k2schools