文章出處

DDL(data definition language)創建和管理表

1.創建表

  • 1.直接創建

    例如:
    create table emp(
    name varchar(20),
    salary int default 1000,
    id int,
    hire_date date );
  • 2.通過子查詢的方式創建

    例如:
    create table
    emp1 as
    select name n_name,id n_id,hire_date from emp;

2.修改表

  • 1.增加新的列

      alter table emp add(birthday date);
  • 2.修改現有的列

     alter table emp alter column name set default "abc"
  • 3.重命名現有的列

    alter table emp change  salary  sal int;
  • 4.刪除現有的列

    alter table emp drop column birthday;
  • 3.清空表中的數據

    truncate table emp1;
  • 4.重命名表

    alter table emp1 rename emp2;
  • 5.刪除表

    drop table emp2;

DML(data manipulation language)數據處理

1.INSERT 增

  • 1.增添一條記錄

    insert into [表名](,,,,,) values(,,,,,)
  • 2.從其它表中拷貝數據

    insert into [表名]
    select .... from [另一個表] where ....

2.UPDATE 改

  • 1.更新數據

    update [表名]
    set .....
    where ....

3.DELETE 刪

  • 1.刪除數據

    delete from [表名]
    where ....

4.SELECT 查

  • 1.查詢數據

    select ....
    from …
    where ….
    group by …
    having …
    order by ….

約束(oracle)

1.創建表的同時,添加對應屬性的約束

  • 1.表級約束 & 列級約束

    create table emp(
      employee_id number(8),
      salary number(8),
       --列級約束
      hire_date date not null,
      dept_id number(8),
      email varchar2(8) constraint emp_email_uk unique,
      name varchar2(8) constraint emp_name_uu not null,
      first_name varchar2(8),
      --表級約束
      constraint emp_emp_id_pk primary key(employee_id),
      constraint emp_fir_name_uk unique(first_name),
      constraint emp_dept_id_fk foreign key(dept_id) references departments(department_id) ON DELETE CASCADE
    );
  • 2.只有 not null 只能使用列級約束。其他的約束兩種方式皆可

2.添加和刪除表的約束--在創建表以后,只能添加和刪除,不能修改

  • 1.添加

    alter table emp
    add constraint emp_sal_ck check(salary > 0);

    對于 not null 來講,不用 add,需要使用 modify:

    alter table emp
    modify (salary not null);
  • 2.刪除

    alter table emp
    drop constraint emp_sal_ck;
  • 3.使某一個約束失效:此約束還存在于表中,只是不起作用

    alter table emp
    disable constraint emp_email_uk;
  • 4.使某一個約束激活:激活以后,此約束具有約束力

    alter table emp
    enable constraint emp_email_uk;

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()