Это один из многих вопросов реляционной модели - PullRequest
1 голос
/ 24 марта 2020

Это код моего sql. Что не так с этим кодом. Вторая таблица показывает сообщение об ошибке. В первой таблице нет ошибок. Сообщение об ошибке ниже. Пожалуйста, выясните причину ошибки.

***error maessage----->*** 

    mysql> create table req(
        -> rid int auto_increment not null,
        -> joinedDate datetime not null default current_timestamp(),
        -> constraint primary key(rid),
        -> constraint foreign key(fid) references friend(fid)
        -> );
   *** ERROR 1072 (42000): Key column 'fid' doesn't exist in table
    mysql>

    ***My SQL code.......>***

    drop database if exists new;
    create database new;

    use new;

    create table friend(
        fid int auto_increment not null,
        fname varchar(25) not null,
        tp int(3) not null,
        constraint primary key(fid)
    );

    insert into friend(fname,tp)
    values
        ('Nimal Perera',0775548754),
        ('Sandun Hashan',0755548754),
        ('Kamni wasundara',0765548754),
        ('Nuawani Sandareka',0771545704);

    select* from friend;
    desc friend;

    create table req(
        rid int auto_increment not null, 
        joinedDate datetime not null default current_timestamp(),
        constraint primary key(rid),
        constraint foreign key(fid) references friend(fid)
    );

Ответы [ 2 ]

1 голос
/ 27 марта 2020
drop database if exists new;
    create database new;

    use new;

    create table friend(
        fid int auto_increment not null,
        fname varchar(25) not null,
        tp int(3) not null,
        constraint primary key(fid)
    );

    insert into friend(fname,tp)
    values
        ('Nimal Perera',0775548754),
        ('Sandun Hashan',0755548754),
        ('Kamni wasundara',0765548754),
        ('Nuawani Sandareka',0771545704);

    select* from friend;
    desc friend;

    create table req(
        fid int,
        rid int auto_increment not null, 
        joinedDate datetime not null default current_timestamp(),
        constraint primary key(rid),
        constraint foreign key(fid) references friend(fid)
    );

    select* from req;
    desc req;

Выход ----->

+-----+-------------------+-----------+
| fid | fname             | tp        |
+-----+-------------------+-----------+
|   1 | Nimal Perera      | 775548754 |
|   2 | Sandun Hashan     | 755548754 |
|   3 | Kamni wasundara   | 765548754 |
|   4 | Nuawani Sandareka | 771545704 |
+-----+-------------------+-----------+

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| fid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| fname | varchar(25) | NO   |     | NULL    |                |
| tp    | int(3)      | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

Empty set (0.00 sec)

+------------+----------+------+-----+-------------------+----------------+
| Field      | Type     | Null | Key | Default           | Extra          |
+------------+----------+------+-----+-------------------+----------------+
| fid        | int(11)  | YES  | MUL | NULL              |                |
| rid        | int(11)  | NO   | PRI | NULL              | auto_increment |
| joinedDate | datetime | NO   |     | CURRENT_TIMESTAMP |                |
+------------+----------+------+-----+-------------------+----------------+
1 голос
/ 27 марта 2020
create table req(
   rid int,
   fid int,
   joinedDate datetime not null default current_timestamp(),
   constraint primary key(rid),
   constraint foreign key(fid) references friend(fid)
   on delete cascade on update cascade
   );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...