SQL DDL (Data Definition Language), CREATE, ALTER, DROP, RENAME and TRUNCATE statements

The language SQL (Structured Query Language) is a database language that allows you to access databases.

This language is used to access the main databases existing today such as SQL Server Microsoft or Oracle database, of course the syntax between these two databases may have some differences, but the basic commands are practically the same.

 

SQL Instruções DDL (Data Definition Language), CREATE, ALTER, DROP, RENAME e TRUNCATE
SQL DDL (Data Definition Language), CREATE, ALTER, DROP, RENAME and TRUNCATE statements

DDL (Data Definition Language) Instructions

Data Definition Language, defines the structure of data and tables. The most common DDL commands are CREATE, ALTER, DROP, RENAME and TRUNCATE.

CREATE – Command used to create a BANK (CREATE DB) is TABLE (CREATE TABLE).

CREATE DB – Creates a database. See an example of use:

  • CREATE DATABASE database_name

CREATE TABLE – Creates a table. See an example of use:

CREATE TABLE doctor
(
INT code,
name VARCHAR(255),
INT age,
crm VARCHAR(80),

primary key code,
)

Explanation

  • CREATE TABLE is the create command
  • doctor – the name you want to give the table
  • code, name, age, crm – are the columns you insert in this table
  • primary key – define what the primary key will be

NOTE: after the column names, you can define the parameters of each column, eg data type, size, if required.

 

ALTER TABLE – Command used to add, delete or modify columns in an existing table.

Add column

  • ALTER TABLE table_name ADD column_name datatype
  • Example usage: ALTER TABLE doctor ADD Address VARCHAR(80)

Delete a column

  • ALTER TABLE table_name DROP COLUMN column_name
  • Example usage: ALTER TABLE doctor DROP Address

 

DROP – Command used to delete data. DROP can be used in three ways:

  • DROP INDEX – Delete an index in a table
  • Usage example: ALTER TABLE table_name DROP INDEX index_name
  • DROP TABLE – Delete a table
  • Usage example: DROP TABLE table_name
  • DROP DATABASE – Delete a database
  • Usage example: DROP DATABASE database_name

 

RENAME – Command used to rename tables. See below an example of use:

  • RENATE TABLE doctor TO doctors

TRUNCATE – Command to delete data from a table, not the table itself. See usage example:

  • TRUNCATE TABLE doctors

 7,964 Total Visualizações,  6 Visualizações Hoje

One comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.