常用sqlステップ文

7126 ワード

一、拡張データベーステーブルフィールド長
--mysql
alter table user modify name  varchar2 (32);
--oracle
alter table A modify(name varchar2(4000))

  
二、テーブルにインデックスを追加する(mysql)
1.PRIMARY KEY(プライマリ・キー・インデックス)の追加
mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )

2.UNIQUE(ユニークインデックス)の追加
mysql>ALTER TABLE `table_name` ADD UNIQUE (`column`)

3.INDEXの追加(通常索引)
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` )

4.FULLTEXTの追加(全文索引)
mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)

5.複数列索引の追加
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

 
 
三、マルチテーブル関連付けupdate、in文を使用する
select * from ok.table_001_reg t where to_char(order_id) in (select to_char(t.order_id) from ok.table_002 t where  t.test_sn ='7777601') for update;
delete  from ok.table_001_reg t where to_char(order_id) in (select to_char(t.order_id) from ok.table_002 t where  t.test_sn  in ('7777601'));

  
四、デッドロックの表示とクリア(oracel)
1、ロックされたデータベースプロセスの表示
select b.owner,b.object_name,a.session_id,a.locked_mode
from v$locked_object a,dba_objects b
where b.object_id = a.object_id;

2、どのsid(session)が引き起こしたのかを問い合わせる
select b.username,b.sid,b.serial#,logon_time
from v$locked_object a,v$session b
where a.session_id = b.sid order by b.logon_time;

3、killプロセス
ALTER system kill session '789, 7296';

 
五、表空間メンテナンス(oracel)
1、表領域の作成
CREATE TABLESPACE TBS_TR_DATA
DATAFILE '/oradata/rTBS_TR_DATA_001.dbf'
SIZE 64G
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO ONLINE;

2、表領域の状態を表示する
select tablespace_name,status from dba_tablespaces;

3、表領域物理ファイルの名前とサイズを表示する
SELECT tablespace_name,file_id,file_name,round(bytes / (1024 * 1024), 0) total_space FROM dba_data_files ORDER BY tablespace_name;

4、データ表領域使用率の表示
SELECT a.tablespace_name,
a.bytes/(1024*1024) total_M,
b.bytes/(1024*1024) used_M,
c.bytes/(1024*1024) free_M,
(b.bytes * 100) / a.bytes "% USED ",
(c.bytes * 100) / a.bytes "% FREE "
FROM sys.sm$ts_avail a, sys.sm$ts_used b, sys.sm$ts_free c
WHERE a.tablespace_name = b.tablespace_name
AND a.tablespace_name = c.tablespace_name;

5、表領域の毎日の使用状況を問い合わせる
select a.name, b.*
from v$tablespace a,
(select tablespace_id,
trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss')) datetime,
max(tablespace_usedsize * 8 / 1024) used_size
from dba_hist_tbspc_space_usage
where trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss')) >
trunc(sysdate - 30) group by tablespace_id,
trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss')) order by
tablespace_id, trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss'))) b
where a.ts# = b.tablespace_id ;

6、表空間拡張
 
--             
SQL> col file_name for a60
SQL> select file_name,bytes/1024/1024 from dba_data_files;
SQL> alter database datafile '/home/oracle/app/oradata/orcl/users01.dbf'resize 51M;
SQL> select file_name,bytes/1024/1024 from dba_data_files;
--           
SQL> alter tablespace andy add datafile '/home/oracle/app/oradata/orcl/andy02.dbf'size 1M
autoextend on next 1m maxsize 2m ;

7、表領域の削除
--すべてのデータベース・オブジェクトの削除とデータ・ファイルの削除
drop tablespace XXX including contents and contents;

8、表領域の名前変更
alter tablespace tablespace_name rename to new_table_name;
alter tablespace andy rename to newandy;

9、表領域のデータファイルを移動する
SQL> select tablespace_name,file_name from dba_data_files where tablespace_name = 'NEWANDY';
SQL> alter tablespace newandy offline;
[oracle@11g ~]$ mv /home/oracle/app/oradata/orcl/andy01.dbf /home/oracle/app/oradata/andy01.dbf
SQL> alter tablespace newandy rename datafile '/home/oracle/app/oradata/orcl/andy01.dbf' to '/home/oracle/app/oradata/andy01.dbf';
SQL> alter tablespace newandy online;
SQL> select tablespace_name,file_name from dba_data_files where tablespace_name = 'NEWANDY';

10、表領域の自動拡張性を修正する
SQL> select tablespace_name,status,extent_management,SEGMENT_SPACE_MANAGEMENT from dba_tablespaces;
SQL> alter database datafile file_name autoextend off|on [next number K|M maxsize unlimited|number K|M]
SQL> select tablespace_name,status,extent_management,SEGMENT_SPACE_MANAGEMENT from dba_tablespaces;


 
六、権限付与文
--   ip         root,   123          。   "%"    ip  ,                   。                  root:123        
grant all privileges on   *.* to root@"%" identified by '123' with grant option;   flush privileges;   

詳細:
--   10.163.225.87   joe       vtdc employee   select,insert,update,delete,create,drop      ,      123。
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to [email protected] identified by ‘123′;
--   10.163.225.87   joe       vtdc employee   select,insert,update,delete,create,drop      ,      123。  
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to [email protected] identified by ‘123′;
--   10.163.225.87   joe       vtdc            ,      123。
mysql>grant all privileges on vtdc.* to [email protected] identified by ‘123′;
--   10.163.225.87   joe                      ,      123。
mysql>grant all privileges on *.* to [email protected] identified by ‘123′;
--     joe                      ,      123。
mysql>grant all privileges on *.* to joe@localhost identified by ‘123′;
--                  
REVOKE ALL PRIVILEGES ON TABLE A,B,C FROM U1,U2;
--       A         
REVOKE SELECT ON TABLE A FROM PUBLIC;
--   U1  A Tname       
REVOKE UPDATE(Tname) ON TABLE A FROM U1;
--      (mysql)
alter user 'root'@'localhost' identified by '123456'  root     123456