SQL how to create fk int in TABLE

-4
  • Movie
  • COD (int, pk)
  • name (varchar 20, not null)
  • birth (datetime, not null)
  • obs (tinybit, not null)
  • obs2 (int, fk, not null)
  • I thought of something like

    create table filme (
    COD int PRIMARY KEY AUTO_INCREMENT,
    nome varchar(200) NOT NULL,
    nascimento datetime NOT NULL,
    obs tinyint NOT NULL,
    obs2 int NOT NULL
    
    )
    

    I do not know how to create fk ...

    How do I create an int fk?

        
    asked by anonymous 10.10.2018 / 07:06

    1 answer

    1

    You can do this:

    create table filme (
    COD int NOT NULL AUTO_INCREMENT,
    nome varchar(200) NOT NULL,
    nascimento datetime NOT NULL,
    obs tinyint NOT NULL,
    obs2 int NOT NULL
    PRIMARY KEY (COD),
    FOREIGN KEY (obs2) REFERENCES OutraTabela(ColunaChave)
    );
    

    If you have already created the table you can add an FK foreign key like this:

    ALTER TABLE 'filme ' ADD CONSTRAINT 'fk_filme' FOREIGN KEY ( 'obs2' ) REFERENCES 'OutraTabela' ( 'ColunaChave' ) ;
    
        
    10.10.2018 / 09:18