SQL insert and id with auto_increment - sql

Hey ya'll I have this insert statement here
INSERT INTO persons VALUES (16,'First Name',NULL,NULL,NULL,2,0,now(),NULL,NULL);
it says that the number of columns do not match because the last column is for the id which is auto incremented. do I have to put in an id value?
Thanks,
J

You should not include an Auto-increment column in your insert.
It is also best practice to put the column names after the table name. This helps to make the query cleaner and easier to read & maintain.
INSERT INTO persons(Column1, col2, ...)
VALUES (16, 'First Name', ...)

Just don't include that field
INSERT INTO persons VALUES (16,'First Name',NULL,NULL,NULL,2,0,now(),NULL);

Related

When inserting a tuple into a table in SQL, does the order of the attributes in the schema matter?

ie. if Students(id,name,major,gpa) is the schema, are the following 2 SQL queries valid?
INSERT
INTO Students(id,name,major,gpa)
VALUES (123, Joe, CPSC, 3.0)
INSERT
INTO Students(name,id,major,gpa)
VALUES (Mike, 505, CPSC, 4.0)
Yes both of the query are correct. And the Values will insert to the column in whatever format your INSERT INTO t_Tablename(column1,column2) is made.
Just to make a check you can do (Assume id is int and Name is varchar)
INSERT INTO Students(id, Name)
VALUES("testString", 1)
which will give you an error, since you're trying to insert a string value to id which accepts an int and vice versa
Yes, the two following queries are Valid. Since you keep the order of your attributes values as described in Students(column1,column2), the query will be ok.
As long as the order in the INTO expression equates to the order in the VALUES expression, that is all that is required. The column list in the INTO expression determines both which columns you're specifying values for and the order the values will appear in the VALUES expression. The first column gets the first value, the second gets the second value, and so on.
If you neglect to specify the column list in the INTO expression, most RDBMSs will assume that the columns in the VALUES expression are in the ordinal order of columns in the table itself. That is, if you do this:
INSERT INTO Students
VALUES (123, Joe, CPSC, 3.0)
Then you better have done something like this:
CREATE TABLE Students (id int, name varchar(30), major varchar(30), gpa numeric(3,2))
That's the only time that the order of columns in the table itself actually matters to an INSERT statement.

SQL: Insert multiple row with common columns

I have a table into which i will insert and it has 4 columns.
While inserting 3 columns will be same and the other one column will be different for each and it will be taken from another table.
How could i do that?
For exmaple;
INSERT INTO sendMsg (Type,Name,SenderName,Message) values(4, 'john','Mike','Hi, blabla')
i will insert same message also for Bob, instead of john.
and the names which i will send are contained in Names table.
Thank you.
Use a select statement to build up your insert. Something like this could work (as you didn't provide more details):
INSERT INTO sendMsg (Type,Name,SenderName,Message)
SELECT 4, "name" ,'Mike','Hi, blabla' FROM anothertable
-- WHERE ....
Column names are in ", so don't be confused. It's to ensure difference between string and database object.
Inside optinal WHERE you could do maybe something like
WHERE name in ('Bob', 'John', ...)
or whichever algorithm you need to determine the names.

Understanding the populating of Tables

I'm trying to understand how to populate Tables and have been given some code by my professor. I can't tell why these two statements are different in their Syntax:
Q1:
Insert into SecurityType (SecurityTypeCode, SecurityTypeDesc)
Values ('STO', 'Stock');
INSERT INTO [Country] ([CountryId], [CountryCode], [CountryDesc]) VALUES (-1, N'NOT SPECIFIED', N'Not Specified')
That is after looking at the two statements above, can we write the first statement as
Insert into SecurityType ([SecurityTypeCode], [SecurityTypeDesc])
Values ('STO', 'Stock');
Q2:
My professor says "You need to set Idenitity insert ON in order to insert a value into an identity column". I'm unclear as to what an "identity column" is.
Thanks
1) Column names can be wrapped in brackets, this is done if your column name is a reserved key word or your column name has spaces.
So you can have the brackets around the column names and it is same as first statement.
2) Identity column values automatically generated and you use this syntax to create such a column
CREATE TABLE Employee
(
id int IDENTITY(1,1)
SQL server doesn't allow you to insert a value for this identity column, in case you want to insert a default value or some values , you can set identity insert ON.

sql insert into a table with computed columns

I have a table with 4 columns, say COLA,COLB,COLC,COLD. COLC and COLD are computed columns.
Suppose I want to insert a row into table, should the query be something like
insert into Table (COLA,COLB,COLC,COLD) values (1,2,'','')?
I know I can't insert into computed columns. But how can I add a row and keep the default computed columns as they are?
Thanks for any advice!
try this
INSERT INTO TABLE (COLA,COLB) values (1,2);
you dont need to provide the values even blanks for computed column. They get calculated automatically
Just specify the columns you want to insert values into:
insert into Table (COLA,COLB) values (1,2)
Just don't specify the calculated columns:
insert into Table (COLA,COLB) values (1,2)
Thank you all! I just got that out too! Previously I got errors that number of col don't match, because I missed a a column.

Adding auto-incremented values to a table with one column

I need to create a table that basically keeps a list of indices only. Therefore I've created a table with just one, auto-incremented column called 'id'. However, I can't seem to implicitly add auto-incremented values to this table.
I know that usually when you have such a column in a table (with more than just this column) you can do:
INSERT INTO TABLE (col1, col2 ...) VALUES (val1, val2 ...)
And if you don't specify the auto-incremented column, it would automatically get a value. However, things like:
INSERT INTO TABLE () VALUES ()
INSERT INTO TABLE
INSERT INTO TABLE ()
etc. all produce an error on my single-columned table. Can anyone offer a solution?
Thanks.
p.s. I'm using Sqlite, in case it matters.
Try this
INSERT INTO dbo.Table DEFAULT VALUES
See this answer:
Previous answer
Try the following:
INSERT INTO YOUR_TABLE(YOUR_ID) VALUES (NULL);