I have these relationships:

`User(uid:integer,uname:varchar)`, key is uid  

`Recipe(rid:integer,content:text)`, key is rid  

`Rating(rid:integer, uid:integer, rating:integer)` , key is (uid,rid). 

I built the table in the following way:

```
CREATE TABLE User(
    uid INTEGER PRIMARY KEY ,
    uname VARCHAR NOT NULL
);
CREATE TABLE Recipes(
    rid INTEGER PRIMARY KEY,
    content VARCHAR NOT NULL
);

```

Now for the `Rating` table: I want it to be impossible to insert a `uid\rid` that does not exist in `User\Recipe`.  

My question is: which of the following is the correct way to do it? Or please suggest the correct way if none of them are correct. Moreover, I would really appreciate if someone could explain to me what is the difference between the two. 

First:

```
CREATE TABLE Rating(
    rid INTEGER,
    uid INTEGER,
    rating INTEGER CHECK (0<=rating and rating<=5) NOT NULL,
    PRIMARY KEY(rid,uid),
    FOREIGN KEY (rid) REFERENCES Recipes,
    FOREIGN KEY (uid) REFERENCES User
);

```

Second: 

```
  CREATE TABLE Rating(
      rid INTEGER REFERENCES Recipes,
      uid INTEGER REFERENCES User,
      rating INTEGER CHECK (0<=rating and rating<=5) NOT NULL,
      PRIMARY KEY(rid,uid)
  );

```

EDIT:
I think `User` is problematic as a name for a table so ignore the name.