修改表的默认字符集:
ALTER TABLE table_name DEFAULT CHARACTER SET character_name;
修改表字段的默认字符集:
ALTER TABLE table_name CHANGE field field field_type CHARACTER SET character_name [other_attribute]
修改表的默认字符集和所有列的字符集:
ALTER TABLE table_name CONVERT TO CHARACTER SET character_name
============================================================================修改数据库字符集:
ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...];把表默认的字符集和所有字符列(CHAR,VARCHAR,TEXT)改为新的字符集:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name [COLLATE ...]
如:ALTER TABLE logtest CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
只是修改表的默认字符集:
ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name [COLLATE...];
如:ALTER TABLE logtest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
修改字段的字符集:
ALTER TABLE tbl_name CHANGE c_name c_name CHARACTER SET character_name [COLLATE ...];
如:ALTER TABLE logtest CHANGE title title VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci;
查看数据库编码:
SHOW CREATE DATABASE db_name;
查看表编码:
SHOW CREATE TABLE tbl_name;
查看字段编码:
SHOW FULL COLUMNS FROM tbl_name;
======================================================
1. 创建支持中文的database
CREATE DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
2. 创建支持中文的table
CREATE TABLE `database_user` (
`ID` varchar(40) NOT NULL default '',
`UserID` varchar(40) NOT NULL default '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
===========================================================
1. mysql 外键设定:
查看table的外键状态: show create table "table_name";
原文链接
a 语法
CREATE TABLE parent(id INT NOT NULL,
PRIMARY KEY (id)
) TYPE=INNODB; -- type=innodb 相当于 engine=innodb
CREATE TABLE child(id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) TYPE=INNODB;
向parent插入数据后,向child插入数据,插入时,child中的parent_id的值只能是parent中有的数据,否则插入不成功;
删除parent记录时,child中的相应记录也会被删除;-->因为: on delete cascade
更新parent记录时,不给更新;-->因为没定义,默认采用restrict.
4.2
若child如下:
mysql>
create table child(id int not null primary key auto_increment,parent_id int,
index par_ind (parent_id),
constraint fk_1 foreign key (parent_id) references
parent(id) on update cascade on delete restrict)
type=innodb;
用上面的:
1).
则可以更新parent记录时,child中的相应记录也会被更新;-->因为: on update cascade
2).
不能是子表操作,影响父表.只能是父表影响子表.
3).
删除外键:
alter table child drop foreign key fk_1;
添加外键:
alter table child add constraint fk_1 foreign key (parent_id) references
parent(id) on update restrict on delete set null;