Add values to an identity(1,1) column in SQL Server - sql

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?

Related

Can't insert a int value in a decimal column SQL

Currently working on a school project.
I'm trying to create the following table:
CREATE TABLE Purchase (
ID INT NOT NULL,
Type INT DEFAULT 3 NOT NULL,
Price DECIMAL(5,5) NOT NULL,
CONSTRAINT check_3 CHECK (TYPE = 3),
CONSTRAINT price_check CHECK (Cost>0),
CONSTRAINT pk_1 PRIMARY KEY (ID),
CONSTRAINT fk_1 FOREIGN KEY (ID,Type) REFERENCES Part(ID,Type));
My problem is when I'm trying to insert values into this column.
When I try to do this:
INSERT INTO Purchase VALUES (12, 3, 200);
I get the following error:
SQL> INSERT INTO Purchase VALUES (12, 3, 200);
INSERT INTO Purchase VALUES (12, 3, 200)
*
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column
I don't get what I'm doing wrong. Can't I add integers to a decimal column? Is that the problem? Doesn't make much sense to me.
Thank you for taking the time to read this!
First, always list the columns for an insert:
INSERT INTO Purchase (id, type, price)
VALUES (12, 3, 200);
Second, your price is declared as DECIMAL(5, 5). That means that the prices range from 0.00000 to 0.99999.
Presumably, you want a broader range. I don't know what that is, but DECIMAL(10, 5) would solve your problem. More generally, just NUMBER solves your problem in Oracle.
DECIMAL(5,5) means you will recieve space for 5 number after the comma (5 of 5). Please try to create your table like that: DECIMAL(10,5)

Creating a Random Number in a Local Temporary Table

today I have a bit of a dilemma. I was tasked with creating a local temporary table that would contain faculty members first name, last name, campus, and new id number. The ID number would be a randomly generated 5 digit number. (I am using Microsoft SQL Server Management Studio)
My problem is I am new to random number generation and local temp tables. I believe most of my code is correct expect for the "random id number" I need to make. I have googled my problem only thing is there seem to be many ways to create "random" numbers and I don't understand the method behind it.
I've included my code and the database below.
My Code:
SELECT FirstName, LastName, Campus, LEFT(CAST(CAST(CEILING(RAND() *100000000) AS bigint) AS varchar), 5) AS IDnumber
INTO #LocalTemp1
FROM Faculty;
SELECT * FROM #LocalTemp1
Database:
CREATE TABLE Faculty
(Faculty_ID INT PRIMARY KEY IDENTITY,
LastName VARCHAR (20) NOT NULL,
FirstName VARCHAR (20) NOT NULL,
Department VARCHAR (10) SPARSE NULL,
Campus VARCHAR (10) SPARSE NULL);
INSERT INTO Faculty VALUES ('Brown', 'Joe', 'Business', 'Kent');
INSERT INTO Faculty VALUES ('Smith', 'John', 'Economics', 'Kent');
INSERT INTO Faculty VALUES ('Jones', 'Sally', 'English', 'South');
INSERT INTO Faculty VALUES ('Black', 'Bill', 'Economics', 'Kent');
INSERT INTO Faculty VALUES ('Green', 'Gene', 'Business', 'South');
CREATE TABLE Course
(Course_ID INT PRIMARY KEY IDENTITY,
Ref_Number CHAR (5) CHECK (Ref_Number LIKE '[0-9][0-9][0-9][0-9][0-9]'),
Faculty_ID INT NOT NULL REFERENCES Faculty (Faculty_ID),
Term CHAR (1) CHECK (Term LIKE '[A-C]'),
Enrollment INT NULL DEFAULT 0 CHECK (Enrollment < 40))
INSERT INTO Course VALUES ('12345', 3, 'A', 24);
INSERT INTO Course VALUES ('54321', 3, 'B', 18);
INSERT INTO Course VALUES ('13524', 1, 'B', 7);
INSERT INTO Course VALUES ('24653', 1, 'C', 29);
INSERT INTO Course VALUES ('98765', 5, 'A', 35);
INSERT INTO Course VALUES ('14862', 2, 'B', 14);
INSERT INTO Course VALUES ('96032', 1, 'C', 8);
INSERT INTO Course VALUES ('81256', 5, 'A', 5);
INSERT INTO Course VALUES ('64321', 2, 'C', 23);
INSERT INTO Course VALUES ('90908', 3, 'A', 38);
A source i was looking at, still need a better understanding: Generating a random & unique 8 character string using MySQL
EDIT: Running the query actually doesn't display any information, just the column names.
EDIT: Still would need help, I don't know if editing this post will bump it
EDIT: So I decided to try and just use "RAND()" on its own. I can now see the results being displayed, however, the number sets are not all random.
EDIT: Updated the formula for the RANDOM ID, it works in a way, just not making every row a unique random number.

