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
SQL UNION Operator

The SQL UNION operator combines the result of two or more SELECT statements. The SQL UNION Operator The UNION operator is used to combine the result-set of two or more SELECT statements. Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order. SQL UNION Syntax SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2; Note: The UNION operator selects only…

Read More

Posted in SQL Comments Off on SQL UNION Operator
SQL Aliases

SQL aliases are used to temporarily rename a table or a column heading. SQL Aliases SQL aliases are used to give a database table, or a column in a table, a temporary name. Basically aliases are created to make column names more readable. SQL Alias Syntax for Columns SELECT column_name AS alias_name FROM table_name; SQL Alias Syntax for Tables SELECT column_name(s) FROM table_name AS alias_name; Example SELECT CustomerName AS Customer, ContactName AS FROM Customers; or SELECT CustomerName, Address+’, ‘+City+’, ‘+PostalCode+’, ‘+Country AS Address FROM Customers;…

Read More

Posted in SQL Comments Off on SQL Aliases
SQL BETWEEN Operator

The BETWEEN operator is used to select values within a range. The SQL BETWEEN Operator The BETWEEN operator selects values within a range. The values can be numbers, text, or dates. SQL BETWEEN Syntax SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2; Example SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;  

Read More

Posted in SQL Comments Off on SQL BETWEEN Operator