1. 删除外键
语法:alter table 表名 drop foreign key 外键名称
我输入:alter table Student drop foreign key t_id;
报错: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
查看外键名称:
alter table Student drop foreign key;
mysql> show create table Student;
+---------+----------------------------+
| Table | Create Table |
+---------+----------------------------+
| Student | CREATE TABLE `student` (
`s_id` int NOT NULL,
`s_name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`s_birthd` datetime DEFAULT NULL,
`t_id` int DEFAULT NULL,
PRIMARY KEY (`s_id`),
KEY `t_id` (`t_id`),
CONSTRAINT `student_ibfk_1` FOREIGN KEY (`t_id`) REFERENCES `teacher` (`t_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+----------------------------+
1 row in set (0.00 sec)
倒数第二行CONSTRAINT student_ibfk_1 FOREIGN KEY ( t_id ) REFERENCES teacher (t_id)
指出,字段id对应的字段名是t2_ibfk_1
mysql> alter table Student drop foreign key student_ibfk_1; -- 把原来的外键名称改成student_ibfk_q就行了
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
扩展:
-- 增加外键
alter table 表名 add
-- 指定外键名
constraint 自定义的外键名字
-- 指定外键字段
foreign key(外键字段)
-- 引用外部表主键
references 外部表(主键字段)
修改表
-- 添加一列
alter table 表名 add 字段名 类型属性;
-- alter table Student add s_sex nvarchar(1);
-- 修改类型属性
alter table 表名 modify 字段名 新的类型属性;
-- alter table Student modify s_sex int(1);
-- 修改字段名
alter table 表名 change 原来的字段名 新字段名 类型属性;
-- alter table Student change s_sex 性别 int(1);
-- 删除字段
alter table 表名 drop 字段名;
-- alter table drop 性别;
-- 修改表名
alter table 原表名 to 新表名;
-- alter table Student to Stu;
2. sublime光标快捷键
Ctrl+ALT+↓
按end定位到行尾
sublime text常用快捷键及多行光标批量操作教程