Oracleオペレーティング・コマンドの概要(確認準備)

8678 ワード

備考:SQL*PLUSの使用
1.ログインシステムユーザー:system/パスワード2.Sysユーザーログイン:connect sys/root as sysdba;3.現在のユーザーの表示:show user;4.データ辞書の表示を選択:select username form dba_users; select tablespace_name from dba_tablespaes; 5.scottユーザーの有効化:alter user username account unlcock;6.scottユーザーログイン:scott/tiger 7.システムのロック解除:sqlplus/as sysdba SQL>alter user system account unlock;8.データ辞書の表示:desc dba_users; desc dba_tablespaces; desc user_constraints; 9.永続表領域の作成:SQL>create tablespace test 1_tablespace 2 datafile ’ test1file.dbf’ size 10m; 10.一時表領域の作成:SQL>create temporary tablespace temptest 1_tablespace 2 tempfile ‘tempfile1.dbf’ size 10m; 11.永続表領域の場所の表示:SQL>desc dba_data_files; 12.test 1_の表示tablespaceの場所:SQL>select file_name from dba_data_files where tablespace_name=’TEST1_TABLESPACE’; 13.表領域ステータスの変更:alter tablespace test 1_tablespace 2 offline; 14.表領域の追加:SQL>alter tablespace test 1_tablespace2 add datafile ‘tast 2_file.dbf’size 10m; 15.表領域の表示:SQL>select file_name from dba_data_files where tablespace_name=’TEST1_TABLESPACE’; 16.データファイルの削除:SQL>alter tablespace test 1_tablespace 2 drop datafile ‘test2_file.dbf’; 17.表領域の削除:drop tablespace test 1_tablespace including contents; 18.表の作成:SQL>create table userifo 2(id number(6.0)、3 username varchar 2(20)、4 userpwd varchar 2(20)、5 email varchar 2(30)、6 regdate date date);19.血液検査表データ:SQL>desc userifo 20.追加データ:SQL>alter table userifo 2 add remarks varchar 2(500);21.データの修正:SQL>alter table userifo 2 modify userpwd number(6,0);22.テーブルデータの削除:SQL>alter table userifo 2 drop column remarks;23.データの名前変更:SQL>alter table userifo 2 rename column email to new_email; 24.テーブルデータのクリア:SQL>truncate table new_userifo; 25.削除表:SQL>drop table new_userifo; 26.追加内容:SQL>insert into userinfo 2 values(1,’1’,’xxx’,’[email protected]’,sysdate); 27.クエリ内容:SQL>select userpwd,email from userinfo;SQL> select from userinfo; 28.挿入内容:SQL>insert into userinfo(id,username,userpwd)2 values(2,’yyy’,’123’);29.表の内容をコピーする
SQL> create table userinfo_new
2 as
3 select from userinfo;
SQL> create table userinfo_new1
2 as
3 select id,username from userinfo;
SQL> insert into userinfo_new
2 select * from userinfo;
SQL> insert into userinfo_new(id,username)
2 select id,username from userinfo;

