Grab Deal : Flat 20% off on live classes - SCHEDULE CALL
SQL or Structured Query Language is used to create, manage, and retrieve the data from relational database systems. Now if you want to know what is a relational database system then it is associated with storing and retrieval of data from the tables of a relational database system. There are a few queries in SQL that are used to store, retrieve, and update the information in the SQL and here in this blog for “How to Insert Date in SQL Server”, we are going to discuss the SQL insert query in detail. Though you may need all other queries to create and maintain the SQL tables, here we will discuss the insert query/command that is a basic statement and used to insert data into the database. It is also used to add date in the SQL.
Firstly, here in this blog, we will discuss the relational database and then the syntax and various versions of this query along with examples.
Relational database tables in which the data is stored are related to each other. Even the tables of the relational database are called relations. Like a table or relation named Employee is shown below:
For any relation or table of the database, some standard terms or terminologies are used by the programmers or database administrator. We will discuss them in our next section:
Before we dive deep into the topic, let us have a quick look at the database languages first that are frequently used by database developers to store data with the help of different queries in SQL Server.
Read: What Is SQL Candidate Key? Difference between Primary Key & Candidate Key
Now, here we will discuss the insert query of SQL database in detail with an example. In order to understand this query, you must be familiar with the SQL queries like Create or Select.
Get all SQL skills in-depth with our online SQL training classes.
This is quite clear the database systems are used to store data in tables. Table data is usually supplied either by application programs or other tables or directly by other tables. These data sources are used to supply data directly to the table. Here in SQL Insert command is used to insert or add data into the table. With the Insert command, a new row is created into the table that can store data.
Learn SQL Server in the Easiest Way
INSERT INTO table_name (columm1, column2, column3, - - - - -) VALUES (value1, value2, vaue3, - - -); Here in this syntax various keywords and their details are listed below:
Here, in the Insert query of SQL, there are some rules and norms that should be followed. They are listed below:
Sign up for a demo class today and learn practical SQL skills.
SQL Server Training & Certification
Let's take an example of the following table and will use the INSERT command to add values. Also, We will discuss how to insert date in SQL Query within a table.
Read: How to Add A New Column to a Table in SQL?
Table: Student(S_id, DoB. Gender, Contact_No, City) (Let the table structure is like this) Let's insert a new row in this table, then below syntax will be used for this purpose: INSERT INTO ‘STUDENT’(‘S_id’, ‘Gender’,’Contact_No’, City) VALUES (‘S_01’, ‘Male’, 098987898, Delhi);
When the above command will be executed than just except the DOB all values will be inserted in the table and there a new row will be formed. Here the values are inserted as numeric values are provided without quotes and string values are provided with the quotes. Here, in the above query, the contact number is a numeric value and is not enclosed within a single quote so here the zero will not be stored, while if you want to store zero along with the value then it is good to enclose the numeric value within a single quote. Here, you can also change the order of column names in Insert query that will not have any impact in value insertion, you must here need to be careful about the order in which the values are inserted and the name of the column that is provided with the query statement. As here in this insertion, DOB value is not provided while creating the new row, so here NULL value will be inserted in this place. Now let us insert a new row in the same table along with DOB value. Here, make sure that the value of date must be enclosed within the single quote and in the format ‘YYYY-MM-DD’.
INSERT INTO ‘STUDENT’ (‘S_id’, ‘DoB’, ‘Gender’,’Contact_No’, City) VALUES (‘S_02’, ‘1985-12-09’, ‘Male’, ‘098987899’, Delhi);
If you are supplying the values for all of the existing columns of the table then here you can skip the query part in which you have specified the name of each column. This insertion is being done from the user but table data can also be imported from another table. In that case, how the values will be inserted is discussed below.
INSERT command of SQL is used to insert data into a table, this data can also be sourced from another table. Here, the syntax for this query will be like:
INSERT INTO table_name SELECT * FROM source_table;
Here let us suppose that we have two tables one is Student and other is Source_Data, then the below-listed command will be used to insert data to the Student table from Source_Data table:
Read: Introduction to SQL Injection and Attacks
INSERT INTO ‘Student’ SELECT * FROM ‘Source_Data’;
Here, when the above-listed query will be executed then all values from the table Source_Data will be inserted to Student table. In order to execute the above query table structure of both the tables that is Student and Source_Data must be the same. In case if the table structure is different for both the tables then you will have to specify column names in this query that will insert the corresponding values from one table to another.
INSERT query statement of SQL can perform below-listed operations:
The values in a table can also be inserted from another table in a particular table for that you may have to merge two or more commands
Read: Difference Between Clustered and Non-Clustered Index in the SQL
In this way, you can use the SQL Insert command to add new values in the database tables. This command is mostly used alone, but scripts can also be used to use this command or SQL statement.
SQL Server Training & Certification
In this blog, we have discussed the MySQL Insert Date command in-depth. Now, you have a sound idea of how to insert data in SQL Server. There are many other DDL and DML statements in SQL that can be used to maintain and handle the relational database. You can use the database as per your requirement and for that, you need not use them separately. You can use them on existing and new tables as per the requirement.
Here, we have discussed the command with examples and the use of the command is not limited, you can use it with many tables. Data insertion is one of the basic requirements for any SQL table you can use them to add new rows or values to the table.
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
MS SQL Server
Receive Latest Materials and Offers on SQL Server Course