SQL Server 2012 - Auto Increment and Null / Not Null

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);

Why this sequence increments by 2?

I can't understand why this sequence is incremented by 2.
Is there any error in sequence to increment by 1? I need this to insert primary key value in table 'food'.
CREATE SEQUENCE food_id_ai START WITH 1 INCREMENT BY 1 CACHE 100;
create table food(
food_id integer,
f_name varchar(30) not null,
category varchar(30) not null,
price number(4),
amount number(4)
);
alter table food add constraint fpk primary key(food_id);
CREATE OR REPLACE TRIGGER insert_into_food
BEFORE INSERT ON food
FOR EACH ROW
BEGIN
:new.food_id:= food_id_ai.nextval;
END;
/
insert into food values(food_id_ai.nextval,'ruchi', 'chanachur' , 8, 50);
insert into food values(food_id_ai.nextval,'chips', 'chips' , 8, 50);
insert into food values(food_id_ai.nextval,'aeromatic', 'soap' , 8, 50);
insert into food values(food_id_ai.nextval,'handwash', 'toyletries', 8, 50);
insert into food values(food_id_ai.nextval,'tissue', 'toyletries' , 8, 50);
Because you're accessing the sequence both in your INSERT statement and in the trigger that is launched for each row, of course it's incremented by two.
Choose one.
I'd go for the trigger-based one, since you won't have to remember to specify the sequence in each insert statement you may execute.
In that case, you'll have to explicitly list the columns you are going to insert VALUES to:
INSERT INTO food (f_name, category, price, amount)
VALUES ('ruchi', 'chanachur' , 8, 50);
you have two options to correct this.
modify insert statement to be like this:
insert into food (f_name, category,price , amount)
values ('ruchi', 'chanachur' , 8, 50);
or modify you triggers as follow:
CREATE OR REPLACE TRIGGER insert_into_food
BEFORE INSERT ON food
FOR EACH ROW
BEGIN
if :new.food_id is null then
:new.food_id:= food_id_ai.nextval;
end if;
END;
/

Autoincrement through IDENTITY in SQL Server 2012 Management Studio

I'm trying to do autoincrement in SQL Server 2012 Management Studio by using identity, but I'm not able to fill in values for incremental column ID. What to fill in insert values?
Part of my code looks like this:
CREATE TABLE G_Members
(
ID int(4) IDENTITY(0001, 1) PRIMARY KEY,
Jméno varchar(20) NOT NULL,
Nick varchar(20) NULL,
Příjmení varchar(20) NOT NULL,
Pohlaví char(1) NOT NULL,
Datum_Narození date NULL
);
INSERT INTO G_Members VALUES
( 'Martin', 'Mates', 'Škorník', 'M', 01-AUG-1978);
INSERT INTO G_Members VALUES
(NEXT VALUE FOR G_Members.ID, 'Ondřej', ' ', 'Panenka', 'M', 29-MAR-1983, );
If you have an Auto Increment Column you cannot insert any data for that column. You have to modify your SQL-Statement in such a way that you specify all other columns and obmit the IDENTITY column. This is achieved as follows:
INSERT INTO G_Members (Jméno,Nick,Příjmení, Pohlaví,Datum_Narození) VALUES ( 'Martin', 'Mates', 'Škorník', 'M', 01-AUG-1978);
SQL Server will insert a new value for the Id column.
Edit:
I suggest you to use nvarchar over varchar because then you will be able to store unicode values. Especially for your language this would be a better choice