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?
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.
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.
CREATE TABLE detectives(
id INTEGER NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
phone_number VARCHAR(10) NOT NULL,
certification_date DATE NOT NULL,
CONSTRAINT detectives_pk PRIMARY KEY (id
);
It says: Incorrect syntax near 'AUTO_INCREMENT'.
Any help with this?
Missing closing ) and using incorrect syntax for an IDENTITY field.
CREATE TABLE detectives(
id INT IDENTITY,
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
phone_number VARCHAR(10) NOT NULL,
certification_date DATE NOT NULL,
CONSTRAINT detectives_pk PRIMARY KEY (id)
)
Change
id INTEGER NOT NULL AUTO_INCREMENT,
To
ID INT NOT NULL IDENTITY(1,1),
you've to make the column identity if you want auto increment. your code will be
CREATE TABLE detectives(
id INT NOT NULL IDENTITY (1, 1),
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
phone_number VARCHAR(10) NOT NULL,
certification_date DATE NOT NULL,
CONSTRAINT detectives_pk PRIMARY KEY (id)
);
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.