SQL FOREIGN KEY Constraint

The FOREIGN KEY constraint is used to point to primary key of another database table. Foreigh key ensures that the data exists when we have reference to it.

For example, we have a table of Employee (EmployeeId is Primary):

EmployeeId FirstName LastName
1 Mkai Khaao
2 Hiaf Lihaaa

And we have another table Project:

ProjectId ProjectName ProjectManagerId
1 Basic Project 1
2 Premium Project 2

The ProjectManagerId in table Project points to EmployeeId in table Employee. That means only an exist employee can be as a project manager. If you try to set ProjectManagerId=3, it will fail, because it doesn’t have an employee with EmployeeId=3.

Continue reading

SQL UNIQUE Constraint

The UNIQUE constraint is used to uniquely identify each row in a database table. It is some similar to Primary key, but has some difference:

  • Primary Key is automatically Unique Constraint
  • One table should have ONE primary key, but could have mutiple Unique Constraint

Continue reading

SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint is used to identify each row in a database table. It has some features:

  • It must be unique value
  • One table should have ONE primary key.
  • One table should ONLY have one primary key.

Continue reading

SQL DEFAULT Constraint

The SQL DEFAULT Constraint is used to set a default value to a column when we insert a new row to a table without providing a value for the column.

We can use the DEFAULT keyword to specify a default value for a column.

Continue reading

SQL Create Table

SQL table is a place where the SQL database stores the data. It has columns and rows. We can use CREATE TABLE statement to create a table in a database.

Continue reading

SQL REVERSE

SQL REVERSE function is used to return reverse of a string. It’s supported by MS SQL Server, MySQL. Oracle supports it too, but maybe not in the documents.

Continue reading

SQL LEFT Function

SQL LEFT() function is used to get left part of a string with the length of specified number. It is supported by MySQL, MS SQL Server, Access, but not by Oracle.

Continue reading