Alter Command In SQL | Truncate Table In SQL

1. SQL – Alter & Truncate Command

Today, in this SQL Tutorial, we discuss SQL Truncate and Alter Command. We are going to start with a brief introduction of Truncate and Alter Command in SQL. Then, we will learn syntax and example of SQL Trunc & Alter Command.
So, let us start with Alter Command In SQL.

Alter Command In SQL | Truncate Table In SQL

Alter Command In SQL | Truncate Table In SQL

2. Alter & Truncate Table Command in SQL

The SQL ALTER TABLE command is used to include, erase or adjust segments in a current table. You ought to likewise use the ALTER TABLE command to include and drop different limitations on a current table.
Do you know about SQL Data Types
The SQL TRUNCATE TABLE command is used to erase data completely from a current table.

3. ALTER Command in SQL

Language Syntax of Alter Table Command in SQL–
The fundamental sentence syntax of an SQL ALTER command to include a new column in a current table is as per the following.

ALTER TABLE table_name ADD column_name datatype;

The fundamental language syntax of an ALTER TABLE order to DROP COLUMN in a current table is as per the following.

ALTER TABLE table_name DROP COLUMN column_name;

The fundamental sentence syntax of an ALTER Command in SQL to change the DATA TYPE of a segment in a table is as per the following.

ALTER TABLE table_name MODIFY COLUMN column_name datatype;

The essential language syntax of an ALTER TABLE command to add a NOT NULL limitation to a segment in a table is as per the following.
Have a look at SQL Constraints

ALTER TABLE table_name MODIFY column_name datatype NOT NULL;

The essential language syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as per the following.

ALTER TABLE table_name
Include CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);

The essential language syntax of an ALTER command in SQL to PRIMARY KEY to a table is as per the following.

ALTER TABLE table_name
Include CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

The fundamental syntax of an ALTER TABLE command to ADD PRIMARY KEY limitation to a table is as per the following.

ALTER TABLE table_name
Include CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);

The essential syntax of an ALTER TABLE order to DROP CONSTRAINT from a table is as per the following.

ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;

In case you’re utilizing MySQL, the code is as per the following −
Do you know about SQL Operators

ALTER TABLE table_name
DROP INDEX MyUniqueConstraint;

The fundamental syntax of an ALTER TABLE command to DROP PRIMARY KEY requirement from a table is as per the following.

ALTER TABLE table_name
DROP CONSTRAINT MyPrimaryKey;

In case you’re utilizing MySQL, the code is as per the following −

ALTER TABLE table_name
DROP PRIMARY KEY;

4. Example of Alter Command in SQL

Consider the CUSTOMERS table having the accompanying records –

Alter Command in SQL

Example of Alter Command in SQL

Alter table in SQL

Example of Alter Command in SQL

Presently, the CUSTOMERS table is changed and the following would be yield from the SELECT explanation.
Let’s revise SQL Server Transaction

Following is the case to ADD a New Column to a current table –

ALTER TABLE CUSTOMERS ADD SEX char(1);

Following is the case to DROP the sex section from the current table.

ALTER TABLE CUSTOMERS DROP SEX;

Presently, the CUSTOMERS table is changed and the following would be the yield from the SELECT articulation.

Alter Command in SQL

SELECT articulation

5. Truncate Command in SQL

You can likewise use DROP TABLE command to delete data completely table yet it would expel delete table structure frame the database and you would need to re-make this table by and by on the off chance that you wish you store a few information.
Have a look at SQL Join
The syntax of Truncate Table Command in SQL –
The fundamental syntax of a SQL TRUNCATE TABLE command is as per the following.

TRUNCATE TABLE table_name;

Example of Truncate Table in SQL–
Consider a CUSTOMERS table having the accompanying records –

Alter Command in SQL

Truncate Table Commands

The syntax of SQL Truncate table command –

SQL > TRUNCATE TABLE CUSTOMERS;

Now, the CUSTOMERS table is truncated and the output from SELECT statement will be as shown in the code block below −

SQL> SELECT * FROM CUSTOMERS;

Empty set (0.00 sec)
So, this was all in Alter Command in SQL. Hope you like our explanation of SQL truncate.

6. Conclusion

Hence, in this SQL tutorial, we discussed Alter Command in SQL and Truncate Table SQL Server. Moreover, we looked out at the syntax and example of SQL Alter Table Command and SQL Truncate. Still, if any doubt regarding SQL Truncate and Alter table Command, ask in the comment tab.
See also –
SQL Data Types
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.