Black Friday Deal : Up to 40% OFF! + 2 free self-paced courses + Free Ebook - SCHEDULE CALL
A collection of related data stored in a table format within a database is called a table. A table is a collection of data elements (values) in relational and flat file databases that employ a model of horizontal rows and vertical columns that can be identified by name; the unit at the intersection of a row and a column is the cell.
A table can have any number of rows despite having a predetermined number of columns. One or more values found in a particular column subset can be used to identify each row.The primary key is a specific selection of columns used to identify rows uniquely. We will learn about create table syntax for various database systems and how to create table in sql server.
This article will teach us about various MySQL and SQL Server methods for creating tables.
Core Table |
Lookup Table |
List Table |
Detail Table |
Cross Table |
Your database has a "main" table like this one. The database simply wouldn't exist in the first place without these. |
A "reference" table with categories, types, keywords, tags, or other "classification" labels, is called a Lookup table. These categories typically signify a limited scale. Some examples are quality, priority, severity, frequency, etc. |
Similar to a Lookup table, this is a different kind of "reference" table with a longer list of records and frequent additions. Therefore, "List" Some examples are Reasons, Objectives, Genres, and Other Information. It might be referred to as the "Growable. Lookup" table. |
Specific items subordinate to the records in a Core table are contained in a Detail table. Some examples are Request Subtleties, Occasion Logs, Episode Reports, Client Remarks, etc. |
Only establishing a many-to-many relationship between two other tables is the purpose of a Cross-Reference table. While not required, these two additional tables are typically Core tables. |
The syntax for creating table in MySQL is as follows:
Create TABLE table_name ( Column1 datatype, Column2 datatype, Column3 datatype …… )
The column parameters specify the name of the column in the table.
Following are a few MYSQL create table syntax examples.
Example 1
The following example creates a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
The PersonID column is of type int and will hold an integer.
The LastName, FirstName, Address, and City columns are of type varchar and will hold characters; the maximum length for these fields is 255 characters.
The empty "Persons" table will now look like this:
Example 2
A copy of an existing table can also be created using CREATE TABLE.
The new table gets the same column definitions. All columns or specific columns can be selected.
If you create a new table using an existing table, the new table will be filled with the existing values from the old table.
The syntax for creating table in mysql is as follows.
CREATE TABLE new_table_name AS SELECT column1, column2,... FROM existing_table_name WHERE ....;
The following SQL creates a new table called "TestTables" (which is a copy of the "Customers" table):
Example is
CREATE TABLE TestTable AS SELECT customername, contactname FROM customers;
The SQL Server Create Table Syntax is as Below
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).
Following are a few examples of Create Table function
Example 1
The following example creates a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
The PersonID column is of type int and will hold an integer.
The LastName, FirstName, Address, and City columns are of type varchar and will hold characters; the maximum length for these fields is 255 characters.
The empty "Persons" table will now look like this:
Example 2
We will create a table from another table.
A copy of an existing table can also be created using CREATE TABLE.
The new table gets the same column definitions. All columns or specific columns can be selected.
If you create a new table using an existing table, the new table will be filled with the existing values from the old table.
The syntax is as follows.
CREATE TABLE new_table_name AS SELECT column1, column2,... FROM existing_table_name WHERE ....;
The following SQL creates a new table called "TestTables" (which is a copy of the "Customers" table):
CREATE TABLE TestTable AS
SELECT customername, contactname FROM customers;
You can also create a table using SQL Server Management Studio. Following are the steps.
Step 1
Log in to SQL Server Management Studio
Step 2
The following screen appears. Select the database in which you want to create the table. Expand it. Select Table and right-click on it. Select new and table.
Step 3
The following screen appears. Provide the necessary column details and datatypes and click on save. The table will be created.
In SQL Server, a database table that only exists for a short time on the server is called a temporary table. A temporary table stores a subset of the data in a normal table for a predetermined time.
When you have a lot of records in a table but only frequently need to work with a small subset of them, temporary tables are a good option. In this case, you can filter the data once and save it in a temporary table instead of filtering it multiple times to get the subset.
Permanent and temporary tables are nearly identical. When the last connection to the query window that created the temporary table is broken, temporary tables are created in TempDB and are automatically deleted. We can store and process intermediate results with Temporary Tables. When we require temporary data storage, temporary tables come into play. Data is an integral part of businesses, and there’s great demand for administrators in SQL; go through our SQL DBA career path if you want to set yourself up for this role.
There are two types of temporary tables
Local Temporary Table |
Global Temporary Table |
Only the session that created the Local Temp Table can access it. When the connection that created it is closed, it is deleted by default. A single "#" is used as a table name prefix when creating a Local Temporary Table. Additionally, the "DROP TABLE #EmpDetails" query allows the user to remove this temporary table. The name of the table will have random numbers added to it. When the stored procedure is finished running, the Temporary Table created within it is automatically deleted. |
Global Temporary Tables are Dropped when the last connection referencing the table closes. They are visible to all connections. A distinct table name must accompany the global table name. The Table Name will not include any random numbers after it. Add the "##" image before the table name to make a Worldwide Impermanent Table. |
The mysql table creation syntax for creating a temporary table is as follows:
CREATE TABLE #name_of_temp_table ( column_1 datatype, column_2 datatype, column_3 datatype, . . column_n datatype )
We have learned different ways of creating a table. Now we will learn different ways to delete a table.
Log into SQL Server.
To delete a table using transact SQL, you have to execute the following command
UseDrop table
Here table name is the table's name. The database name is the name of the database where the table belongs.
1) Log into SQL Server Management Studio
2) Select the desired database and expand.
3) Go to tables.
4) Select the table you want to delete and right-click on it.
5) Select delete.
Over the last few paragraphs, we have learned about different ways of creating a table in MySQL and SQL Server. We have also learned about the creation of temporary tables. How to delete a database table is also covered in this write-up. This will give the reader enough interest to study the table creation subject further. Or else you can also enroll in an online SQL server training course and shape your ever-growing SQL career.
SQL Testing Training
What is Schema in SQL With Example: All You Need to Know
Data Definition Language (DDL) Commands in SQL
What does a Database Administrator do? A Detailed Study
Database Files-Heart of SQL Server Database
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Download Syllabus
Get Complete Course Syllabus
Enroll For Demo Class
It will take less than a minute
Tutorials
Interviews
You must be logged in to post a comment