简体   繁体   中英

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)

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)
  • add a foreign key studentNo

I don't know the alter table syntax in sqlite3.

So, I need help.

Thanks in advance.

SQLITE3 does not have syntax to do what you want.

From the SQLITE documentation at http://www.sqlite.org/lang_altertable.html

SQLite supports a limited subset of 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. 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. 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,
stuNo integer,
absentDesc varchar,
primary key(attenDate,stuNo)
foreign key(stuNo) references stuClass(stuNo)
)


That will work as what I needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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