SQL / SQL Data Definition
Creating and Modifying Tables in SQL
This tutorial will teach you how to create and modify tables in SQL. You'll learn to structure your data effectively in a database.
Section overview
5 resourcesCovers creating, modifying, and deleting database structures.
Creating and Modifying Tables in SQL
1. Introduction
In this tutorial, we will learn how to create and modify tables in SQL. SQL or Structured Query Language is used for managing and manipulating databases. Specifically, we will focus on the creation and modification of tables, which are a vital part of any database.
By the end of this tutorial, you will be able to create tables, modify existing tables by adding or deleting columns, and changing the data type of columns in SQL.
Prerequisites: Basic understanding of databases and SQL syntax.
2. Step-by-Step Guide
2.1 Creating Tables
To create a table in SQL, we use the CREATE TABLE statement. The syntax is as follows:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);
Here, table_name is the name of the table you want to create. column1, column2, etc. are the names of the columns. datatype defines the type of data that can be stored in each column (e.g., INT, VARCHAR, DATETIME), and constraint defines any restrictions on the data (e.g., NOT NULL, UNIQUE).
2.2 Modifying Tables
To modify an existing table in SQL, we use the ALTER TABLE statement. The syntax is as follows:
- Adding a column:
ALTER TABLE table_name
ADD column_name datatype constraint;
- Deleting a column:
ALTER TABLE table_name
DROP COLUMN column_name;
- Changing the data type of a column:
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
3. Code Examples
3.1 Creating a table
Let's create a table named Employees with three columns: ID, Name, and DOB.
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
DOB DATE
);
In this code:
IDis of typeINTand is the primary key.Nameis of typeVARCHARwith maximum length 100 and cannot be null.DOBis of typeDATE.
3.2 Adding a column
Let's add a column named Email to the Employees table.
ALTER TABLE Employees
ADD Email VARCHAR(255);
In this code, we're adding a new column Email of type VARCHAR with a maximum length of 255.
3.3 Deleting a column
Let's delete the DOB column from the Employees table.
ALTER TABLE Employees
DROP COLUMN DOB;
This code drops the column DOB from the Employees table.
3.4 Changing the data type of a column
Let's change the data type of the ID column to BIGINT.
ALTER TABLE Employees
ALTER COLUMN ID BIGINT;
This code changes the data type of the ID column from INT to BIGINT.
4. Summary
In this tutorial, we learned how to create and modify tables in SQL. We learned how to add and delete columns from a table and how to change the data type of a column.
The next step in learning SQL would be understanding how to insert, update, and delete data from these tables. You might also want to explore how to create relationships between different tables.
5. Practice Exercises
Try out the following exercises to practice what you've learned:
- Create a table
Studentswith columnsStudentID,FirstName,LastName,DOB, andEmail. - Add a new column
PhoneNumberto theStudentstable. - Change the data type of
StudentIDfromINTtoVARCHAR. - Delete the
Emailcolumn from theStudentstable.
Solutions:
- Creating the
Studentstable:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
DOB DATE,
Email VARCHAR(255)
);
- Adding
PhoneNumbercolumn to theStudentstable:
ALTER TABLE Students
ADD PhoneNumber VARCHAR(15);
- Changing the data type of
StudentID:
ALTER TABLE Students
ALTER COLUMN StudentID VARCHAR(50);
- Deleting the
Emailcolumn:
ALTER TABLE Students
DROP COLUMN Email;
Keep practicing and exploring more about SQL!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article