sql alter table语句用法

重命名表
重命名表的基本语法是:

 代码如下 复制代码

ALTER TABLE table_name
 RENAME TO new_table_name;

For example:

ALTER TABLE suppliers

这将重命名的供应商表供应商。

 

表中添加列(S)
语法#1

要添加到现有的表列,ALTER TABLE的语法是:

 代码如下 复制代码

ALTER TABLE table_name
 ADD column_name column-definition;

For example:

ALTER TABLE supplier
 ADD supplier_name  varchar2(50);

这会增加供应商表中的列称为supplier_name。

 

语法#2

要添加到现有表的多个列,ALTER TABLE的语法是:

 代码如下 复制代码

ALTER TABLE table_name
ADD ( column_1 column-definition,
  column_2 column-definition,
  ... 
  column_n column_definition );

For example:

ALTER TABLE supplier
ADD ( supplier_name varchar2(50),
  city varchar2(45) );

这将增加两列(supplier_name市)的供应商表。

 

修改表中的列(S)
语法#1

要修改现有表列,ALTER TABLE的语法是:

 代码如下 复制代码

ALTER TABLE table_name
 MODIFY column_name column_type;

For example:

ALTER TABLE supplier
 MODIFY supplier_name   varchar2(100)     not null;

这将修改所谓supplier_name,是一个VARCHAR2数据类型(100),并迫使列不允许空值的列。

 

语法#2

要修改现有表中的多个列,ALTER TABLE的语法是:

 代码如下 复制代码

ALTER TABLE table_name
MODIFY ( column_1 column_type,
  column_2 column_type,
  ... 
  column_n column_type );

For example:

ALTER TABLE supplier
MODIFY ( supplier_name varchar2(100) not null,
 city varchar2(75)   );

这将修改supplier_name和城市列。

 

(S)在一个表中删除列
语法#1

要删除一个现有的表列,ALTER TABLE的语法是:

 代码如下 复制代码

ALTER TABLE table_name
 DROP COLUMN column_name;

For example:

ALTER TABLE supplier
 DROP COLUMN supplier_name;

这将下降supplier_name名为供应商的表列。

 

在表重命名列(S)
(新在Oracle 9i第2版)
语法#1

在Oracle9i第2版开始,你现在可以重命名列。

要在现有的表重命名列,ALTER TABLE的语法是:

 代码如下 复制代码

ALTER TABLE table_name
 RENAME COLUMN old_name to new_name;

For example:

ALTER TABLE supplier
 RENAME COLUMN supplier_name to sname;

This will rename the column called supplier_name to sname.

Acknowledgements: Thanks to Dave M., Craig A., and Susan W. for contributing to this solution!

 

Practice Exercise #1:

Based on the departments table below, rename the departments table to depts.

 代码如下 复制代码
CREATE TABLE departments
( department_id number(10) not null,
  department_name varchar2(50) not null,
  CONSTRAINT departments_pk PRIMARY KEY (department_id)
);   

 

Solution:

The following ALTER TABLE statement would rename the departments table to depts:

 代码如下 复制代码

ALTER TABLE departments
 RENAME TO depts;

Practice Exercise #2:

Based on the employees table below, add a column called salary that is a number(6) datatype.

 代码如下 复制代码

CREATE TABLE employees
( employee_number number(10) not null,
  employee_name varchar2(50) not null,
  department_id number(10), 
  CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);   

Solution:

The following ALTER TABLE statement would add a salary column to the employees table:

 代码如下 复制代码

ALTER TABLE employees
 ADD salary number(6);

Practice Exercise #3:

Based on the customers table below, add two columns - one column called contact_name that is a varchar2(50) datatype and one column called last_contacted that is a date datatype.

 代码如下 复制代码

CREATE TABLE customers
( customer_id number(10) not null,
  customer_name varchar2(50) not null,
  address varchar2(50), 
  city varchar2(50), 
  state varchar2(25), 
  zip_code varchar2(10), 
  CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);   

Solution:

The following ALTER TABLE statement would add the contact_name and last_contacted columns to the customers table:

 代码如下 复制代码

ALTER TABLE customers
ADD ( contact_name varchar2(50),
  last_contacted date );

Practice Exercise #4:

Based on the employees table below, change the employee_name column to a varchar2(75) datatype.

 代码如下 复制代码
CREATE TABLE employees
( employee_number number(10) not null,
  employee_name varchar2(50) not null,
  department_id number(10), 
  CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);   

 

Solution:

The following ALTER TABLE statement would change the datatype for the employee_name column to varchar2(75):

 代码如下 复制代码

ALTER TABLE employees
 MODIFY employee_name varchar2(75);

Practice Exercise #5:

Based on the customers table below, change the customer_name column to NOT allow null values and change the state column to a varchar2(2) datatype.

 代码如下 复制代码

