SQL DDL - основные команды и использование
SQL DDL (Data Definition Language)
SQL DDL (Data Definition Language) is a set of commands in the SQL language that is used to define the structure of a database. These commands allow you to create, modify, or delete tables, indexes, integrity constraints, and other database objects.
One of the main operations in SQL DDL is creating tables. Here is an example code demonstrating the creation of a "users" table using the CREATE TABLE command:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT,
email VARCHAR(100) UNIQUE,
creation_date DATE DEFAULT CURRENT_DATE
);
In this example, we are creating a "users" table with columns "id", "name", "age", "email", and "creation_date". The "id" column is the primary key (PRIMARY KEY), ensuring the uniqueness of each record in the table. The "name" column has a VARCHAR(50) type and cannot be empty (NOT NULL). The "age" column has an INT type and can contain integer values. The "email" column has a VARCHAR(100) type and must be unique for each record in the table. The "creation_date" column has a DATE type and defaults to the current date when a new record is added.
Another important operation in SQL DDL is modifying tables. For example, we can add new columns to an existing table. Here is an example code that adds a new "phone" column with a VARCHAR(20) type to the "users" table:
ALTER TABLE users
ADD phone VARCHAR(20);
Now the "users" table will have an additional "phone" column to store users' phone numbers.
SQL DDL commands also allow you to delete tables and other database objects. For example, the following code demonstrates deleting the "users" table:
DROP TABLE users;
After executing this command, the "users" table and all its data are completely deleted from the database.
In conclusion, SQL DDL provides powerful tools for defining the structure of a database. It allows you to create, modify, and delete tables, as well as define integrity constraints and other objects. The code examples described above demonstrate basic SQL DDL operations that can be used to manage a database.