SQL

Structured Query Language

SQL DEFAULT Constraint

The DEFAULT Constraint is used to insert the default values into a column during design time.

The default values is used in new record if no value is specified during INSERT Statement.

The default

SQL DEFAULT Constraint Syntax

CREATE TABLE table_name ( column_name1 data_type DEFAULT Value, column_name2 data_type DEFAULT Value, column_name3 data_type DEFAULT Value, .... )

Example

SQL COUNT() Function

COUNT is a simple aggregate function provided by SQL. As it is clear from its name that it simply counts the number of records in the table and returns the total count.

There are three (3) different usage for the Count function.

  • SQL COUNT(column_name)
  • COUNT(*)
  • COUNT(DISTINCT column_name)

Consider the following table for this exercise

Employess

SQL Data Types

The data type determines the kind of values that users can store in the field. Like every programming languages, the SQL also has specific data type to store data. Following are some data type and there descriptions in which SQL allows us to store data.

Microsoft Access Data Types

Source: Microsoft Access Help

SQL AUTO INCREMENT Field

The AUTO INCREMENT generates a new number that increment by 1 from the previous number. This will allow a unique number to be associated in a row when it is inserted into a table.

You can set the AUTO INCREMENT during creation of the table or you can also use ALTER TABLE Statement if you have already created the table.

The following example will create a table named "Users" and add a Primary Key with Auto Increment in ID field.

Syntax for SQL Server

SQL ALTER TABLE Statement

The ALTER TABLE statement is used to change the structure of an existing table like adding, deleting, or modifying of column.

SQL ALTER TABLE Syntax

To add a new column to the existing table use the following syntax:
ALTER TABLE table_name ADD column_name datatype

To remove a column from a table use the following syntax:
ALTER TABLE table_name DROP TABLE column_name

SQL HAVING Clause

The HAVING Clause is always used after the GROUP BY clause, it can not come without the GROUP BY clause. It works the same as the WHERE Clause, it is therefore also used to apply conditions on the aggregate_functions which was impossible by using simple WHERE clause.

The HAVING clause simply contains the conditions to filter data just like in WHERE clause.

SQL HAVING Syntax

SELECT column_name(s), aggregate_function(column_name) FROM Table WHERE some_condition GROUP BY grouping_condtion HAVING aggregate_function(column_name) operator value