Database add/delete/modify table fields (super detailed)

1. Add table fields

1.1 Grammatical structure

alter table table name add field name field type

1.2 Examples

(1) Create a new student information table (this step can be ignored)

create table student_info (
  sid         number(10),
  sname       varchar2(10),
  sex         varchar2(2),
  create_date date
);

(2) Initial appearance

The table created above is called student_info with only fields and no data空表

SELECT * FROM student_info

Insert image description here

(3) Grammar explanation

alter table table name add field name field type
alter table: Indicates telling the database which table the field is to be added to, fixed Matching, cannot be omitted
Table name: The table to which the field is to be added (the table must exist in the database)
add field name : add is followed by the name of the field to be added
Field type: refers to the attributes of the field to be added, compare the field Is it an integer type or a character type?
For example: In the above tablestudent_info add a new one called< a i=12>, the attribute is a character field, the field is also called column nameyear_old

alter table student_info add year_old varchar(1100)

The database executes the above statement and the addition is successful.

Insert image description here
Insert image description here

2. Modify table fields

2.1 Grammatical structure

(1) Modify field attributes

alter table 表名 modify 字段名 字段类型

modify: means modification. Other explanations are the same as above.

(2) Modify field name

alter table 表名 rename  column  列名 to 新列名

rename: means to rename, followed by column (column), prompting the database to modify the listed name

2.2 Examples

(1) Modify field attributes

student_infoThe sid attribute of the table is an integer type, change it to a character field.

alter table student_info modify sid varchar(1000)

Execute the statement. It can be seen that it has become a character type.
Insert image description here

(2) Modify field column names

Change the year_old column name in the table `student_info to classes

alter table student_info rename column year_old to classes

Execute the statement and you can see that it has been changed.
Insert image description here

3. Delete table fields

3.1 Grammatical structure

alter table table name drop column field name

3.2 Examples

Delete the sname field in table student_info

alter table student_info drop column sname

Execute the statement and you can see that the table fields have been deleted.
Insert image description here

Guess you like

Origin blog.csdn.net/m0_46688827/article/details/130831952