建立简单的表,并对表进行简单ddl操作

1.常见的数据类型

  char(n)   定长

  varchar2(n) 变长,最大到4000

  number(p,s)

  long

  lob

  raw

2.创建一个简单的表

[email protected]>create table t1(
  2  name char(8),
  3  salary number(5) default 0,
  4  content char (4 char),
  5  hiredate date );

Table created.
[email protected]>desc t1;   //描述表结构
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 NAME                                                                       CHAR(8)
 SALARY                                                                     NUMBER(5)
 CONTENT                                                                    CHAR(4 CHAR)
 HIREDATE                                                                   DATE

  在现有的表基础上创建一个新的表。也就是复制现有的表结构和数据创建一个新的表空间

[email protected]>create table t2 as
  2  select ename,job,sal from emp;

Table created.

[email protected]>desc t2;
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 ENAME                                                                      VARCHAR2(10)
 JOB                                                                        VARCHAR2(9)
 SAL                                                                        NUMBER(7,2)

[email protected]>select * from t2;

ENAME                JOB                       SAL
-------------------- ------------------ ----------
SMITH                CLERK                     800
ALLEN                SALESMAN                 1600
WARD                 SALESMAN                 1250
JONES                MANAGER                  2975
MARTIN               SALESMAN                 1250
BLAKE                MANAGER                  2850
CLARK                MANAGER                  2450
SCOTT                ANALYST                  3000
KING                 PRESIDENT                5000
TURNER               SALESMAN                 1500
ADAMS                CLERK                    1100
JAMES                CLERK                     950
FORD                 ANALYST                  3000
MILLER               CLERK                    1300

14 rows selected.

  如果不想要数据,只建立表结构,则在最后写一条不成立的where子句即可

[email protected]>create table t3 as
  2  select ename,job,sal from emp where 1=2;

Table created.

[email protected]>desc t3;
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 ENAME                                                                      VARCHAR2(10)
 JOB                                                                        VARCHAR2(9)
 SAL                                                                        NUMBER(7,2)

[email protected]>select * from t3;

no rows selected

3.修改表结构

  1)如果列为null,可以随便修改列的类型和宽度

       如果有数据,修改会受到限制。但不会破坏数据

     如果不改变数据类型,只改变宽度的话加大是可以的

[email protected]>alter table t1 modify(name char(4));

Table altered.

[email protected]>desc t1;
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 NAME                                                                       CHAR(4)
 SALARY                                                                     NUMBER(5)
 CONTENT                                                                    CHAR(4 CHAR)
 HIREDATE                                                                   DATE

  2)修改表的名称

[email protected]>rename t1 to t_1;

Table renamed.

[email protected]>select * from tab;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
BONUS                                                        TABLE
DEPT                                                         TABLE
EMP                                                          TABLE
SALGRADE                                                     TABLE
T2                                                           TABLE
T3                                                           TABLE
T_1                                                          TABLE

7 rows selected.

  3)修改列的名称(10g以上才可以)

[email protected]>alter table t3 rename column ename to c1;

Table altered.

[email protected]>desc t3;
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 C1                                                                         VARCHAR2(10)
 JOB                                                                        VARCHAR2(9)
 SAL                                                                        NUMBER(7,2)

  4)表注释

[email protected]>comment on table emp is ‘employee table‘;

Comment created.
[email protected]>select comments from user_tab_comments where table_name=‘EMP‘;

COMMENTS
------------------------------------------------------------------------------------------------------------------------
employee table

  5)列注释

[email protected]>comment on column emp.sal is ‘money‘;

Comment created.
[email protected]>select comments from user_col_comments
  2  where table_name=‘EMP‘ AND column_name=‘SAL‘;

COMMENTS
------------------------------------------------------------------------------------------------------------------------
money

  6)丢弃表

    被丢弃的表并没有被删除,只是改了个名称。

[email protected]>select * from tab;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
BONUS                                                        TABLE
DEPT                                                         TABLE
EMP                                                          TABLE
SALGRADE                                                     TABLE
T2                                                           TABLE
T3                                                           TABLE
T_1                                                          TABLE

7 rows selected.

[email protected]>
[email protected]>
[email protected]>drop table t2;

Table dropped.

[email protected]>select * from tab;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
BIN$TkFwIgLVUOTgU0MKqMBNZw==$0                               TABLE
BONUS                                                        TABLE
DEPT                                                         TABLE
EMP                                                          TABLE
SALGRADE                                                     TABLE
T3                                                           TABLE
T_1                                                          TABLE

7 rows selected.

  显示回收站的信息

[email protected]>show recyclebin;
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T2               BIN$TkFwIgLVUOTgU0MKqMBNZw==$0 TABLE        2017-04-28:17:05:49
[email protected]>select * from user_recyclebin;