CREATE TABLE customers
( customer_id number(10) not null,
  customer_name varchar2(50),  
  address varchar2(50), 
  city varchar2(50), 
  state varchar2(25), 
  zip_code varchar2(10), 
  CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);   

Solution:

The following ALTER TABLE statement would modify the customer_name and state columns accordingly in the customers table:

 代码如下 复制代码

ALTER TABLE customers
MODIFY ( customer_name varchar2(50) not null,
  state varchar2(2) );

Practice Exercise #6:

Based on the employees table below, drop the salary column.

 代码如下 复制代码

CREATE TABLE employees
( employee_number number(10) not null,
  employee_name varchar2(50) not null,
  department_id number(10), 
  salary number(6), 
  CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);   

Solution:

The following ALTER TABLE statement would drop the salary column from the employees table:

 代码如下 复制代码

ALTER TABLE employees
 DROP COLUMN salary;

Practice Exercise #7:

Based on the departments table below, rename the department_name column to dept_name.

 代码如下 复制代码

CREATE TABLE departments
( department_id number(10) not null,
  department_name varchar2(50) not null,
  CONSTRAINT departments_pk PRIMARY KEY (department_id)
);   

解决方案:

下面的ALTER TABLE语句将重新命名department_name列dept_name部门表:

 代码如下 复制代码

ALTER TABLE departments
 RENAME COLUMN department_name to dept_name;

时间: 2024-11-09 00:52:06

sql alter table语句用法的相关文章

SQL CREATE TABLE 语句用法

SQL CREATE TABLE 语句用法 CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库教程中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据类型, 列名称3 数据类型, .... ) 数据类型(data_type)规定了列可容纳何种数据类型.下面的表格包含了SQL中最常用的数据类型: 数据类型 描述 integer(size) int(size) smallint(size) tinyin

ruby sql select 查询语句用法

ruby sql select 查询语句用法 require 'mysql教程' m = Mysql.new("localhost","ruby","secret","maillist") r = m.query("SELECT * FROM people ORDER BY name") r.each_hash do |f|   print "#{f['name']} - #{f['email']

mysql中TRUNCATE TABLE 语句用法详解

TRUNCATE TABLE删除表中的所有行,而不记录单个行删除操作. 语法    TRUNCATE TABLE name; 参数    name(是要截断的表的名称或要删除其全部行的表的名称) TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少. DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项.TRUNCATE TABLE

SQL ORDER BY 语句用法

ORDER BY 语句用于对结果集进行排序. ORDER BY 语句 ORDER BY 语句用于根据指定的列对结果集进行排序. ORDER BY 语句默认按照升序对记录进行排序. 如果您希望按照降序对记录进行排序,可以使用 DESC 关键字. 原始的表 (用在例子中的): Orders 表: Company OrderNumber IBM 3532 W3School 2356 Apple 4698 W3School 6953 实例 1 以字母顺序显示公司名称: SELECT Company, O

sql group by 语句用法

合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name SQL GROUP BY 实例 我们拥有下面这个 "Or

sql union all语句用法

UNION ALL 的语法如下: [SQL 语句 1] UNION ALL [SQL 语句 2] 我们用和上一页同样的例子来显示出 UNION ALL 和 UNION 的不同 UNION ALL查询允许您结合的结果,设置2个或更多的"select"查询.它返回的所有行(即使该行存在超过一个的"select"报表). 联盟内的每个SQL语句的所有查询必须在结果中相同的字段数套类似的数据类型. 语法一个UNION ALL查询:  代码如下 复制代码 select fie

SQL INSERT INTO 语句用法

INSERT INTO 语句用于向表格中插入新的行. INSERT INTO 表名称 VALUES (值1, 值2,....) 我们也可以指定所要插入数据的列: INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) LastName FirstName Address City Carter Thomas Changan Street Beijing INSERT INTO Persons VALUES ('Gates', 'Bill'

sql where条件语句用法

SELECT.INSERT.UPDATE 或 DELETE 语句或其他子查询中的查询.任何允许使用表达式的地方都可以使用子查询.在此示例中,子查询用作 SELECT 语句中名为 MaxUnitPrice 的列表达式.  代码如下 复制代码 other USE AdventureWorks2008R2; GO SELECT Ord.SalesOrderID, Ord.OrderDate,     (SELECT MAX(OrdDet.UnitPrice)      FROM AdventureWo

sql between 条件语句用法

BETWEEN 条件,允许您检索在一定范围内的值. BETWEEN的语法是:  代码如下 复制代码 SELECT columns FROM tables WHERE column1 between value1 and value2; 实例  代码如下 复制代码 SELECT * FROM suppliers WHERE supplier_id between 5000 AND 5010; 当也可以用其它来判断  代码如下 复制代码 SELECT * FROM suppliers WHERE s