Contact Us

Kockpit is here to help you

Business Form

Creating and Modifying Database Structures with SQL

The SQL Data Definition Language (DDL) plays a pivotal role in shaping a database's structure. It empowers developers and database administrators to craft, alter, and oversee a wide array of database elements, including tables, indexes, and constraints. These elements form the cornerstone of any database-driven application, underscoring the significance of proficiency in utilizing SQL DDL for the development and upkeep of robust databases. 

Modern applications heavily rely on databases as they are responsible for storing, organizing, and efficiently retrieving data. Structured Query Language (SQL) is a crucial tool used to manage these databases. It is a powerful language that includes both Data Definition Language (DDL) and Data Manipulation Language (DML). This article aims to explain SQL DDL and how it is used to create and modify database structures.

The Basics of Data Definition Language 

Data Definition Language in short DDL, is a part of SQL responsible for defining the structure of a database. It mainly comprises different commands that enable you to create, modify, and delete various objects in the database like tables, indexes, views, and constraints. DDL statements play a vital role in defining the schema or structure of a database, which includes data types and relationships. In simpler terms, DDL is a crucial aspect of database management, as it helps create, modify, and define the structure of a database.

Key DDL Commands in SQL 

The primary DDL commands include: 


  •  CREATE: It is used to create new databases, tables, and indexes.

  •  ALTER: This statement is used to modify the objects in the database.

  •  DROP: This statement is used to delete objects in the database, such as tables and indexes.

  •  TRUNCATE: It is used to DELETE all the records from the table permanently.


BEGIN TRAN;

TRUNCATE TABLE table_name;

rollback;


NOTE:  In MS SQL Server you can not rollback the TRUNCATE TABLE statement unless you use the TRANSACTION statement.


  • RENAME: This command is used to change the name of an existing table

Syntax for MS SQL Server:

EXEC sp_rename 'old_table_name', 'new_table_name'


We can show the renaming of a table using the below employee table.

Here in our cases, we have used sp_name a store procedure syntax to change the name of an employee table to employee_info. 


 

To check whether the name of the table is changed or not, it can be done by executing the SELECT statement:

So these key commands of DDL need to be learned. Let's learn about each of them in depth.

Creating Tables in SQL

Creating tables is one of the fundamental tasks in database design. The ‘CREATE TABLE’ statement is used to define a new table, specifying its name, column names, data types, and constraints. This statement is the building block for structuring data.

CREATE TABLE Statement in SQL

The Syntax is as follows:

CREATE TABLE [table_name]

 ( column_name1 data_type [NULL | NOT NULL], 

column_name2 data_type [NULL | NOT NULL], 

column_name3 data_type [NULL | NOT NULL], 

..., 

);



This is how you can CREATE TABLE statement to define a new table by specifying names, column names, data types, and constraints. This statement is essential for structuring data in a database.

  • Defining Columns in SQL

Each column in a table must have a name, data type, and other attributes that define its properties, such as whether it can store NULL values, its default value, and more.

 

  • Specifying Constraints in SQL

Constraints define rules that ensure data integrity. Common constraints include primary keys, foreign keys, unique constraints, and check constraints. They help maintain the quality and accuracy of the data.

Modifying Database Structures in SQL 

Changing data types, adding new columns, or modifying existing tables are common steps in SQL database structure modification. These modifications can be made with SQL using commands like ALTER TABLE.


The `ALTER TABLE` statement is used to make changes to an existing table's structure. You can add and also, modify, or drop columns and constraints. This statement is essential for evolving your database schema as your application requirements change.


  • Adding, Modifying, and Dropping Columns in SQL

Modifying columns allows you to adjust data types, constraints, and properties as needed. Adding and dropping columns can significantly impact how your application handles data.


  • Adding Column using ALTER TABLE in SQL

This statement is used to add a column. It adds a column to the existing table in a database, it is mandatory to also specify the datatype of it.


ALTER TABLE table_name

ADD COLUMN column_name datatype; 

 

          

This command adds a new column to an existing table. It's useful when you need to accommodate additional data without affecting the existing structure of the table.


  • Dropping Column using ALTER TABLE in SQL

