I have created the following table for apartments and used auto-increment on column ApartmentID. I thought once I set this auto-increment to 101, it would automatically increase in the rows as I insert, however I just get a null value.
My Create table code is;
CREATE TABLE Apartments
(
ApartmentID smallint,--AUTOINCREMENT = 101,
Occupier smallint NULL default 0,
Rent money default 0,
CurrentOccupier smallint NULL,
)
Because I have auto increment included in my create table, I insert values as follows;
INSERT INTO dbo.Apartments (Occupier, Rent, CurrentOccupier)
VALUES
('1','400','21'),
('0','450','90'),
The following is my result;
ApartmentID Occupier Rent CurrentOccupier
NULL 1 400.00 21
NULL 0 450.00 90
Concerned that the ApartmentID column was showing NULL instead of 101, 102, 103 etc and thinking this was to do with the column property null, I dropped the table an recreated it at follows;
CREATE TABLE Apartments
(
ApartmentID smallint NOT NULL,--AUTOINCREMENT = 101,
Occupier smallint NULL default 0,
Rent money default 0,
CurrentOccupier smallint NULL,
)
The result / message I got was
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ApartmentID', table 'Apartment_2.dbo.Apartments'; column does not
allow nulls. INSERT fails.
I would like to work with the first table I created, if I could get the values 101, 102 etc to show up in the column ApartmentID, when I run the query select * from Apartments instead of the word null.
Any / advice help would be appreciated. Again it may be a simple error, but being new I do not recognize my error. Also I have included -- infront of auto increment, as I have seen that online.
Thanks
Josie
In SQL Server you declare the column as IDENTITY not autoincrement. The syntax to declare such a column seeded at 101 is
CREATE TABLE Apartments
(
ApartmentID smallint NOT NULL IDENTITY(101,1) PRIMARY KEY,
Occupier smallint NOT NULL default 0,
Rent money NOT NULL default 0,
CurrentOccupier smallint NULL
)
I assumed that likely you will want this column to be the primary key. Remove those keywords if this is not the case.
Please try the below code. This should help you.
CREATE TABLE Apartments ( ApartmentID smallint IDENTITY(101,1) NOT NULL, Occupier smallint NULL default 0, Rent money default 0, CurrentOccupier smallint NULL);
Related
I have two tables:
CREATE TABLE project.teacher
(
PROFESSOR_Codigo SMALLINT IDENTITY( 100, 1),
birth DATE NOT NULL,
phone VARCHAR(15)
);
CREATE TABLE project.student
(
STUDENT_Codigo IDENTITY( 1, 1),
birth DATE NOT NULL,
phone VARCHAR(15)
);
When I add a student to the student table it will increment 1 from the number 1, and stay (1, 2, 3, 4, 5, 6, ...)
In the teacher table I already have data entered, and start with a value of 100 in the identity column, but when I add 1, it gets identity 1, not 101
I've tried everything, scope_identity(), ##identity, but I couldn't! Does anyone have any ideas?
What I do to insert a row into the teacher table:
INSERT INTO project.student
VALUES ('1998-05-08', '963597461');
INSERT INTO project.teacher
VALUES ('1994-05-09', '968413692');
try
dbcc checkident(teacher, noreseed)
this will return the current identity value of the ident column. your create statement is fine so maybe something went wrong on the on create?
Say I have a table that looks like:
ID Name Age Co
1 Adls 15 US
2 sldkl 14 FR
3 sldke 16 UK
4 sldee 17 IN
I want to add values to the table and have the ID incremented. ID is the primary key, and I set is Identity under column properties to 'Yes' and identity increment to 1.
So basically, I am doing:
Insert Into TableName(Name, Age, Co)
Values(slkdje, 19, CH)
(sldjklse, 20, AU)
(slfjke, 12, PK)
But, I am getting errors that the primary key is null, and therefore this operation in invalid. How would I add the values, but get the primary key values to increment?
here is a great example for what you want here
here is also a sql query that is copy and paste that will show my example.
create table #temp(
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
)
insert into #temp(LastName) values('billy'),('bob')
select * from #temp
drop table #temp;
hope this helps dude.
Trying to insert dates into my SQL Server table, I am using the format YYYYMMDD
I'm getting this error
Msg 241, Level 16, State 1, Line 201
Conversion failed when converting date and/or time from character string
This is the table I created:
CREATE TABLE SupplierOrders
(
supplierOrderID CHAR(5) NOT NULL,
orderDate DATE NOT NULL,
orderTotal NUMERIC(20,1) NOT NULL,
status VARCHAR(25) NOT NULL,
orderReceiveDate DATE,
orderPaymentDate DATE ,
paymentRefNo DATE ,
quotationID CHAR(6) NOT NULL UNIQUE,
PRIMARY KEY (supplierOrderID),
foreign key (quotationID)
references QuotationProduct(quotationID)
on update cascade on delete no action
);
and this is the data I'm trying to insert
INSERT INTO SupplierOrders
VALUES('s9021', '20150101', 10, 'delivered', '20150101', '20150101', 'po900', 'qo1021');
What am I doing wrong?
The problem is this column in your table.
paymentRefNo DATE ,
I think maybe you meant to make it a char or varchar.
You are trying to insert 'po900' into it.
i am not sure if i could use conditional statement while creating new columns.
Code:
create table Employees(
Emp_ID int primary key identity (1,1),
Hours_worked int,
Rate int default '')
/*Now here in default value i want to set different rates depending upon hours worked. like if hour worked is greater than 8 then rate is 300, if hours worked is less than 8 hour the rate is 200.) How to write this as a Default value in sql server 2008.
My second question is:
Why i get error if i write like this,
create table tbl_1(
col_1 varchar(max) unique
)
The error is
Column 'col_1' in table 'tbl_1' is of a type that is invalid for use as a key column in an index.
Msg 1750, Level 16, State 0, Line 1
Regards
Waqar
you can use COMPUTED Column, http://msdn.microsoft.com/en-us/library/ms191250.aspx
create table Employees(
Emp_ID int primary key identity (1,1),
Hours_worked int,
Rate as (case when Hours_worked > 8 then 300 else 200 end) persisted )
The default value cannot refer to any other column names. So the "default" value of Rate won't know the value of Hours_worked. You could handle it with a trigger or whatever is doing the actual inserting could contain this logic.
http://msdn.microsoft.com/en-us/library/ms173565(v=sql.100).aspx
You cannot but a UNIQUE constraint on a VARCHAR(MAX) field.
CREATE TABLE Type1
(
TypeID TINYINT NOT NULL IDENTITY(1,1),
TypeName VARCHAR(20) NOT NULL,
Speed VARCHAR(10) NOT NULL
CONSTRAINT TypeID_pk PRIMARY KEY (TypeID)
);
CREATE TABLE Splan
(
PlanID TINYINT NOT NULL IDENTITY(1,1),
TypeID TINYINT NOT NULL,
PlanName VARCHAR(20) NOT NULL,
Quota SMALLINT NOT NULL
CONSTRAINT PlanID_pk PRIMARY KEY (PlanID)
CONSTRAINT TypeID_fk FOREIGN KEY (TypeID) REFERENCES Type1(TypeID)
);
INSERT INTO Type1(TypeName, Speed)
VALUES ('Sample type', '10Mbps'),
('Other type', '50Mbps');
^Up until there its fine
and then when I enter the following it returns "Msg 515, Level 16, State 2, Line 8
Cannot insert the value NULL into column 'TypeID' ..... column does not allows. INSERT fails." Statement terminates
INSERT INTO Splan(PlanName, Quota)
VALUES ('Some sample name', '500GB'),
('sample2, '250GB');
I've tried creating the constraints at both column and table level but the second INSERT statement still refused to enter. Double checked via the GUI and 'TypeID' definitely has an IDENTITY property.
I've looked about everywhere and this errors seems to stem from the lack of an IDENTITY property, yet its present in my creation statements and the error still comes up. Tried removing the seed and increment from IDENTITY, still nothing. Also tried inserting the data one row at a time, nothing there either.
P.S If you haven't noticed the actual names have been substituted and other columns rows have been omitted.
Since you created typID as NOT NULL, Sql is complaining that the default value (NULL) is not acceptable.
Try
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (1, 'Some sample name', '500GB'),
(2, 'sample2, '250GB');
Where corresponding records with TypeID = 1 and TypeID = 2 are in your Type1 table.
You are creating 2 tables: Type1 which has a primary key TypeId that is auto generated
and SPlan which has a primary key PlanId that is also auto generated and a foreign key TypeId that must be supplied and cannot be null.
As written you must enter 1 or more records into Type1 first, obtain their TypeIds, then enter those TypeIds into new records in SPlan.
Incidentally, using TINYINT for your primary key data types is perfectly legal but probably a really bad idea if this is anything other than homework.
You need to supply a value for TypeID in your second query because you have a foreign key relationship with the Type1 table and because the TypeID column in the Splan table is also declared NOT NULL.
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (1, 'Some sample name', '500GB'),
(2, 'sample2, '250GB');
Try inserting both records in a transaction using SCOPE_IDENTITY
begin tran
INSERT INTO Type1(TypeName, Speed)
VALUES ('Sample type', '10Mbps')
DECLARE #id INT = SCOPE_IDENTITY();
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (#id, 'Some sample name', '500GB')
commit