简体   繁体   English

SQL通过alter table选项添加复合主键

[英]SQL adding composite primary key through alter table option

I have already created a table to the database. 我已经创建了一个数据库表。

Table is something like following (I am using sqlite on mac for iPhone dev) 表格如下所示(我在iPhone开发人员的Mac上使用sqlite)

create table
(
attendanceDate   varchar,
studentNo        integer,
absentDetail     varchar
);

Now I have already committed this statements. 现在,我已经提交了此声明。 What I need is the following: 我需要的是以下内容:

  • Add a composite primary key (attandanceDate,studentNo) 添加复合主键(attandanceDate,studentNo)
  • add a foreign key studentNo 添加外键学生

I don't know the alter table syntax in sqlite3. 我不知道sqlite3中的alter table语法。

So, I need help. 所以,我需要帮助。

Thanks in advance. 提前致谢。

SQLITE3 does not have syntax to do what you want. SQLITE3没有语法可以执行所需的操作。

From the SQLITE documentation at http://www.sqlite.org/lang_altertable.html 从位于http://www.sqlite.org/lang_altertable.html的SQLITE文档中

SQLite supports a limited subset of ALTER TABLE. SQLite支持ALTER TABLE的有限子集。 The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. SQLite中的ALTER TABLE命令允许用户重命名表或向现有表添加新列。 It is not possible to rename a colum, remove a column, or add or remove constraints from a table. 无法重命名列,删除列或在表中添加或删除约束。

You will need to recreate the table with the composite primary key and foreign key defined. 您将需要使用定义的复合主键和外键重新创建表。

When you create the table you need to create the primary key then. 创建表时,您需要先创建主键。 SQLLite3 doesn't support foreign key constraints as far as I know. 据我所知,SQLLite3不支持外键约束。 Check out this link for more information. 查看此链接以获取更多信息。 I hope this helps. 我希望这有帮助。

I got the solution, By R&D. 通过研发,我找到了解决方案。

I think, I should drop my table & recreate the table by following syntax, 我认为,我应该删除表并按照以下语法重新创建表,


create table stuAttendance ( attenDate varchar, 创建表stuAttendance(attenDate varchar,
stuNo integer, stuNo整数,
absentDesc varchar, 缺少varchar,
primary key(attenDate,stuNo) 主键(attenDate,stuNo)
foreign key(stuNo) references stuClass(stuNo) 外键(stuNo)引用stuClass(s​​tuNo)
)


That will work as what I needed. 那将按我的需要工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM