例如:按照department_id查詢employees(員工表)和departments(部門表) 的信息,。 方式一(通用型):SELECT … FROM … WHERE SELECT e.last_name,e.department_id,d.department_name FROM employees e,departments d where e.department_id = d.department_id 方式二:SELECT … FROM … NATURAL JOIN … 有局限性:會(huì)自動(dòng)連接兩個(gè)表中相同的列(可能有多個(gè):department_id和manager_id) SELECT last_name,department_id,department_name FROM employees NATURAL JOIN departments 方式三:SELECT … JOIN … USING … 有局限性:好于方式二,,但若多表的連接列列名不同,此法不合適 SELECT last_name,department_id,department_name FROM employees JOIN departments USING(department_id) 方式四:SELECT … FROM … JOIN … ON … 常用方式,,較方式一,,更易實(shí)現(xiàn)外聯(lián)接(左、右,、滿) SELECT last_name,e.department_id,department_name FROM employees e JOIN departments d ON e.department_id = d.department_id –內(nèi)連接 1) –等值連接 –不等值連接 2) –非自連接 –自連接 –外連接 –左外連接,、右外連接、滿外連接 創(chuàng)建和管理表(DDL) CRAETE TABLE /TRUNCATE TABLE /ALTER TABLE /REANME…TO/DROP TABLE ..操作完以后,,自動(dòng)commit;所以,,rollback對其操作,,沒有效果 1.創(chuàng)建表 1)直接創(chuàng)建 create table emp1( name varchar2(20), salary number(8,2)default 1000, id number(4), hire_date date ); 2)通過子查詢的方式創(chuàng)建 create table emp2 as select last_name name,employee_id id,hire_date from employees; 或者 create table emp2 as select last_name name,employee_id id,hire_date from employees where department_id = 80;/where 1=2; 2.修改表 1)增加新的列 alter table emp1 add(birthday date) 2)修改現(xiàn)有的列 alter table emp1 modify(name varchar2(25) default 'abc’) 3)重命名現(xiàn)有的列 alter table emp1 rename column salary to sal; 4)刪除現(xiàn)有的列 alter table emp1 drop column birthday; 3.清空表中的數(shù)據(jù)(與delete from table_name區(qū)分開) truncate table emp2; 4.重命名表 rename emp2 to emp3; 5.刪除表 drop table emp3; 數(shù)據(jù)處理 DML 1)增 1.1 增添一條記錄 insert into [表名](,,,,,) values(,,,,,) 1.2 從其它表中拷貝數(shù)據(jù) insert into [表名] select …. from [另一個(gè)表] where …. 2)改 update [表名] set ….. where …. 3)刪 delete from [表名] where …. 4)查(最常用的數(shù)據(jù)庫操作) select …. from … where …. group by … having … order by …. 約 束 對創(chuàng)建的表的列屬性、字段進(jìn)行的限制,。諸如:not null/unique/primary key/foreign key/check 1.如何定義約束—在創(chuàng)建表的同時(shí),,添加對應(yīng)屬性的約束 1.1 表級(jí)約束 & 列級(jí)約束 create table emp1( employee_id number(8), salary number(8), –列級(jí)約束 hire_date date not null, dept_id number(8), email varchar2(8) constraint emp1_email_uk unique, name varchar2(8) constaint emp1_name_uu not null, first_name varchar2(8), –表級(jí)約束 constraint emp1_emp_id_pk primary key(employee_id), constraint emp1_fir_name_uk unique(first_name), constraint emp1_dept_id_fk foreign key(dept_id) references departments(department_id) ON DELETE CASCADE ) 1.2 只有not null 只能使用列級(jí)約束。其他的約束兩種方式皆可 2.添加和刪除表的約束–在創(chuàng)建表以后,,只能添加和刪除,,不能修改 2.1添加 alter table emp1 add constaint emp1_sal_ck check(salary > 0) 2.1.1對于not null來講,不用add,,需要使用modify: alter table emp1 modify (salary not null) 2.2 刪除 alter table emp1 drop constaint emp1_sal_ck 2.3使某一個(gè)約束失效:此約束還存在于表中,,只是不起作用 alter table emp1 disable constraint emp1_email_uk; 2.4使某一個(gè)約束激活:激活以后,此約束具有約束力 alter table emp1 enable constraint emp1_email_uk; |
|