DDL中drop-alter table

一、DROP TABLE语句:用于删除数据表

  DROP TABLE removes one or more tables.

  You must have the DROP privilege for each table.

  All table data and the table definition are removed, so be careful with this statement!

基本格式:

  drop table <table_name>

DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]

TEMPORARY:用于删除临时表(推荐针对性使用),删除临时表不会结束当前的事务

IF EXISTS:用于在表不存在时,阻止错误消息的显示

[RESTRICT | CASCADE]:现在什么也不做

其实删表语法如上,还是挺简单的,但是当一个表被删除时,所有依赖于该表的对象也被删除(例如约束、索引、视图和权限等)

所以,删除表之前(注意):

  ①确认这个表没有人在使用,确实是一个废除的表

  ②看好确认是这个数据库的表



顺带提一下,如何重命名表:

RENAME  TABLE  tbl_name TO  new_tbl_name
        [, tbl_name2 TO new_tbl_name2] …

当然,可以使用ALTER TABLE语句替换该语句,如下……

二、ALTER TABLE语句:添加、修改和删除列

基本格式:

  1.修改表名:alter table 原表名 rename to 新表名;

  2.新增列:alter table 表名 add [column] 列名 varchar(20) ;

  3.删除列:alter table 表名 drop [column] 列名;

  4.修改列名: alter table 表名 change 原列名 新列名 varchar(20) ;

  5.修改列属性:alter table 表名 modify 列名 varchar(20) ;

ALTER [IGNORE]  TABLE  tbl_name
   ADD [COLUMN] col_name  column_definition
    [FIRST | AFTER col_name ]
  |ADD [COLUMN] (col_name  column_definition,...)
  |MODIFY [COLUMN] col_name column_definition
    [FIRST | AFTER col_name]
  |DROP [COLUMN] col_name
  |CHANGE [COLUMN] old_col_name  new_col_name column_definition
  [FIRST|AFTER col_name]

FIRST:表示表头行

AFTER col_name:表示在col_name的后面,即下一行(没有before选项)

##Copy一张TEAMS表用作示例

mysql> create table teams_copy

 -> as

 -> select * from TEAMS;



1、添加列

  ①新列自动成为表中的最后一个列,除非指定了FIRST或AFTER选项(表中已有的行在新列上自动得到NULL值或默认值)

例1:添加type列给teams_copy表中

mysql> alter table teams_copy
    -> add type1 char(1);    #添加的新列归位最后一个列

mysql> alter table teams_copy
    -> add type2 char(1) default‘1‘;    #添加列的同时设置默认值

mysql> alter table teams_copy
    -> add type3 char(1) after PLAYERNO;    #指定新列的位置

mysql> select * from teams_copy;
+--------+----------+-------+----------+-------+-------+
| TEAMNO | PLAYERNO | type3 | DIVISION | type1 | type2 |
+--------+----------+-------+----------+-------+-------+
|      1 |        6 | NULL  | first    | NULL  | 1     |
|      2 |       27 | NULL  | second   | NULL  | 1     |
+--------+----------+-------+----------+-------+-------+

  ②如果添加新列时指定了NOT NULL约束

则:

  字符串列自动得到空字符串;

  数字列自动得到0;

  日期类型自动得到0日期;

  时间类型自动得到00:00:00

mysql> alter table teams_copy
    -> add (
    ->   type4 char(1) NOT NULL,
    ->   type5 INTEGER NOT NULL,
    ->   type6 DATE NOT NULL,
    ->   tpye7 time not null );

mysql> select * from teams_copy;  #截的一部分图

2、删除列

  列值是否有数据都可以删除,依赖于该列的其它数据库对象,如索引、权限也将被删除

mysql> alter table teams_copy
    -> drop type6;

mysql> alter table teams_copy drop type2;
mysql> alter table teams_copy drop type4;
mysql> alter table teams_copy drop type5;    #只能一列一列的删

mysql> select * from teams_copy;
+--------+----------+-------+----------+-------+----------+
| TEAMNO | PLAYERNO | type3 | DIVISION | type1 | tpye7    |
+--------+----------+-------+----------+-------+----------+
|      1 |        6 | NULL  | first    | NULL  | 00:00:00 |
|      2 |       27 | NULL  | second   | NULL  | 00:00:00 |
+--------+----------+-------+----------+-------+----------+

3、修改列---经常用到

基本格式:

  alter table <表名> modify <列名> 数据类型(……) 选项;

1)修改列的宽度:

  如果是增加列宽,都无所谓

  如果是减少列宽,就必须保证原有的值要能放得下,否则出错

2)改变数据类型时,列中的值必须能转变为新的类型

mysql> desc student;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id    | int(11)       | NO   | PRI | NULL    |       |
| name  | varchar(20)   | NO   |     | NULL    |       |
| sex   | enum(‘M‘,‘F‘) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from student;
+----+--------+------+
| id | name   | sex  |
+----+--------+------+
|  1 | 张三   | M    |
|  2 | 李四   | F    |
|  5 | 王五   | NULL |
+----+--------+------+
3 rows in set (0.00 sec)

mysql> alter table student
    -> modify id smallint not null;
Query OK, 3 rows affected (0.29 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> alter table student
    -> modify name int not null;
ERROR 1366 (HY000): Incorrect integer value: ‘张三‘ for column ‘name‘ at row 1

解析:student表中id列值可转换为新的数据类型,但是name列的值无法转变,所以modify失败。

3)修改列名(不要加引号)

