SQL CHECK Constraint

SQL CHECK Constraint The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row. SQL CHECK Constraint on CREATE TABLE The following SQL creates a CHECK constraint on the…

Read More

Posted in SQL Tagged , Comments Off on SQL CHECK Constraint
SQL FOREIGN KEY Constraint

SQL FOREIGN KEY Constraint A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Let’s illustrate the foreign key with an example. Look at the following two tables: The “Persons” table: P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger The “Orders” table: O_Id OrderNo P_Id 1 77895 3 2 44678 3 3 22456 2 4 24562 1 Note that the…

Read More

Posted in SQL Tagged Comments Off on SQL FOREIGN KEY Constraint
SQL PRIMARY KEY Constraint

SQL PRIMARY KEY Constraint The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only ONE primary key. SQL PRIMARY KEY Constraint on CREATE TABLE The following SQL creates a PRIMARY KEY on the “P_Id” column when the “Persons” table is created: MySQL: CREATE TABLE Persons ( P_Id int NOT NULL,…

Read More

Posted in SQL Tagged Comments Off on SQL PRIMARY KEY Constraint
SQL UNIQUE Constraint

SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. SQL UNIQUE Constraint on CREATE TABLE The following SQL creates a UNIQUE constraint on the “P_Id”…

Read More

Posted in SQL Comments Off on SQL UNIQUE Constraint
SQL NOT NULL Constraint

By default, a table column can hold NULL values. SQL NOT NULL Constraint 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. The following SQL enforces the “P_Id” column and the “LastName” column to not accept NULL values: Example CREATE TABLE PersonsNotNull ( P_Id…

Read More

Posted in SQL Comments Off on SQL NOT NULL Constraint
SQL Constraints

SQL Constraints SQL constraints are used to specify rules for the data in a table. If there is any violation between the constraint and the data action, the action is aborted by the constraint. Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement). SQL CREATE TABLE + CONSTRAINT Syntax CREATE TABLE table_name ( column_name1 data_type(size) constraint_name, column_name2 data_type(size) constraint_name, column_name3 data_type(size) constraint_name, …. ); In SQL, we…

Read More

Posted in SQL Comments Off on SQL Constraints
SQL CREATE TABLE Statement

The SQL CREATE TABLE Statement The CREATE TABLE statement is used to create a table in a database. Tables are organized into rows and columns; and each table must have a name. SQL CREATE TABLE Syntax CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), …. ); The column_name parameters specify the names of the columns of the table. The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.). The size…

Read More

Posted in SQL Comments Off on SQL CREATE TABLE Statement
SQL CREATE DATABASE Statement

The SQL CREATE DATABASE Statement The CREATE DATABASE statement is used to create a database. SQL CREATE DATABASE Syntax CREATE DATABASE dbname; SQL CREATE DATABASE Example The following SQL statement creates a database called “my_db”: CREATE DATABASE my_db; Database tables can be added with the CREATE TABLE statement.

Read More

Posted in SQL Comments Off on SQL CREATE DATABASE Statement
SQL INSERT INTO SELECT Statement

With SQL, you can copy information from one table into another. The INSERT INTO SELECT statement copies data from one table and inserts it into an existing table. The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement selects data from one table and inserts it into an existing table. Any existing rows in the target table are unaffected. SQL INSERT INTO SELECT Syntax We can copy all columns from one table to another, existing table: INSERT INTO table2…

Read More

Posted in SQL Comments Off on SQL INSERT INTO SELECT Statement
SQL SELECT INTO Statement

With SQL, you can copy information from one table into another. The SELECT INTO statement copies data from one table and inserts it into a new table. The SQL SELECT INTO Statement The SELECT INTO statement selects data from one table and inserts it into a new table. SQL SELECT INTO Syntax We can copy all columns into the new table: SELECT * INTO newtable  FROM table1; Or we can copy only the columns we want into the new table: SELECT column_name(s)…

Read More

Posted in SQL Comments Off on SQL SELECT INTO Statement