PLSQL Tools for query primary key and foreign key of a table
2017-02-03 16:33
549 查看
SET VERIFY OFF SET SERVEROUTPUT ON DECLARE V_TABLE_NAME VARCHAR2(100); -- 表名 V_PK_NAME VARCHAR2(100); -- 主キー制約の名前 V_COLUMNS VARCHAR2(3000); -- 列名の取得(列1,列2,・・) BEGIN -- 表名の取得(キーボードから入力) V_TABLE_NAME := UPPER('&TABLE_NAME'); -- 主キー制約の名前の取得 SELECT CONSTRAINT_NAME INTO V_PK_NAME FROM USER_CONSTRAINTS WHERE TABLE_NAME = V_TABLE_NAME AND CONSTRAINT_TYPE = 'P'; -- 取得した主キーの列名(複合例)の取得 FOR REC IN ( SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = V_PK_NAME ORDER BY POSITION ) LOOP -- "列名," を後ろに追記する V_COLUMNS := V_COLUMNS || REC.COLUMN_NAME || ','; END LOOP; -- 最後の余分な','をとっている V_COLUMNS := SUBSTR(V_COLUMNS,1,LENGTH(V_COLUMNS)-1); -- "列名,列名"の前後をかっこ()で囲む V_COLUMNS := '(' || V_COLUMNS || ')'; DBMS_OUTPUT.PUT_LINE('Table Name:' || V_TABLE_NAME || ' Primary Key :' || V_COLUMNS); -- この表を参照する子表の外部キー制約名を取得し、 -- 子表の名前と外部キー列を表示する FOR REC1 IN ( SELECT CONSTRAINT_NAME, TABLE_NAME FROM USER_CONSTRAINTS WHERE R_CONSTRAINT_NAME = V_PK_NAME AND CONSTRAINT_TYPE = 'R' ) LOOP -- さらに外部キー制約名から、列名(複合列)を変数に取得する V_COLUMNS := NULL; FOR REC2 IN ( SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = REC1.CONSTRAINT_NAME ORDER BY POSITION ) LOOP V_COLUMNS := V_COLUMNS || REC2.COLUMN_NAME || ','; END LOOP; V_COLUMNS := SUBSTR(V_COLUMNS,1,LENGTH(V_COLUMNS)-1); V_COLUMNS := '(' || V_COLUMNS || ')'; -- 子表の情報を画面表示 DBMS_OUTPUT.PUT_LINE('Child Table Name:' || REC1.TABLE_NAME || ' Foreign Key :' || V_COLUMNS); END LOOP; END;
相关文章推荐
- sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database
- SQL Server: Get table primary key and Foreign Key using sql query
- SQL Server: Get table primary key and Foreign Key using sql query
- Find out all the Primary Key and Foreign Key Constraints in a table
- EJB3: Mapping of 1-to-1 relationships when primary key in the source table is also a foreign key for the target entity
- MySQL Error Query database. Causejava.sql.SQLException: Incorrect key file for table ‘/tmp/#sql_181c
- Adding Primary Keys and Foreign Keys to a Table
- one of methods of query table a and table b
- LOV for Editable Primary Key in ADF Table Problem
- valgrind - a suite of tools for debugging and profiling programs
- phoenix create table Order of columns in primary key constraint must match the order in which they'r
- ORA-02429: cannot drop index used for enforcement of unique/primary key
- Debugging Tools for Windows - List of Tools and Documentation
- Database Primary key and Foreign key [From Internet]
- 解决:Reading table information for completion of table and column names
- ORA-02429: cannot drop index used for enforcement of unique /primary key
- DSP Tools, System Generator for DSP, AccelDSP - Which versions of System Generator for DSP and Accel
- 解决MYSQL访问时无法切换数据库的问题.Reading table information for completion of table and column names
- TACAS(International Conference on Tools and Algorithms for the Construction and Analysis of Systems)到底是什么档次的会议?
- Design for Trustworthy Software: Tools, Techniques, and Methodology of Developing Robust Software