Linux MySQL --- 删

(删)删除指定数据表(库)

DROP TABLE 表名;    # 如不用USE进入库中,则需加上数据库名

MySQL [chuid]> show tables;

+-----------------+

| Tables_in_chuid |

+-----------------+

| chd             |

+-----------------+

1 row in set (0.00 sec)

MySQL [chuid]> drop table chuid.chd;    # 删除指定数据库中的表

Query OK, 0 rows affected (0.11 sec)

MySQL [chuid]> show tables;   # 查看数据表

Empty set (0.00 sec)

(删)删除指定的数据库

DROP DATABASE (数据库名);  

MySQL [mysql]> show databases;    # 查看数据库

+--------------------+

| Database           |

+--------------------+

| information_schema |

| chuid              |

| mysql              |

| mysql_chd          |

| mysql_chuid        |

| performance_schema |

+--------------------+

6 rows in set (0.00 sec)

MySQL [mysql]> drop database mysql_chd;    # 删除指定的数据库

Query OK, 1 row affected (0.02 sec)

MySQL [mysql]> show databases;   # 查看数据库

+--------------------+

| Database           |

+--------------------+

| information_schema |

| chuid              |

| mysql              |

| mysql_chuid        |

| performance_schema |

+--------------------+

5 rows in set (0.00 sec)

(删)在数据表中删除指定的数据记录

DELETE FROM 表名 [WHERE 条件表达式];

MySQL [mysql_chuid]> select * from student;

+----+-------+------+

| id | name  | sex  |

+----+-------+------+

|  1 | chd   | man  |

|  2 | chuid | man  |

|  3 | anivd | man  |

+----+-------+------+

3 rows in set (0.00 sec)

MySQL [mysql_chuid]> delete from student where id=3;   # 删除指定表id=3的内容

Query OK, 1 row affected (0.04 sec)

MySQL [mysql_chuid]> select * from student;    # 查询student表中所有的数据信息

+----+-------+------+

| id | name  | sex  |

+----+-------+------+

|  1 | chd   | man  |

|  2 | chuid | man  |

+----+-------+------+

2 rows in set (0.00 sec)

(删)删除字段

ALTER TABLE表名 DROP 字段名;

MySQL [mysql_chuid]> select * from student;    # 查询student表中所有的数据信息

+----+-------+------+

| id | name  | sex  |

+----+-------+------+

|  1 | chd   | man  |

|  2 | chuid | man  |

+----+-------+------+

2 rows in set (0.00 sec)

MySQL [mysql_chuid]> alter table student drop sex;   # 删除student表中的sex字段

Query OK, 0 rows affected (0.44 sec)

Records: 0  Duplicates: 0  Warnings: 0

MySQL [mysql_chuid]> select * from student;    # 查询student表中所有的数据信息

+----+-------+

| id | name  |

+----+-------+

|  1 | chd   |

|  2 | chuid |

+----+-------+

2 rows in set (0.00 sec)

(删)删除被其它表关联的主表

数据表之间经常存在外键关联的情况,这时如果直接删除父表,会破坏数据表的完整性,也会删除失败。

* 删除父表有以下两种方法:

1)先删除与它关联的子表,再删除父表;但是这样会同时删除两个表中的数据。

2)将关联表的外键约束取消,再删除父表;适用于需要保留子表的数据,只删除父表的情况。

:如何取消关联表的外键约束并删除主表

MySQL [mysql_chuid]> create table class_1(id int(5) PRIMARY KEY,name char(10),sex int(5));  # 创建表class_1

Query OK, 0 rows affected (0.04 sec)

MySQL [mysql_chuid]> DESC class_1;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(5)   | NO   | PRI | NULL    |       |

| name  | char(10) | YES  |     | NULL    |       |

| sex   | int(5)   | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.04 sec)

# 创建表class_2,并添加外键约束

MySQL [mysql_chuid]> create table class_2(id int(5) PRIMARY KEY,name varchar(10),claID int(10),grades float,CONSTRAINT FK_cla1_cla2 FOREIGN KEY(claID) REFERENCES class_1(id));

Query OK, 0 rows affected (0.28 sec)

# 由SQL运行结果可以看出,表class_2为子表,具有FK_cla1_cla2的外键约束;class_1为父表,其主键id被子表class_1所关联

MySQL [mysql_chuid]> show create table class_2\G;   # 查看表class_2的外键约束

*************************** 1. row ***************************

       Table: class_2

Create Table: CREATE TABLE `class_2` (

  `id` int(5) NOT NULL,

  `name` varchar(10) DEFAULT NULL,

  `claID` int(10) DEFAULT NULL,

  `grades` float DEFAULT NULL,

  PRIMARY KEY (`id`),

  KEY `FK_cla1_cla2` (`claID`),

  CONSTRAINT `FK_cla1_cla2` FOREIGN KEY (`claID`) REFERENCES `class_1` (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

MySQL [mysql_chuid]> ALTER table class_2 DROP FOREIGN KEY FK_cla1_cla2;   # 解除子表class_2的外键约束(取消class_1和class_2之间的关联)

Query OK, 0 rows affected (0.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

MySQL [mysql_chuid]> DROP table class_1;     # 删除父表class_1

Query OK, 0 rows affected (0.01 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值