MySQL NOT NULL Constraint

MySQL NOT NULL Constraint

By default, a column can hold NULL values.

The NOT NULL constraint enforces a column to NOT accept NULL values.

This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.


Continue reading MySQL NOT NULL Constraint

MySQL Constraints

SQL constraints are used to specify rules for data in a table.

Create Constraints

Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.

Syntax

CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    column3 datatype constraint,
    ....
);

Continue reading MySQL Constraints

MySQL ALTER TABLE Statement

MySQL ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

The ALTER TABLE statement is also used to add and drop various constraints on an existing table.


Continue reading MySQL ALTER TABLE Statement

MySQL DROP TABLE Statement

The MySQL DROP TABLE Statement

The DROP TABLE statement is used to drop an existing table in a database.

Syntax

DROP TABLE table_name;

Note: Be careful before dropping a table. Deleting a table will result in loss of complete information stored in the table!

Continue reading MySQL DROP TABLE Statement

MySQL CREATE TABLE Statement

The MySQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in a database.

Syntax

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);

The column parameters specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).

Continue reading MySQL CREATE TABLE Statement

MySQL DROP DATABASE Statement

The MySQL DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database.

Syntax

DROP DATABASE databasename;

Note: Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database!

Continue reading MySQL DROP DATABASE Statement

MySQL CREATE DATABASE Statement

The MySQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new SQL database.

Syntax

CREATE DATABASE databasename;

Continue reading MySQL CREATE DATABASE Statement

MySQL Operators

MySQL Arithmetic Operators

Operator Description
+ Add
Subtract
* Multiply
/ Divide
% Modulo

Continue reading MySQL Operators

MySQL Comments

MySQL Comments

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.

Single Line Comments

Single line comments start with --.

Any text between — and the end of the line will be ignored (will not be executed).

The following example uses a single-line comment as an explanation: Continue reading MySQL Comments

MySQL NULL Functions

MySQL IFNULL() and COALESCE() Functions

Look at the following “Products” table:

P_Id ProductName UnitPrice UnitsInStock UnitsOnOrder
1 Jarlsberg 10.45 16 15
2 Mascarpone 32.56 23
3 Gorgonzola 15.67 9 20

Suppose that the “UnitsOnOrder” column is optional, and may contain NULL values. Continue reading MySQL NULL Functions