mysql> alter table student
    -> change id 学号 int;

mysql> select * from student;
+--------+--------+------+
| 学号   | name    | sex  |
+--------+--------+------+
|      1 | 张三    | M    |
|      2 | 李四    | F    |
|      5 | 王五    | NULL |
+--------+--------+------+

4、修改约束

  可以添加或删除约束,但是不能修改一个已有的约束。

ALTER [IGNORE] TABLE tbl_name
   ADD [CONSTRAINT [symbol]] PRIMARY KEY(col_name,...)
  |ADD [CONSTRAINT [symbol]] UNIQUE (col_name,...)
  |ADD [CONSTRAINT [symbol]] FOREIGN KEY (col_name,...)
    References parent_table(col_name)
  |DROP PRIMARY KEY
  |DROP FOREIGN KEY fk_symbo
  |DROP {INDEX|KEY} index_name

示例:定义两张表t1和t2

mysql> create table t1(
    ->   a int not null primary key,
    ->   b int not null);

mysql> create table t2(
    ->   a int not null primary key,
    ->   b int not null,
    ->   constraint c1 unique(b),
    ->   constraint fk1 foreign key(a) references t1(a)
    -> );

1)删除唯一性约束:删除对应的索引(index)即可
mysql> alter table t2
    -> drop index c1;

2)给t1添加外键约束,取名fk2
mysql> alter table t1
    -> add constraint fk2 foreign key(a) references t2(a);

3)删除外键fk2
mysql> alter table t1
    -> drop foreign key fk2;

4)删除t1表的主键
mysql> alter table t1
    -> drop primary key;
时间: 2024-08-24 02:06:08

DDL中drop-alter table的相关文章

oracle中比较alter table t move 和alter table t shrink space

alter table t move和alter table t shrink space都可以用来进行段收缩,降低高水位HWM,也都可以用来消除行链接(Row Chaining)和行迁移(Row Migration),但是有如下区别:1)使用alter table move,会把表格最多收缩到创建表格时的storage子句指定的初始大小,使用alter table shrink space,则不受此限制.2)使用alter table move之后,索引会无效,需要重建,使用alter tab

MySQL5.6 ALTER TABLE 分析和测试

在MySQL5.5和之前版本,在运行的生产环境对大表(超过数百万纪录)执行Alter操作是一件很困难的事情.因为将重建表和锁表,影响用户者的使用.因此知道Alter操作何时结束对我们是非常重要的.甚至当执行Create index的时候.如果启用了 fast_index_creation,则不会重建表,但是仍然会锁表.fast_index_creation特性引进在MySQL5.5和更高版本里.在MySQL5.1中如果使用了innodb plugin则也可以使用该特性. 自从MySQL5.6开始

Hive - Create Table&amp;Drop Table &amp; ALTER Table(中)

译注:书接上篇,了解过创建表以及load data后,假如发现需要更改表字段类型或者添加表字段,怎么办?这篇文章将进一步了解具体细节. This chapter explains how to alter the attributes of a table such as changing its table name, changing column names, adding columns, and deleting or replacing columns. Alter Table St

[Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Create/Drop/Truncate Table Alter Table/Partition/Column Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function Create/Drop/Grant/Revoke Roles

SQL ALTER TABLE 语句在项目中的使用

1.在实际的项目开发过程中,之前已经创建好的实体类可能需要增加/删除字段,亦或是更改已有字段的属性,比如主键的增长策略从自增型改为UUID型,那么就会涉及到 SQL 中 alter table 语句的使用. ALTER TABLE table_name ADD column_name datatype 增加表中的列 ALTER TABLE table_name DROP COLUMN column_name 删除表中的列 ALTER TABLE table_name ALTER COLUMN c

ORACLE中通过SQL语句(alter table)来增加、删除、修改字段

1.添加字段: alter table  表名  add (字段  字段类型)  [ default  '输入默认值']  [null/not null]  ; 2.添加备注: comment on column  库名.表名.字段名 is  '输入的备注';  如: 我要在ers_data库中  test表 document_type字段添加备注  comment on column ers_data.test.document_type is '文件类型'; 3.修改字段类型: alter

mySql中alter table的使用

1.修改表名:alter table 原表名 rename to 新表名; 2.新增列:alter table 表名 add column 列名 varchar(20) ; 3.删除列:alter table 表名 drop column 列名; 4.修改列名: alter table 表名 change 原列名 新列名 varchar(20) ; 5.修改列属性:alter table 表名 modify 列名 varchar(20) ;

oracle数据库中drop table与purge使用实例

oracle 数据库中 drop table 与purge 实际示例: CREATE OR REPLACE PROCEDURE pro_droptable IS cursor cur is select table_name from user_tables where table_name like 'TEMPTABLE%'; drop_sql     varchar2(1000); table_number number; BEGIN SELECT COUNT(*) INTO table_n

ORACLE中Drop table cascade constraints

出处:https://www.cnblogs.com/xd502djj/archive/2010/11/16/1878392.html 当你要drop一个table时,如果删除table的动作会造成trigger或constraint产生矛盾,系统会出现错误警告的讯息而不会允许执行..一个极简单的例子,例如你有一个员工基本资料表,上面可能有员工编号和员工姓名等字段,另外有一个员工销售表,上面有员工编号和员工销售额两个字段,员工薪资表的员工编号字段为一个foreign key参照到员工基本资料表的