SQL INSERT statement

The INSERT statement is used to insert a new record into the table. Ways to insert the data into a table: Inserting the data directly to a table. Inserting data to a table through a select statement. 1. Inserting the data directly to a table. In case when we insert the values in the specified … Read more

Categories SQL

SQL UNIQUE Constraint

The UNIQUE constraint is used to uniquely identify each row in a table. It is like primary key but it can contain one null value and a table can have more than one UNIQUE constraint. Syntax of UNIQUE Constraint on one column with CREATE TABLE statement: MySQL: CREATE TABLE Persons ( P_Id int NOT NULL, … Read more

Categories SQL

SQL NOT NULL Constraint

The NOT NULL constraint enforces a field or column to not contain a null value. Example of NOT NULL constraint with CREATE TABLE statement: CREATE TABLE PERSONS (   P_ ID INT NOT NULL,   NAME VARCHAR (20) NOT NULL,   AGE INT,   PRIMARY KEY (P_ID)   );CREATE TABLE PERSONS ( P_ ID INT NOT NULL, NAME VARCHAR (20) NOT … Read more

Categories SQL

SQL FOREIGN KEY

The FOREIGN KEY is used to define a relationship between two tables and a FOREIGN KEY in one table points to the PRIMARY KEY in another table. Syntax of FOREIGN KEY Constraint on one column with CREATE TABLE statement: MySQL: CREATE TABLE Orders ( Order_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY … Read more

Categories SQL

SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint is used to uniquely identify each row in a table. A PRIMARY KEY must contain unique values and it can’t contain a null value. A table can have only one primary key. We can use multiple columns or fields to define a primary key, such primary key is known as composite … Read more

Categories SQL

SQL CONSTRAINTS

The SQL constraints are the rules enforced on the data in a table. A constraint can be specified with the CREATE TABLE statement at the time table creation or we can specify it with the ALTER TABLE statement after the table creation. Syntax: CREATE TABLE <em>tableName</em> ( <em>columnName1 datatype</em>(<em>size</em>) <em>constraintName</em>, <em>columnName2 data_type</em>(<em>size</em>) <em>constraintName</em>, <em>columnName2 data_type</em>(<em>size</em>) … Read more

Categories SQL

SQL TRUNCATE Table

The TRUNCATE TABLE command is used to remove the all rows from an existing table. Syntax: TRUNCATE TABLE tableName;  Example: TRUNCATE TABLE EMPLOYEE;TRUNCATE TABLE EMPLOYEE; Output: Table truncated.Table truncated. Difference between TRUNCATE, DELETE and DROP commands: The DELETE statement is used to delete or remove all the rows from the table. We can use where … Read more

Categories SQL

SQL DELETE from table

The DELETE statement is used to delete or remove all the rows from the table. Syntax: DELETE FROM tableName; Example: DELETE FROM EMPLOYEE;DELETE FROM EMPLOYEE; Output: 3 row(s) deleted.3 row(s) deleted. The DELETE statement with where condition is used to delete or remove the specific rows from the table. Syntax: DELETE FROM tableName [WHERE condition]; … Read more

Categories SQL

SQL DROP Table

The DROP TABLE statement is used to delete an existing table definition, indexes, constraints, access privileges and all associated data. Note: When a table is deleted all the available information in the table will be lost, so use it carefully. Syntax: DROP TABLE tableName;   Example: DROP TABLE EMPLOYEE;DROP TABLE EMPLOYEE; Output: Table dropped.Table dropped.   Next Topic: … Read more

Categories SQL

SQL RENAME Table

The RENAME TABLE statement is used to change the table name. Syntax: RENAME tableName TO newTableName; We can also use the ALTER TABLE statement to change the table name. Syntax: ALTER tableName RENAME TO newTableName; Example: RENAME EMPLOYEE1 TO EMPLOYEE2;   Or   ALTER EMPLOYEE1 RENAME TO EMPLOYEE2;RENAME EMPLOYEE1 TO EMPLOYEE2; Or ALTER EMPLOYEE1 RENAME TO EMPLOYEE2; Example: Statement processed.Statement processed.   … Read more

Categories SQL