SQL Date Functions

As long as your data contains only the date portion, your queries will work as expected. However, if a time portion is involved, it gets complicated. Before talking about the complications of querying for dates, we will look at the most important built-in functions for working with dates. MySQL Date Functions The following table lists the most important built-in date functions in MySQL: Function Description NOW() Returns the current date and time CURDATE() Returns the current date CURTIME() Returns…

Read More

Posted in SQL Tagged Comments Off on SQL Date Functions
SQL Views

A view is a virtual table. This chapter shows how to create, update, and delete a view. SQL CREATE VIEW Statement In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the…

Read More

Posted in SQL Tagged Comments Off on SQL Views
SQL AUTO INCREMENT Field

Auto-increment allows a unique number to be generated when a new record is inserted into a table. AUTO INCREMENT a Field Very often we would like the value of the primary key field to be created automatically every time a new record is inserted. We would like to create an auto-increment field in a table. Syntax for MySQL The following SQL statement defines the “ID” column to be an auto-increment primary key field in the “Persons” table: CREATE TABLE…

Read More

Posted in SQL Tagged Comments Off on SQL AUTO INCREMENT Field
SQL ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. SQL ALTER TABLE Syntax To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype To delete a column in a table, use the following syntax (notice that some database systems don’t allow deleting a column): ALTER TABLE table_name DROP COLUMN column_name To change the data type of a column in a table, use the following syntax:…

Read More

Posted in SQL Tagged Comments Off on SQL ALTER TABLE Statement
SQL DROP INDEX – DROP TABLE – DROP DATABASE

Indexes, tables, and databases can easily be deleted/removed with the DROP statement. The DROP INDEX Statement The DROP INDEX statement is used to delete an index in a table. DROP INDEX Syntax for MS Access: DROP INDEX index_name ON table_name DROP INDEX Syntax for MS SQL Server: DROP INDEX table_name.index_name DROP INDEX Syntax for DB2/Oracle: DROP INDEX index_name DROP INDEX Syntax for MySQL: ALTER TABLE table_name DROP INDEX index_name   The DROP TABLE Statement The DROP TABLE statement is…

Read More

Posted in SQL Tagged Comments Off on SQL DROP INDEX – DROP TABLE – DROP DATABASE
SQL CREATE INDEX Statement

The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading the whole table. Indexes An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/queries. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only…

Read More

Posted in SQL Comments Off on SQL CREATE INDEX Statement
SQL DEFAULT Constraint

The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified. SQL DEFAULT Constraint on CREATE TABLE The following SQL creates a DEFAULT constraint on the “City” column when the “Persons” table is created: My SQL / SQL Server / Oracle / MS Access: CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City…

Read More

Posted in SQL Tagged Comments Off on SQL DEFAULT Constraint
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