Oracle 基本SQL语句

  • 创建表

    create table A( aid number(2) not null, aname varchar2(4), asal number(7,2) );

  • 增加数据(插入)

    insert into a(aid,aname,asal) values(1,\'张三\',1000.5); insert into a values(1,\'张三\',1000.5);

  • 修改数据(更新)

    update <表名> set <列名=更新值> [where <更新条件>]

    例:update tongxunlu set 年龄=18 where 姓名=\'蓝色小名\'

  • 删除数据

    【删除<满足条件的>行数据】delete from <表名> [where <删除条件>]

    例:delete from a where name=\'开心朋朋\'(删除表a中列值为开心朋朋的行) 

    【删除整个表数据】truncate table <表名>

    例:truncate table tongxunlu

    注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表

    【删除整个表】drop table 表名称

    例:drop table a;

  • 查询数据

    select * from a;

    select 字段 from 表名 where 条件

  • 增加字段语法

    alter table tablename add (column datatype [default value][null/not null],….);

    说明:alter table 表名 add (字段名 字段类型 默认值 是否为空);

    例:alter table sf_users add (HeadPIC blob);

    例:alter table sf_users add (userName varchar2(30) default \'空\' not null);

  • 修改字段的语法

    alter table tablename modify (column datatype [default value][null/not null],….);

    说明:alter table 表名 modify (字段名 字段类型 默认值 是否为空);

    例:alter table sf_InvoiceApply modify (BILLCODE number(4));

  • 删除字段的语法

    alter table tablename drop (column);

    说明:alter table 表名 drop column 字段名;

     例:alter table sf_users drop column HeadPIC;

  • 字段的重命名

  说明:alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)

  例:alter table sf_InvoiceApply rename column PIC to NEWPIC;

  • 表的重命名

    说明:alter table 表名 rename to 新表名

    例:alter table sf_InvoiceApply rename to sf_New_InvoiceApply;

  • 实现将一个表的数据插入到另外一个表

    1.第一种情况

    1》如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:

      INSERT INTO 目标表 SELECT * FROM 来源表;

    2》比如要将 articles 表插入到 newArticles 表中,则是:

      INSERT INTO newArticles SELECT * FROM articles;

    3》如果只希望导入指定字段,可以用这种方法:

      INSERT INTO 目标表 (字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM 来源表;

    2.第二种情况

    1》如果将一个表的数据放在另外一个不存在的表:

      select * into 目标不存在的表 from 来源表

    2》如果只希望导入指定字段,可以用这种方法:

      select 字段1,字段2,... into 目标不存在的表 from 来源表