Reputation: 43
My table is this:
CREATE TABLE studio(
s_id int NOT NULL IDENTITY(1,1),
name varchar(30),
country varchar(15),
income_money float(53),
movies int,
CONSTRAINT primaryKeyStudio PRIMARY KEY(s_id)
)
I want to insert values into table studio, is it better this way:
INSERT INTO studio VALUES ('Walt Disney Studios','USA',45041000.99,50)
INSERT INTO studio VALUES ('Warner Bros Studios','London',30205000.76,60)
INSERT INTO studio VALUES ('Universal Studios','Hollywood',50235499.87,80)
Or this way:
INSERT INTO studio(name,country,income_money,movies) VALUES ('Walt Disney Studios','USA',45041000.99,50)
INSERT INTO studio(name,country,income_money,movies) VALUES ('Warner Bros Studios','London',30205000.76,60)
INSERT INTO studio(name,country,income_money,movies) VALUES ('Universal Studios','Hollywood',50235499.87,80)
Upvotes: 2
Views: 193
Reputation: 317
Your second approach is always better since if you miss inserting a row by mistake,the insertion just takes place,and hence difficult to find the missing value
Whereas in the second method the exact values to which you have to insert is known to you initially itself
Upvotes: 0
Reputation: 726967
The second way, i.e. this one
INSERT INTO studio(name,country,income_money,movies) VALUES ('Walt Disney Studios','USA',45041000.99,50)
where you list column names explicitly is better regardless of the identity column, for several reasons:
INSERT
to use additional columnsINSERT
- Someone adding a column in the middle would not break your existing code.Upvotes: 2
Reputation: 94499
The first method requires a value for each column to be provided. If a new column is added to the table all insert statements must be rewrote to include the new column. Statements wrote using the second approach would remain functional since they specify columns explicitly.
I would recommend using the second approach.
Upvotes: 0