30.テーブル内容の更新:SQL>update userinfo 2 set userpwd='111',email='[email protected]’; SQL> update userinfo 2 set userpwd=’yyy’ 3 where username=’1’; 31.削除内容:SQL>delete from userinfo 2 where username='yyy';32.空でない制約の作成:SQL>create table userinfo_1 2 (id number(6.0), 3 username varchar2(20) not null, 4 userpwd varchar2(20) not null); 33.制約表領域の変更:SQL>alter table userinfo 2 modify username varchar 2(20)not null;SQL> alter table userinfo 2 modify username varchar2(20) null; 34.プライマリ制約スペースの作成:SQL>create table userinfo_p 2 (id number(6.0) primary key, 3 username varchar2(20), 4 userpwd varchar2(20)); SQL> create table userinfo_p1 2 (id number(6.0), 3 username varchar2(20), 4 userpwd varchar2(20), 5 constraint pk_id_username primary key(id,username)); 35.検索制約:SQL>select constraint_name from user_constraints where table_name=’USERINFO_P1’; 36.プライマリ・キーの追加:SQL>alter table userinfo 2 add constraint pk_id primary key(id); 37.プライマリ・キー情報の検索:SQL>select constraint_name from user_constraints where table_name=’USERINFO’; 38.プライマリ・キーの名前の変更:SQL>alter table userinfo 2 rename constraint pk_id to new_pk_id; 39.プライマリ・キーの無効化/起動:SQL>alter table userinfo 2 disable constraint new_pk_id; enable起動40.制約ステータスの表示:SQL>select constraint_name,status from user_constraints where table_name=’USERINFO’; 41.削除制約:SQL>alter table userinfo 2 drop constraint new_pk_id; SQL> alter table userinfo 2 drop primary key; 42.テーブルの作成時にキー制約を設定する(テーブル内の対応する外部キー値は、プライマリ・テーブル内の値またはNULL値でなければならない):プライマリ・テーブル:SQL>alter table typeinfo 2 rename column type to typeid;セカンダリ・テーブル:SQL>create table userinfo_f 2(id varchar 2(10)primary key,3 username varchar 2(20)、4 typeid_new varchar2(10) references typeinfo(typeid)); プライマリ・テーブルの値:SQL>insert into userinfo_f(id,typeid_new)values(1,1); NULL:SQL>insert into userinfo_f(id,typeid_new)values(2,null); 43.テーブル作成時の外部キー制約の追加:SQL>create table userinfo_f3 2 (id varchar2(10) primary key, 3 username varchar2(20), 4 typeid_new varchar2(10), 5 constraint fk_typeid_new1 foreign key(typeid_new)references typeinfo(typeid) on delete cascade) 44.テーブルを変更するときの外部キー制約の追加:SQL>alter table userinfo_f4 2 add constraint fk_typeid_alter foreign key(typeid_new) references typeinfo(typeid); 45.外部キー制約の無効化/起動:SQL>alter table userinfo_f4 2 disable constraint FK_TYPEID_ALTER; 46.外部キー制約の削除:SQL>alter table userinfo_f4 2 drop constraint FK_TYPEID_ALTER; 47.テーブルの作成時に一意の制約を設定する:SQL>create table userinfo_u 2 (id varchar2(20) primary key, 3 username varchar2(20) unique, 4 userpwd varchar2(20)); SQL> create table userinfo_u1 2 (id varchar2(10) primary key, 3 username varchar2(20), 4 constraint un_username unique(username)); 48.テーブルの変更時に一意の制約を追加する:SQL>alter table userinfo_u2 2 add constraint un_username_new unique(username); 49.一意制約の無効化:SQL>alter table userinfo_u2 2 disable constraint UN_USERNAME_NEW; 50.一意制約の削除:SQL>alter table userinfo_u2 2 drop constraint UN_USERNAME_NEW; 51.テーブル作成時のチェック制約の設定:SQL>create table userinfo_c 2 (id varchar2(10) primary key, 3 username varchar2(20), 4 salary number(5,0) check(salary>0)); SQL> create table userinfo_c1 2 (id varchar2(10) primary key, 3 username varchar2(20), 4 salary number(5,0), 5 constraint ck_salary check(salary>0)); 52.テーブルの変更時にチェック制約を追加する:SQL>alter table userinfo_c3 2 add constraint ck_salary_new check(salary>0); 53.チェック制約の無効化:SQL>alter table userinfo_c3 2 disable constraint CK_SALARY_NEW; 54.チェック制約の削除:SQL>alter table userinfo_c3 2 drop constraint CK_SALARY_NEW; 55.別名の設定:SQL>col username headingユーザー名;SQL>select id as番号、username asユーザー名、salary給与from users;(asはスペースで代用可能)56.テーブルの作成時にチェック制約を設定する:列レベル:SQL>create table userinfo_c 2(id varchar 2(10)primary key,3 username varchar 2(20),4 salary number(5,0)check(salary>0);表レベル:SQL>create table usrinfo_c1 2 (id varchar2(10) primary key, 3 username varchar2(20), 4 salary number(5,0), 5 constraint ck_salary check(salary>0)); 57.同じユーザー名を表示しない:SQL>select distinct username asユーザー名from users;58.条件付きクエリー:SQL>select username from users where salary>800;SQL> select username,salary from users where id=’3’; SQL> select username from users where salary>800 and salary<>1800.5;(<>等しくない)SQL>select from users where username='aaa'or salary>2000;SQL>select from users where username='aaa'or(salary>800 and salary<=2000);SQL> select from users where not( username=’aaa’); 59.論理演算子の優先度:1.not 2.and 3.or 60.ファジイクエリ:複数文字SQL>select from users where username like'a%'の代わりに、1文字%を使用します.SQL> select from users where username like ‘_a%’; SQL> select from users where username like ‘%a%’; 61.範囲照会:SQL>select from users where salary between 800 and 2000;SQL> select from users where salary not between 800 and 2000; SQL> select from users where username in (‘aaa’,’bbb’); SQL> select from users where username not in (‘aaa’,’bbb’); 62.クエリー結果のソート:(desc降順asc昇順)SQL>select from users order by id desc;SQL>select from users order by id desc,salary asc;63.演算子/関数を使用してクエリー結果を変更する:1.SQL>select id,username,salary+200 from users;2.case..when文使用:SQL>select username,case username when‘aaa’then‘コンピュータ部門’2 when‘bbb’then‘市場部門’else‘その他の部門’end as部門3 from users;SQL>select username,case when username=’aaa’then‘コンピュータ部門’2 when username=’bbb’then‘市場部門’else‘その他の部門’end as部門3 from users;
SQL>select username,case when salary<800 then‘賃金低’2 when salary>5000 then‘賃金高’end as賃金レベル3 from users;
3.decode関数の使用:SQL>select username,decode(username,’aaa’,’コンピュータ部門’,’bbb’,’市場部門’,’その他’)as部門2 from users;