MySQL学习笔记

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常用快捷键及多行光标批量操作教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值