I want to do that when inserting a data block in a database, but that my Primary Key is the same for that data block, and it increases when it is finished inserting
Any ideas?
I want to do that when inserting a data block in a database, but that my Primary Key is the same for that data block, and it increases when it is finished inserting
Any ideas?
The primary keys are unique , therefore, unrepeatable. I would recommend you use an index .
One way you could be this:
Create a table to group the data, for example:
CREATE TABLE 'datos_grupos' (
'idGrupo' int(11) NOT NULL,
'nombre' tinytext COLLATE latin1_general_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
ALTER TABLE 'datos_grupos'
ADD PRIMARY KEY ('id');
ALTER TABLE 'datos_grupos'
MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT;
Note : This table has an incremental auto primary key.
Create a table for the records to be grouped
CREATE TABLE 'datos' (
'id' int(11) NOT NULL,
'dato' text COLLATE latin1_general_ci NOT NULL,
'idGrupo' int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
ALTER TABLE 'datos'
ADD PRIMARY KEY ('id'),
ADD KEY 'idGrupo' ('idGrupo');
ALTER TABLE 'datos'
ADD PRIMARY KEY ('id');
Note : This table has an incremental auto primary key. Also an index type key in which the reference index to the first table must be saved
With these 2 tables, before inserting the first group of data, you should create a group and each data indicate the group. Example:
# Creamos el primer grupo
INSERT INTO 'datos_grupos' ('nombre') VALUES ('grupo 1');
set @idGrupo = last_insert_id();
# Por cada dato
INSERT INTO 'datos' ('nombre', 'idGrupo') VALUES ('dato1', @idGrupo);
Finally, if you want to obtain all the data related to a group, one option would be:
SELECT * FROM 'datos' WHERE idGrupo = 1
I would like to know in which database engine you work.
Because some of them have tools like graphic and then generate the code.
But just in case:
CREATE TABLE PROFESOR(
ID INT NOT NULL ***IDENTITY*** PRIMARY KEY,
NOMBRE VARCHAR(100) NOT NULL,
APELLIDO VARCHAR(100) NOT NULL,
EDAD INT NOT NULL
)
INSERT INTO PROFESOR(NOMBRE, APELLIDO, EDAD) VALUES
(‘Miguel’,’Cervantes’,23),
(‘Augusto’,’Munch’,32),
(‘Minerva’,’Fredson’,29);
Extracted from: link
To be able to do what you want you have to create a variable, this has to contain the value of your Primary Key.
Here is an example:
CREATE TABLE Tabla_01 (
id INT NOT NULL,
campo1 VARCHAR(100),
campo2 varchar(100)
);
CREATE TABLE Tabla_02 (
id INT NOT NULL,
idT1 int NOT NULL,
campo1 VARCHAR(100),
campo2 varchar(100)
);
set @ident=1;
insert into Tabla_01 (id,campo1,campo2)
Values (@ident,'ValorCampo1','ValorCampo2');
insert into Tabla_02 (id,idT1,campo1,campo2)
values
(1,@ident,'ValorCampo1','ValorCampo2'),
(2,@ident,'ValorCampo1','ValorCampo2'),
(3,@ident,'ValorCampo1','ValorCampo2');
set @ident=@ident+1;
insert into Tabla_02 (id,idT1,campo1,campo2)
values
(1,@ident,'ValorCampo1','ValorCampo2'),
(2,@ident,'ValorCampo1','ValorCampo2'),
(3,@ident,'ValorCampo1','ValorCampo2');
You can see this example in SQL Fiddle .