First registration of a foreign key

1

Good morning classmates I need to know how I can identify when the first record of a relationship is created, I have 2 related tables the main table has idclase primary key and the second table with its foreign key idclase and primary key id test I have a class called sciences that was created on 04/12/2017 and the first program of that class was created on 05/15/2017 that date has to be automatically put in the first column of the science class

create table clase(
idclase int identity primary key,
clase varchar(10),
fechaclase datetime ,
primerprograma datetime
)


create table programa(
idclase int ,
idprograma int identity primary key,
programa varchar(20),
fechaprograma datetime,
)

alter table programa add constraint idclase
foreign key(idclase) references clase(idclase)
go

IDCLASE     IDPROGRAMA      CLASE       PROGRAMA        FECHACLASE      FECHAPROGRAMA   PRIMERPROGRAMA
1                           CIENCIAS                    12/04/2017                      ***15/05/2017***
2                           TECNOLOGIA                  22/04/17
3                           ARTES                       26/07/17                        ***25/04/2017***
1           1               CIENCIAS    QUMICA                          ***15/05/2017***        
3           2               ARTES       ARTESNIAS                       ***25/04/2017***        

What I do is a trigger to save a type history so that at the end all that information is saved in another table called movements dates

    
asked by Ersuka6 31.07.2017 в 18:48
source

2 answers

0

Select top 1 * from program where idclass = 1425 order by idprogram

If id program is autonumerico you must return the first one.

    
answered by 31.07.2017 в 19:22
0

You should create a column that stores the date and time when the record is created, you can do a select Top 1 dateRegister from program where program.idclass [email protected].

    
answered by 31.07.2017 в 19:30