OBJECT_NAME
------------------------------------------------------------
ORIGINAL_NAME                                                    OPERATION
---------------------------------------------------------------- ------------------
TYPE                                               TS_NAME
-------------------------------------------------- ------------------------------------------------------------
CREATETIME                             DROPTIME                                  DROPSCN
-------------------------------------- -------------------------------------- ----------
PARTITION_NAME                                                   CAN_UN CAN_PU    RELATED BASE_OBJECT PURGE_OBJECT
---------------------------------------------------------------- ------ ------ ---------- ----------- ------------
     SPACE
----------
BIN$TkFwIgLVUOTgU0MKqMBNZw==$0
T2                                                               DROP
TABLE                                              USERS
2017-04-28:15:16:19                    2017-04-28:17:05:49                       1331779
                                                                 YES    YES         88703       88703        88703
         8

  将回收站的表还原

[email protected]>flashback table t2 to before drop;

Flashback complete.

[email protected]>select * from tab;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
BONUS                                                        TABLE
DEPT                                                         TABLE
EMP                                                          TABLE
SALGRADE                                                     TABLE
T2                                                           TABLE
T3                                                           TABLE
T_1                                                          TABLE

7 rows selected.

  将回收站的表还原的同时更改名字

[email protected]>flashback table t2 to before drop rename to tt2;

Flashback complete.

[email protected]>select * from tab;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
BONUS                                                        TABLE
DEPT                                                         TABLE
EMP                                                          TABLE
SALGRADE                                                     TABLE
T3                                                           TABLE
TT2                                                          TABLE
T_1                                                          TABLE

7 rows selected.

  清空回收站内指定的表

[email protected]>create table t4 as select * from emp;

Table created.

[email protected]>drop table t4;

Table dropped.

[email protected]>purge table t4;

Table purged.

  清空当前用户的回收站,不会影响其它用户的回收站

[email protected]>purge recyclebin;

Recyclebin purged.

  绕过回收站,直接彻底删除一张表

[email protected]>drop table t4 purge;

Table dropped.
时间: 2024-10-04 09:18:04

建立简单的表,并对表进行简单ddl操作的相关文章

mvc area区域和异步表单,bootstrap简单实例

码农最怕眼高手低 今天来练习mvc Area技术和bootstrap以及异步表单的C#代码实现. 1.area区域架构对于建立复杂业务逻辑很有帮助,由  AreaRegistration.RegisterAllAreas()方法遍历路由表,获得所有注册的路由.参见 建立类库Common,下设一个文件夹BookStore 在其中建立model和controller.(注意引用System.Web.Mvc这个dll) 项目结构如图: 其中book.cs为model模型 namespace Commo

Java+MyEclipse+Tomcat (二)配置Servlet及简单实现表单提交

在Java EE应用编程中Servlet是基础,JSP是建立在Servlet基础之上的,其他Web框架如Struts.WebWork和Spring MVC都是基于Servlet的.本文主要讲述MyEclipse和Tomcat配置Servlet的过程和简单实现表单提交功能. 希望对大家有所帮助,参考书籍<Java EE企业级应用开发实例教程>. Java+MyEclipse+Tomcat (一)配置过程及jsp网站开发入门 一. Servlet编程 1.Web基础知识 在讲述Servlet之前,

lua 对表的简单序列化与反序列化

参考文档:http://blog.csdn.net/xiaodan007/article/details/7096718 function sz_T2S(_t) local szRet = "{" function doT2S(_i, _v) if "number" == type(_i) then szRet = szRet .. "[" .. _i .. "] = " if "number" == ty

下拉选择框改变表单action的简单例子

做了个简单的小例子 用js弄的. form  的代码 1 <form  action=""  method="post" id="formRole"> type的 下拉单 1 2 3 4 5 6 <select name="type" id="type"  onchange="changeAction()">       <option value=&quo

C#简单注册表操作实例

1.简介操作 //设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径不存在,则创建 //2.如果指定键存在,则覆盖值 string path = "HKEY_CURRENT_USER\\myRegOne"; Registry.SetValue(path, "Expend", "hellow world!"); Mes

jquery 一个简单的表单验证实例

表单验证在网站开发过程中经常遇到,我们可以使用服务器端语言验证,也可以使用客户端语言来验证.本文章向大家介绍jquery客户端验证表单的一个简单实例.实例仅作参考. <body> <form method="post" action=""> <div class="int"> <label for="username">用户名:</label> <!-- 为每个

ThinkPHP 数据库表结构处理类(简单实用)

<?php /*  * mysql表结构处理类  * 创建数据表,增加,编辑,删除表中字段  *  */ class MysqlManage{ /*  * 创建数据库,并且主键是aid  * table 要查询的表名  */ function createTable($table){ $sql="CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB;"; M()-&g

JQuery中一个简单的表单验证的实例

html代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&quo

根据实例类型反射操作数据库(简单通用表操作类)

这个类适用简单的表 1.有且只有id为主键, 2.并且实例类主键,也就是id应为字段,其他为属性 3.实例类名跟表名一样,字段属性跟列名一样 public class ProType { public int id; public string type { get; set; } public int array { get; set; } public string relyTpye { get; set; } public string etype { get; set; } } 下面就是