SoFunction
Updated on 2025-04-08

Access database uses SQL statement to add fields, modify fields, and delete fields

Here is an example:
Create a table using DDL's Create Table 1, the primary key is the automatically numbered field, and the other field is a text field with length 10.
Copy the codeThe code is as follows:

CREATE TABLE Table1 (Id COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, MyText TEXT (10))


Create another table with two fields Table2, field id is a long integer, field MyText is text
Copy the codeThe code is as follows:

CREATE TABLE Table2 (Id LONG, MyText TEXT)


Use the following statement to establish a one-to-many relationship between Table1 and Table2, update cascaded, and delete cascaded:
Copy the codeThe code is as follows:

ALTER TABLE Table2 ADD CONSTRAINT Relation1 FOREIGN KEY ([Id]) REFERENCES Table1 ([Id]) ON UPDATE CASCADE ON DELETE CASCADE


Use the following statement to delete a relationship:
Copy the codeThe code is as follows:

ALTER TABLE Table2 DROP CONSTRAINT Relation1


Delete Table1 with the following statement:
Copy the codeThe code is as follows:

DROP TABLE Table1


Set a field as the primary key
Copy the codeThe code is as follows:

ALTER TABLE Table 1 ALTER COLUMN [id] COUNTER CONSTRAINT MyPrimaryKey PRIMARY KEY


Add a field MySalary
Copy the codeThe code is as follows:

alter table AAA add COLUMN MySalary CURRENCY


Delete a field MySalary
Copy the codeThe code is as follows:

alter table AAA drop COLUMN MySalary