(一)查看与修改 字符编码
-
修改数据库字符集
ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...];
如:ALTER DATABASE myDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
-
把表默认的字符集和所有字符列(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;
(二)添加与删除 外键
-
添加外键
alter table <表名> add foreign key (class_id) references <另一个表> (class_id) on delete set null;///在删除外键时,将从表的外键值设置为null
- 删除表的某个外键
SHOW CREATE TABLE user;
ALTER TABLE user DROP FOREIGN KEY location_id;
(三)改变某字段的数据类型
- 法一:
alter table <table_name> modify column <column_name> <new_data_type>;
- 法二:
alter table <table_name> change [column] <column_name> <new_column_name> <new_data_type>;
【其中的"column"可写可不写】
(四)关于时间与日期类型
- DATATIME
- 大小:8 Byte
- 范围:1000-01-01 00:00:00/9999-12-31 23:59:59
- 格式:YYYY-MM-DD HH:MM:SS
- 用途:混合日期和时间值
- TIMESTAMP
- 大小:8 Byte
- 范围:1970-01-01 00:00:00/2037 年某时
- 格式:YYYYMMDD HHMMSS
- 用途:混合日期和时间值,时间戳
(五)MySQL启动与关闭相关
- Ubuntu环境,apt-get安装为例
- 启动:
sudo /etc/init.d/mysql start
sudo start mysql
sudo service mysql start
- 关闭:
sudo /etc/init.d/mysql stop
sudo stop mysql
sudo service mysql stop
- 重启mysql:
sudo/etc/init.d/mysql restart
sudo restart mysql
sudo service mysql restart
- Mac环境
- 启动:
sudo /usr/local/mysql/support-files/mysql.server start
- 关闭:
sudo /usr/local/mysql/support-files/mysql.server stop
- 重启:
sudo /usr/local/mysql/support-files/mysql.server restart