Inserting Values into a Table In SQL
In this previous blog post, we discussed how to create a database and a table with initialized columns by using a script. We will continue that post and now insert data into the student table. The first approach is entering in one row at a time. This is the first line that we see in the code box. We use the command INSERT INTO, followed by the table name where we can specify, in paretheses, the attributes that we want to change. The next command is VALUES. This is where we assign values to our row. But, this can be incredibly painful to do at a large scale. Starting on line four, we will see a more efficient way of entering in values. Instead of creating one parenteses pair after the VALUES command, we will chain together multiple lines of input, and have the convenience of writing this in a script so any errors that occur can easily be fixed.
SQL
INSERT INTO students(student_fname, student_lname, address, gpa) VALUES(
'Steve', 'Smith', '123 Somewhere Lane Somewhere, Country', 3.5
);
INSERT INTO students(student_fname, student_lname, address, gpa) VALUES(
'Steve', 'Smith', '123 Somewhere Lane, SomeWhere, SomeState', 3.5
);
INSERT INTO students(student_fname, student_lname, address, gpa) VALUES
('Sam', 'Smith', '123 Somewhere Lane, SomeWhere, SomeState', 2.3),
('Angel', 'Devil', '123 Somewhere Lane, SomeWhere, SomeState', 3.3),
('Mary', 'Abbot', '123 Somewhere Lane, SomeWhere, SomeState', 4.0),
('Steve', 'Johnson', '123 Somewhere Lane, SomeWhere, SomeState', 3.4),
('John', 'Elderberry', '123 Somewhere Lane, SomeWhere, SomeState', 2.5),
('Elaine', 'Huff', '123 Somewhere Lane, SomeWhere, SomeState', 2.0);
Code copied
In conclusion, chaining together our inputs into a table is far less time consuming than if you were individually input every row. Hope this helps.