Article Directory
Oracle, mysql 5.7 database table field addition, modification, deletion and other operations
1. Create a table
The basic elements required to create a MySQL data table are:
1. 1, table name
1.2, table field names
1. 3, define each table field
1.4. Code to create the form
CREATE TABLE IF NOT EXISTS `dc3688_tbl`(
`dc3688_id` INT UNSIGNED AUTO_INCREMENT,
`dc3688_title` VARCHAR(100) default 'www.oceanoemchina.com',
`dc3688_author` VARCHAR(40) NOT NULL,
`s_date` DATE,
PRIMARY KEY ( `dc3688_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
1. 5. Table parameter description
If you don't want the field to be NULL, you can set the attribute of the field to NOT NULL. If you enter the data in the field as NULL when operating the database, an error will be reported.
AUTO_INCREMENT defines columns as self-incrementing attributes, which are generally used for primary keys, and the value will automatically increase by 1.
The PRIMARY KEY keyword is used to define the column as the primary key. You can use multiple columns to define the primary key, separated by commas.
ENGINE set the storage engine, add engine=myisam or engine=innodb at the end of the table;
or directly operate:
mysql> alter table table_name engine=myisam;
mysql> alter table table_name engine=innodb;
CHARSET sets the encoding.
– Create table
create table TB_PERMISSION_ORGANIZATION
(
id VARCHAR2(150) not null,
parent_id VARCHAR2(150),
name VARCHAR2(200) not null,
org_code VARCHAR2(200) not null,
org_type NUMBER(2) default 0,
create_date date not null,
update_date date NOT null,
is_delete NUMBER default 0 not NULL
);
alter table TB_PERMISSION_ORGANIZATION add constraint PK_TB_PERMISSION_ORGANIZATION primary key (ID);
alter table TB_PERMISSION_ORGANIZATION add constraint UNIQUE_ORG_CODE unique (ORG_CODE);
COMMIT;
-- Create table
create table TB_PERMISSION_USER
(
id NUMBER(10) not null,
login_name VARCHAR2(200) not null,
password VARCHAR2(200) not null,
name VARCHAR2(50),
phone VARCHAR2(20),
email VARCHAR2(100),
address VARCHAR2(500),
describe VARCHAR2(1000),
organization_id VARCHAR2(150),
create_date date not null,
update_date date NOT NULL,
is_delete NUMBER default 0 not NULL
);
-- Add comments to the columns
comment on column TB_PERMISSION_USER.login_name is '登录名称';
comment on column TB_PERMISSION_USER.is_delete is '是否删除0:未删除 1 已删除';
-- Create/Recreate primary, unique and foreign key constraints
alter table TB_PERMISSION_USER add constraint PK_TB_PERMISSION_USER primary key (ID);
alter table TB_PERMISSION_USER add constraint UNIQUE_TB_PERMISSION_USER unique (login_NAME);
alter table TB_PERMISSION_USER add constraint FK_TB_PERMISSION_USER_ORG foreign key (ORGANIZATION_ID) references TB_PERMISSION_ORGANIZATION (ID) on delete cascade;
COMMIT;
Take tb_permission_user as an example, and there are data in the table.
2. Add fields
语法:alter table tablename add (column datatype [default value][null/not null],….);
说明:alter table 表名 add (字段名 字段类型 默认值 是否为空);
举例:ALTER TABLE tb_permission_user ADD(english_name Varchar2(100) DEFAULT 'admin' NOT NULL);
3. Delete fields
语法:alter table tablename drop (column);
说明:alter table 表名 drop column 字段名;
举例:ALTER TABLE tb_permission_user DROP COLUMN english_name;
4. Modify fields
4.1. Modify the field name
**
语法:alter table tablename rename column name to name1;
说明:alter table 表名rename column 原字段名 to 更改后的字段名;
举例:ALTER TABLE tb_permission_user RENAME COLUMN NAME TO ch_name;
4.2. Modify field type/length
语法:alter table tablename modify (column datatype [default value][null/not null],….);
说明:alter table 表名 modify (字段名 字段类型 默认值 是否为空);
表中无数据,则不管改为什么字段类型,均可直接执行
举例:ALTER TABLE tb_permission_user MODIFY (address VARCHAR2(1000));
5. Modify the primary key-data exists in the table
Example: Change the primary key id of the tb_permission_user table (number(10)–>id(varchar2(100)))
5.1 - New primary key temporary column id_temp
ALTER TABLE tb_permission_user ADD(id_temp VARCHAR2(100));
5.2 - Copy the value of the original id to id_temp
UPDATE tb_permission_user SET id_temp = ID;
5.3 - Delete the original primary key id column
ALTER TABLE tb_permission_user DROP COLUMN ID;
5.4 - Modify the column id_temp as the primary key id column
ALTER TABLE tb_permission_user RENAME COLUMN id_temp TO ID;
5.5 --Add the original primary key constraint of id
ALTER TABLE tb_permission_user ADD CONSTRAINT PK_TB_PERMISSION_USER PRIMARY KEY (ID);