How can I create a table with autoincrement in access. Here is what I have been doing but not working.
CREATE TABLE People_User_Master(
Id INTEGER primary key AUTOINCREMENT,
Name varchar(50),
LastName varchar(50),
Userid varchar(50) unique,
Email varchar(50),
Phone varchar(50),
Pw varchar(50),
fk_Group int,
Address varchar(150)
)
Remove INTEGER (it conflicts with AUTOINCREMENT)
CREATE TABLE People_User_Master(
Id AUTOINCREMENT primary key ,
Name varchar(50),
LastName varchar(50),
Userid varchar(50) unique,
Email varchar(50),
Phone varchar(50),
Pw varchar(50),
fk_Group int,
Address varchar(150)
)
Try adding the constraint at the end
CREATE TABLE People_User_Master(
Id AUTOINCREMENT
, Name varchar(50)
, LastName varchar(50)
, Userid varchar(50) unique
, Email varchar(50)
, Phone varchar(50)
, Pw varchar(50)
, fk_Group int
, Address varchar(150)
, CONSTRAINT id_pk PRIMARY KEY(Id)
)
Updated to fit the actual answer (the definition of INTEGER on the AUTOINCREMENT column was not allowed). Leaving PRIMARY KEY at the same line as Id AUTOINCREMENT does work.
You can do it using IDENTITY (supported by Jet4+)
CREATE TABLE People_User_Master
(
ID IDENTITY (1, 1),
Name ..
Failing that;
ID AUTOINCREMENT,
Should work (note you don't specify a type)
It may be working, but appears to fail if attempting an INSERT INTO with ID in the Column clause. This seems to override MS Access AUTOINCREMENT.
This attempts to insert a record with ID=1 bypassing AUTOINCREMENT
INSERT INTO People_User_Master
(Id, Name, LastName, Userid, Email, Phone, Pw, fk_Group, Address)
VALUES (1, "John", "Smith", "JS100", "JS#MyMail.Net", 12345678, "****","","")
Omitting ID lets AUTOINCREMENT function properly.
INSERT INTO People_User_Master
(Name, LastName, Userid, Email, Phone, Pw, fk_Group, Address)
VALUES ("John", "Smith", "JS100", "JS#MyMail.Net", 12345678, "****","","")
Related
Many database engines support auto-incrementing primary keys, and I would like to use this approach in my new DuckDB approach, but I can't figure out how to set it up. For example, in MySQL:
CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
create a table:
CREATE TABLE Persons (
Personid integer primary key,
LastName varchar(255) not null,
FirstName varchar(255),
Age integer
);
create a sequence:
CREATE SEQUENCE seq_personid START 1;
Insert some data:
INSERT INTO Persons VALUES (nextval('seq_personid'), 'Doe', 'John', 99);
hi i found an answer here and used the exemple
CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
but when i enter records, it does not allow duplicates in column lastname?!
i would expect id 3 and "hans" but it makes NULL?! it shouldnt be a problem that there is again name hans in row 3 ...
what do i wrong?
If do you want to do autoincrement on Personid, you should do:
CREATE TABLE Persons (
Personid int AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid) );
And if do you want insert something into:
INSERT INTO Persons (LastName, FirstName, Age int) VALUES (..,..,..)
I hope I've helped.
This is all in MS SQL.
I've seen this issue pop up a lot, and I searched around but I still can't figure out my issue. I'm getting the error telling me my foreign key references in invalid column in an uncreated table. However, I created the table as it shows up on my database table list, so it shows it is created.
I tried to move that said table above the table that references it, yet I'm still receiving the error. Would anyone know how to fix this?
My code:
CREATE SCHEMA usr_in;
go
CREATE TABLE gender_interst (
id int IDENTITY (1,1),
gend_id int
CONSTRAINT gender_interstpk PRIMARY KEY (id)
)
CREATE TABLE gender (
id int IDENTITY (1,1),
gend VARCHAR (20)
CONSTRAINT genderpk PRIMARY KEY (id)
);
SELECT * FROM gender_interst LEFT JOIN gender on gender_interst.id = gender.id;
SELECT * from gender_interst;
CREATE TABLE user_info (
id int PRIMARY KEY IDENTITY (1,1),
usr_name VARCHAR (30) NOT NULL,
f_name VARCHAR (30) NOT NULL,
l_name VARCHAR (30),
b_day DATE,
email VARCHAR (120) NOT NULL,
genderpkid VARCHAR (10) NOT NULL,
CONSTRAINT gender_fk FOREIGN KEY (genderpkid) REFERENCES gender (genderpk),
);
INSERT
INTO user_info
( usr_name, f_name, l_name, b_day, genderpkid, email)
VALUES
('JMAN', 'JOHN', 'DOE', '1990-01-01','M','EX#EMAIL'),
('JAM','JANE', 'DOE', '1995-05-02','F','EX#EMAIL'),
('NMAN','NICK', 'WEBB', '1999-06-22','M','EX#EMAIL'),
('LOBA','LOLA', 'LILLY', '1994-01-08','F','EX#EMAIL'),
('NOTSPMAN','PETER', 'PARKER','1985-11-25','M','EX#EMAIL');
SELECT * FROM user_info;
Your foreign key needs to reference the name of a table column (which is either id or gend) and not the name of the primary key (genderpk).
Therefore the foreign key script (CONSTRAINT gender_fk FOREIGN KEY (genderpkid) REFERENCES gender (genderpk)) should look something like CONSTRAINT gender_fk FOREIGN KEY (genderpkid) REFERENCES gender (id)
However to create the relationship the two columns need to have the same data type. user_info .genderpkid is VARCHAR (10) and user_info.id is int.
The other problem you might encounter is that the insert scripts insert the data into genderpkid column as M or F. Therefore int is not going to work
If you were to use use the values M or F for gender, then you can create a script like:
CREATE TABLE gender (
id VARCHAR (1),
gender VARCHAR (20)
CONSTRAINT gender_pk PRIMARY KEY (id)
);
CREATE TABLE user_info (
id int PRIMARY KEY IDENTITY (1,1),
usr_name VARCHAR (30) NOT NULL,
f_name VARCHAR (30) NOT NULL,
l_name VARCHAR (30),
b_day DATE,
email VARCHAR (120) NOT NULL,
genderId VARCHAR (1) NOT NULL,
CONSTRAINT gender_fk FOREIGN KEY (genderId) REFERENCES gender (id),
);
INSERT INTO gender (id, gender)
VALUES
('F', 'Female'),
('M', 'Male')
INSERT
INTO user_info
( usr_name, f_name, l_name, b_day, genderId, email)
VALUES
('JMAN', 'JOHN', 'DOE', '1990-01-01','M','EX#EMAIL'),
('JAM','JANE', 'DOE', '1995-05-02','F','EX#EMAIL'),
('NMAN','NICK', 'WEBB', '1999-06-22','M','EX#EMAIL'),
('LOBA','LOLA', 'LILLY', '1994-01-08','F','EX#EMAIL'),
('NOTSPMAN','PETER', 'PARKER','1985-11-25','M','EX#EMAIL');
SELECT * FROM user_info;
A better approach could be, to pass the whole phraze (male / female) to the table user_info. The table gender can be used to enforce referential integrity. All your information is then contained in SELECT * FROM user_info
For example:
CREATE TABLE gender (
[name] VARCHAR (20)
CONSTRAINT gender_pk PRIMARY KEY ([name])
);
CREATE TABLE user_info (
id int PRIMARY KEY IDENTITY (1,1),
usr_name VARCHAR (30) NOT NULL,
f_name VARCHAR (30) NOT NULL,
l_name VARCHAR (30),
b_day DATE,
email VARCHAR (120) NOT NULL,
gender VARCHAR (20) NOT NULL,
genderIntrest VARCHAR (20) NOT NULL,
CONSTRAINT gender_fk FOREIGN KEY (gender) REFERENCES gender ([name]),
CONSTRAINT genderIntrest_fk FOREIGN KEY (genderIntrest) REFERENCES gender ([name]),
);
INSERT INTO gender ([name])
VALUES
('Female'),
('Male')
INSERT
INTO user_info
( usr_name, f_name, l_name, b_day, gender, genderIntrest, email)
VALUES
('JMAN', 'JOHN', 'DOE', '1990-01-01','Male','Female', 'EX#EMAIL'),
('JAM','JANE', 'DOE', '1995-05-02','Female','Female', 'EX#EMAIL'),
('NMAN','NICK', 'WEBB', '1999-06-22','Male','Female','EX#EMAIL'),
('LOBA','LOLA', 'LILLY', '1994-01-08','Female','Female','EX#EMAIL'),
('NOTSPMAN','PETER', 'PARKER','1985-11-25','Male','Female','EX#EMAIL');
SELECT * FROM user_info;
You can remove the gender table and let the app that consumes it pass in the data. However for a learning exercises, it's probably better to leave it in
This is the code that created the table.
CREATE TABLE CUSTOMERS
(
Customer_ID INT NOT NULL,
CHECK(Customer_ID <= 11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS PRIMARY KEY(Customer_ID);
Then I try to insert data (using this code) into the table and that is where I get this error.
INSERT INTO dbo.CUSTOMERS(Customer_ID, First_Name, Last_Name, Home_Street, Home_City, Home_State, Home_Zip, PhoneNumber)
VALUES (11223344556, 'John', 'Doe', '1234 Hand Street', 'Wahiawa', 'HI', 96786, 2535551267);
What am I doing wrong and what can I do to fix this issue?
AS per my understanding you are checking length of Customer_ID <=11 so you should mention len(Customer_ID)<=11 it will work and you should alter datatype of Customer_ID int to bigint
CREATE TABLE CUSTOMERS
(
Customer_ID bigINT NOT NULL,
CHECK(len(Customer_ID)<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);
INSERT INTO dbo.CUSTOMERS(Customer_ID,First_Name,Last_Name,Home_Street,
Home_City,Home_State,Home_Zip,PhoneNumber)
VALUES(11223344556,'John','Doe','1234 Hand Street',
'Wahiawa','HI',96786,2535551267);
You need customer id to be bigint:
CREATE TABLE CUSTOMERS
(
Customer_ID BIGINT NOT NULL,
CHECK(Customer_ID<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);
The problem may be due to CHECK(Customer_ID<=11) since Customer_ID id integer datatype the server may check for integer validation and not length validation. Try to change the validation.
I have 3 tables User, Profile and ProfilePicture. Profile and ProfilePicture have a foreign key relation with User table. What I want to do here is whenever I insert data into the User table through web application their AutoGeneratedID get and inserted into Profile and ProfilePicture tables.
CREATE TABLE User
(
UserId INT(11) NOT NULL AUTO_INCREMENT,
Username VARCHAR(45) NOT NULL,
Password VARCHAR(50) NOT NULL,
PRIMARY KEY (`UserId`),
UNIQUE INDEX `UserIdId_UNIQUE` (`UserId` ASC)
);
CREATE TABLE Profile
(
UserId INT(11) NOT NULL,
Firstname VARCHAR(50) NULL,
Lastname VARCHAR(50) NULL,
FOREIGN KEY (UserId) REFERENCES User (UserId)
)
CREATE TABLE ProfilePicture
(
UserId INT(11) NOT NULL,
Picture image NULL,
insertdate date NULL,
FOREIGN KEY (UserId) REFERENCES User (UserId)
)
I know I have to use a trigger but I don't understand how to do this.
I am supposing that you are doing this using stored procedure or raw queries. This thing can be achieved by using OUTPUT clause.
Define a local table with column Id
DECLARE #OutputTbl TABLE (ID INT)
Now when you save the User then insert the new gnerated id into #OutputTbl
INSERT INTO User (Username, Password)
OUTPUT INSERTED.UserId INTO #OutputTbl(ID)
VALUES ('name', 'password')
Now when you need this id in Profile/ProfilePicture, get this id from local table
insert into Profile (
UserId ,
Firstname,
Lastname) Values ((Select ID from #OutputTbl),'fName','lName')
You can try something as below :
CREATE TABLE #tempUser(
UserId INT,
ShopRef INT
)
INSERT INTO [User] (UserPassword,Name,MobileNo,Gender,Dob,Country,State,City,StreetAddress
,ZipCode,IsActive
,CreatedDate,ModifiedBy,CreatedBy,IsAdmin,EmailOtp,UserImage,Rating
,ContactNo)
OUTPUT inserted.UserId, inserted.EmailOtp INTO #tempUser
SELECT 'NA', [Name], [MobileNo], '-','1900-01-01',[Country],[State],[City],[StreetAddress],
[ZipCode], 1
,#Date,#UserId,#UserId,0,ID,'NA',0
,'NA'
FROM #temp WHERE Status ='SUCCESS'
If you are executing these inserts in the same SP then you can use, make sure UserId is an Identity Column :
SET #UserId = SCOPE_IDENTITY()
you can after insert into table, call SCOPE_IDENTITY() function, to get the latest
identity inserted
for more informatin see:
https://msdn.microsoft.com/en-us/library/ms190315.aspx
http://www.codeproject.com/Articles/103610/Difference-between-IDENTITY-SCOPE-IDENTITY-IDENT-C