如何在sql语法错误[mysql]中使用alter

wljmcqd8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(290)

我需要修改一个已经存在的表。起始表如下:

create TABLE schema.students(
  id_students int(11) NOT NULL AUTO_INCREMENT,
  data_signUP date NOT NULL,
  description varchar(100) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  id_university int(11) NOT NULL,
  PRIMARY KEY (id_students),
  KEY id_university(id_university),
  CONSTRAINT students_university_FK FOREIGN KEY (id_university) REFERENCES university(id_university)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

现在我需要修改表并在表exams(id\u exams)中添加另一个fk,我还需要修改列“description”,该列最初不为null,但现在可以为null,因此我要:

alter TABLE schema.students(
      id_students int(11) NOT NULL AUTO_INCREMENT,
      data_signUP date NOT NULL,
      add description varchar(100) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
      id_university int(11) NOT NULL,
 id_exam int(11) NOT NULL,
      PRIMARY KEY (id_students),
      KEY id_university(id_university),
 ADD KEY id_exam(id_exam),
      CONSTRAINT students_university_FK FOREIGN KEY (id_university) REFERENCES university(id_university),
CONSTRAINT students_exams_FK FOREIGN KEY (id_exam) REFERENCES exams(id_exam)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

有人能帮我吗?

deikduxw

deikduxw1#

您可以通过删除表并重新创建它来重新创建整个架构。
否则,请看 ALTER TABLE 语法。你可以这样做:

alter table students modify column description varchar(100);

可以对外键约束执行类似的操作。

相关问题