table in ms sql with primary key and auto_increment problem - sql

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.

Related

Create an auto incrementing primary key in DuckDB

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

ALTER TABLE statement conflicted with the FOREIGN KEY constraint

When creating the foreign key I came across this error. Below is my code.
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int
)
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
alter table tblPerson add constraint tblPerson_GenderId_FK
foreign key (GenderId) references tblGender(ID)
You want to identify any "do not align" rows....
I have made the below.
You won't be able to add the FK constraint, if any rows come back from the SELECT query.
I have also removed the hungarian notation for "tbl". I would advise against it.
create table dbo.Person(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int )
create table dbo.Gender (
ID int not null primary key,
Gender varchar(50) not null
)
/* any rows below? your FK creation will fail */
Select *, p.GenderId as 'HoustonWeHaveAProblemValue' from dbo.Person p Where Not Exists (Select 1 from dbo.Gender g where g.ID = p.GenderId)
alter table dbo.Person add constraint Person_GenderId_FK
foreign key (GenderId) references dbo.Gender(ID)
Hi please use the following code to achieve your goal :
1-First create tblGender:
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
2-Then create table tblPerson with the relationship between 2 tables since the beginning:
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int references tblGender(ID)
)
works fine.

Why Auto_Increment not working in SQL?

CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
When I run this code, it is not run successfully.
Syntax for MySQL
CREATE TABLE Persons (
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Syntax for SQL Server
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Syntax for Oracle
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
If this is Microsoft SQL Server then change to IDENTITY instead of Auto_Number. If it's Postgres then I believe you want SERIAL as the datatype instead of INT. If this is MySQL then it should work as-is.
What RDBMS are you using?

Creating Entity Relationship Diagram

I created the ERD of my system and now I would like to create a SQL Code.
So should a SQL Code look like this?:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL FOREIGN KEY
);
Your problem is with the FOREIGN KEY part of your query, you are not defining the foreign key there. If you now remove that, your query will work, but without a defined FK:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL
);
If you want to create the foreign key, you need to do something like this (with the correct table and column):
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL REFERENCES Group(Group_ID)
);
Your "FOREIGN KEY" declaration is incomplete - you need to say which table/column the foreign key references.
If you just want to get the table built,
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL);
should work.

autoincrement in access sql is not working

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, "****","","")