This statement is used to drop a column. It removes a specific column from the existing table in a database

ALTER TABLE table_name

DROP COLUMN column_name;


If you no longer require a column in your table, you can remove it by using this command. This is particularly useful when you want to simplify your table or if the column is no longer needed for your data analysis.


  • ALTER/ MODIFY column using ALTER TABLE IN SQL

Adding a new column to an existing table can be done using this command. It is useful when you need to insert additional data without altering the current structure of the table. 


ALTER TABLE table_name

ALTER COLUMN column_name datatype;


 


  • Renaming Table using ALTER TABLE in SQL

You can use this command to change the name of a table.


ALTER TABLE table_name

RENAME TO new_table_name;


Defining Constraints in SQL

Constraints are essential for maintaining data integrity. They include:


  • Primary Key: Enforces uniqueness of a column or set of columns, ensuring no duplicate values.

  •  Foreign Key: Establishes relationships between tables, maintaining referential integrity.

  •  Unique Constraint: This is a constraint to a particular column that cannot accept repeated or duplicated values.

  •  Check Constraint: It is a constraint that is assigned to a particular column for extra validations. Check constraints are assigned with a condition, if the condition is true the value gets accepted, it is rejected.


Adding Constraints to Tables in SQL

You can add constraints during table creation or later using the `ALTER TABLE` statement.


ALTER TABLE table_name

ADD CONSTRAINT constraint_name constraint_type (column_name);


Modifying and Dropping Constraints in SQL

Changing or removing constraints is a common task as business rules evolve. SQL DDL provides the necessary commands for these operations.


ALTER TABLE table_name

DROP CONSTRAINT constraint_name;


In conclusion, we can change or remove a constraint by using the DROP statement, and also a new constraint to an existing column.

Indexes and Types of Indexes in SQL

Indexes are vital components in SQL Server that significantly enhance the performance of database queries. When designed and used effectively, they can drastically reduce the time it takes to retrieve data


The ‘CREATE INDEX’ statement allows you to define indexes on one or more columns, enabling faster data retrieval. SQL Server supports types of indexes, each designed for specific use.



Clustered Index

  • A clustered index establishes the physical arrangement of data within a table.

  • Each table is restricted to having just one clustered index, and it significantly influences the overall organization of the entire table. 

  • Although it is commonly created on the primary key column, it is not constrained to that specific column.

To create a clustered index, you can use the given syntax below:


CREATE CLUSTERED INDEX index1

ON database1.schema1.table1 (column1;


 Non-Clustered Index

  • A non-clustered index is an independent structure distinct from the data rows within a table. Each table can accommodate several non-clustered indexes. 

  • These indexes encompass a replica of the indexed columns along with a reference to the specific data row. 

  • They are well-suited for columns, frequently used in WHERE clauses or JOIN conditions.

To create a non-clustered index, you can use the given syntax below:


CREATE INDEX index1

ON schema1.table1 (column1);


Unique Index 

  • A unique index ensures the uniqueness of values in one or more columns.

  • It can be either clustered or non-clustered and serves to prevent the presence of duplicate values within the indexed columns.

To create a unique index, you can use the syntax below:


CREATE UNIQUE INDEX index1

ON schema1.table1 (column1 DESC, column2 ASC, column3 DESC);


Syntax to Modify and Drop Indexes in SQL

To drop a table, follow the syntax given below: 

The first syntax shows that if there exist any indexes with a particular name given that must drop from the table


DROP INDEX IF EXISTS index_name

ON database_name.table_name;


While the second syntax shows that any indexes from the provided table name will be dropped, you can drop as many indexes:   


DROP INDEX 

index1_name ON database_name.table_name,

index2_name ON database_name.table_name;


Just as with tables, you can modify or drop indexes using the `ALTER TABLE` and `DROP INDEX` statements.

Conclusion

The SQL DDL commands are vital for managing databases. It creates and modifies structures like tables, indexes, and constraints. Mastery of SQL DDL empowers administrators to design and maintain effective and reliable database systems, ensuring data accuracy, integrity, and performance. Whether creating a new database or evolving an existing one, SQL DDL is the tool of choice.