Image this, you’ve got created a big desk in a database to control worker information. The HR and senior managers ask you to incorporate columns comparable to employee_id, first_name, last_name, gender, email_id, and deal with. You already effectively created the desk. However now, HR comes and says that they would like you so as to add column in SQL to the already created Worker desk. What is going to you do?
There are two choices to be had; you’ll take backup, drop the desk, and create a brand new one or upload some other column to the desk. The primary possibility isn’t a sensible choice as it’ll devour numerous time, particularly if the desk has huge information. Therefore, programmers don’t seem to be more likely to take that possibility leaving the simpler selection so as to add some other column at disposal. For this, you’ll use the ALTER TABLE command. This command means that you can upload, alter, and delete a number of issues together with, constraints, perspectives, and columns in SQL.
Methods to Create an SQL Desk?
Right here, you’ll discover the techniques so as to add column in sql to an present desk right here. Therefore, earlier than beginning with the rest, create a desk the usage of the CREATE TABLE command and populate it the usage of the INSERT INTO remark. In spite of everything, you’ll use the SELECT remark to view the desk. For this case, you will have to create a desk named ‘Worker’ with E_ID, E_Name, E_Gender, and E_Email_Id columns.
CREATE TABLE Worker(
E_ID INT PRIMARY KEY,
E_Name VARCHAR(25),
E_Gender VARCHAR(1),
E_Email_Id NVARCHAR(20) UNIQUE
);
INSERT INTO Worker VALUES (1, ‘George’, ‘M’, ‘ge.com’);
INSERT INTO Worker VALUES (2, ‘David’, ‘M’, ‘da.com’);
INSERT INTO Worker VALUES (3, ‘Florina’, ‘F’, ‘fl.com’);
INSERT INTO Worker VALUES (4, ‘Rachel’, ‘F’, ‘ra.com’);
SELECT * FROM Worker;
Output:
As you’ll see, the code discussed above creates the ‘Worker’ desk with 4 columns and 4 rows. You’ve got set the ‘E_ID’ column as a number one key and the ‘E_Email_Id’ column as distinctive.
NOTE: You’ll have to use a Microsoft SQL server right through.
What’s an SQL ALTER TABLE Remark?
As discussed in the past, the ALTER TABLE permits you to alter, delete, and upload columns in SQL. It additionally lets you do the similar with constraints, indexes, and perspectives. However for this text, you’ll stay the limitations, indexes, and perspectives at bay and pay attention to find out how to upload a column in SQL and paintings with it the usage of the ALTER TABLE remark.
Methods to Upload a Column in SQL?
So as to add columns in SQL to an present desk, you’ll use the ALTER TABLE command and insert the column identify and outline. The syntax for a similar is as follows:
ALTER TABLE table_name
ADD col_name data_type;
Now, use the desk and insert a brand new column, ‘E_LastName,’ to the already present ‘Worker’ desk.
ALTER TABLE Worker
ADD E_LastName VARCHAR(20);
SELECT * FROM Worker;
Output:
As you’ll see within the output, the above code provides a brand new column, ‘E_LastName,’ to the desk and helps to keep the worth NULL as you haven’t added the values for it to the desk but.
Methods to Upload Columns in SQL (More than one)?
With the similar ALTER TABLE command, you’ll briefly upload a couple of columns to an present desk via a unmarried ALTER TABLE command. Right here’s the syntax so as to add columns in SQL.
ALTER TABLE table_name
ADD col_1_name data_type
col_2_name data_type
…
col_n_name data_type;
Now that the syntax so as to add columns in SQL, use it so as to add two columns, ‘E_Address’ and ‘E_Salary,’ on your already present ‘Worker’ desk.
ALTER TABLE Worker
ADD E_Address NVARCHAR(30),
E_Salary VARCHAR(10);
SELECT * FROM Worker;
Output:
Methods to Regulate a Column in an Current Desk?
The ALTER TABLE command additionally means that you can alter an present column. The syntax for editing a column is:
ALTER TABLE table_name
ALTER COLUMN col_name col_description
Within the syntax discussed above, you’ll point out the adjustments within the position of col_desription.
For this case, you’ll alter the E_Salary column. If in case you have spotted, this case has used VARCHAR data_type for this column. However a wage will also be both INT or decimal. Therefore, you’ll alter the data_type for that column.
ALTER TABLE Worker
ALTER COLUMN E_Salary INT;
SELECT * FROM Worker;
Output:
Chances are you’ll now not have the ability to witness any distinction within the above output. However upload an integer quantity to the primary row the usage of the UPDATE remark and notice if it accepts the information. If the column used to be changed, it’ll settle for the information and insert it into the row.
UPDATE Worker
SET E_Salary = 50000
WHERE E_ID = 1;
SELECT * FROM Worker;
Output:
As you’ll see, it up to date the information because it changed the column to be of INT information kind. Now, attempt to insert VARCHAR information to peer the effects.
UPDATE Worker
SET E_Salary = ‘Hello’
WHERE E_ID = 2;
SELECT * FROM Worker;
Output:
As you’ll see, the question failed because the server needed to convert the VARCHAR information to INT so as to add within the row, however it failed.
Methods to Drop a Column in SQL?
Like the way you upload columns in SQL, you’ll additionally drop it the usage of the ALTER TABLE command. The syntax for losing a column is as follows:
ALTER TABLE table_name
DROP COLUMN col_name;
This will likely drop an already present desk from the desk. Now, use it on your ‘Worker’ desk to drop the ‘E_Address’ column.
ALTER TABLE Worker
DROP COLUMN E_Address;
SELECT * FROM Worker;
Output:
Methods to Rename a Column From an Already Current Desk?
When you upload columns in SQL, you’ll additionally rename them. However you can not do that with the ALTER TABLE command. As a substitute, it’s important to use the sp_rename command for this. The syntax to rename a column in SQL is:
sp_rename ‘table_name.old_col_name’, ‘new_col_name’, ‘COLUMN’;
Now that the syntax, it’s time to use it and rename the ‘E_LastName’ column out of your ‘Worker’ desk to ‘E_LName.’
sp_rename ‘Worker.E_LastName’, ‘E_LName’, ‘COLUMN’
SELECT * FROM Worker;
Output:
Even if the sp_rename command is helping rename the column, Microsoft recommends losing the column and re-adding it with a brand new identify to stop breaking the saved procedures and scripts.
Methods to Rename a Desk?
Very similar to a column, you’ll additionally rename a desk. Alternatively, like columns, Microsoft additionally recommends losing and recreating a desk. Regardless, the syntax of renaming a desk is given beneath.
sp_rename ‘old_table_name’, ‘new_table_name’;
Now, use the syntax and rename your ‘Worker’ desk to ‘Workers.’ You will have to then use the SELECT remark with the desk identify as ‘Workers’ to substantiate the identify alternate.
sp_rename ‘Worker’, ‘Workers’;
SELECT * FROM Workers;
Output:
What are the Barriers and Restrictions to Upload Columns in SQL?
Whilst you use the ALTER TABLE command so as to add columns in SQL, it’ll, through default, upload the columns to the top of the desk. This can be a important limitation as it’s all the time excellent to peer the columns of a desk in the most well liked ordering. Alternatively, it’s simple to conquer this limitation through the usage of the SQL Server Control Studio. If in case you have ALTER permissions within the server studio, practice the stairs beneath to switch the ordering of a column. For this case, you’ll first drop the ‘E_LName’ column after which upload it with the GUI.
ALTER TABLE Workers
DROP COLUMN E_LName;
Output:
Now, upload the ‘E_LName’ the usage of the GUI and position it beside the ‘E_Name’ column.
- In finding the desk within the object explorer and enlarge it, click on on columns, and make a choice New Column.
- Upload the column identify, information kind, and different houses you need within the column.
- Double click on at the arrow at the excessive left and drag the brand new column anyplace you need.
Alternatively, this will likely most effective paintings if it has accredited you to delete and re-create the desk. That’s as a result of regardless of simply growing a brand new column, the server will in any case delete and recreate the desk. Thus, ordering columns is very beneficial to be achieved whilst growing the desk itself.
Achieve experience in the most recent Trade analytics gear and methods with the Put up Graduate Program in Trade Research. Join now!
Conclusion
On this article, you discovered the entirety about find out how to upload columns in SQL, at the side of a easy instance. You’ve got additionally noticed find out how to alter, rename, and drop a column in an present desk the usage of the ALTER TABLE command. You’ll be able to now use the ALTER TABLE command so as to add columns on your tables on your database.
Check out various things to get a greater working out of the idea that and the way it all works. Alternatively, if you wish to get an entire take hold of in this and different basic ideas, take Simplilearn’s SQL Certification Coaching Route. The path comes with limitless get entry to to self-paced finding out fabrics and hands-on implemented finding out tactics for various SQL servers. To place it merely, the path let you excel in database control and construct a a success occupation within the box.
Have any questions for us? Depart them within the feedback segment of this text, and our professionals gets again to you at the identical, once conceivable!
supply: www.simplilearn.com