我需要一个查询(INFORMATION_SCHEMA),它将为给定的架构和表名显示所有具有以下属性的表列(它是什么键类型:PK =>主键,UQ =>唯一键,FK => ;外键,什么是键名,如果是外键,则引用schema.table.column):
<!-- Normal Single Dropdown button -->
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
Action <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
这是我的SQL:
Sub INeedaDate()
Dim r As Range
For Each r In Selection
v = r.Text
r.Value = DateSerial(Left(v, 4), Mid(v, 5, 2), Right(v, 2))
r.NumberFormat = "mm/dd/yyyy"
Next r
End Sub
以下是我的表:
COLUMN_NAME | DATA_TYPE | KEY_TYPE | KEY_NAME | REFERENCED
============+===========+==========+=============+========================
empid | int | PK | PRIMARY |
empname | varchar | UQ | uq_empname |
empactive | enum | | |
empcatid | int | FK | fk_emp_cat | schema.categories.catid
答案 0 :(得分:1)
这是我的最后一个(透视)查询:
SELECT c.COLUMN_NAME,
--c.COLUMN_KEY,
IF(EXISTS(select *
FROM information_schema.KEY_COLUMN_USAGE k
JOIN information_schema.TABLE_CONSTRAINTS tc
ON (k.TABLE_SCHEMA=tc.TABLE_SCHEMA
AND k.TABLE_NAME=tc.TABLE_NAME
AND k.CONSTRAINT_NAME=tc.CONSTRAINT_NAME)
WHERE k.TABLE_SCHEMA=c.TABLE_SCHEMA
AND k.TABLE_NAME=c.TABLE_NAME
AND tc.CONSTRAINT_TYPE='PRIMARY KEY'
AND c.COLUMN_NAME=k.COLUMN_NAME),'PK',null) AS PK,
IF(EXISTS(select *
FROM information_schema.KEY_COLUMN_USAGE k
JOIN information_schema.TABLE_CONSTRAINTS tc
ON (k.TABLE_SCHEMA=tc.TABLE_SCHEMA
AND k.TABLE_NAME=tc.TABLE_NAME
AND k.CONSTRAINT_NAME=tc.CONSTRAINT_NAME)
WHERE k.TABLE_SCHEMA=c.TABLE_SCHEMA
AND k.TABLE_NAME=c.TABLE_NAME
AND tc.CONSTRAINT_TYPE='UNIQUE'
AND c.COLUMN_NAME=k.COLUMN_NAME),'UQ',null) AS UQ,
IF(EXISTS(select *
FROM information_schema.KEY_COLUMN_USAGE k
JOIN information_schema.TABLE_CONSTRAINTS tc
ON (k.TABLE_SCHEMA=tc.TABLE_SCHEMA
AND k.TABLE_NAME=tc.TABLE_NAME
AND k.CONSTRAINT_NAME=tc.CONSTRAINT_NAME)
WHERE k.TABLE_SCHEMA=c.TABLE_SCHEMA
AND k.TABLE_NAME=c.TABLE_NAME
AND tc.CONSTRAINT_TYPE='FOREIGN KEY'
AND c.COLUMN_NAME=k.COLUMN_NAME),'FK',null) AS FK,
c.EXTRA,
c.IS_NULLABLE,
c.DATA_TYPE,
c.COLUMN_TYPE,
c.CHARACTER_MAXIMUM_LENGTH,
c.COLUMN_COMMENT,
k.REFERENCED_TABLE_SCHEMA,
k.REFERENCED_TABLE_NAME,
k.REFERENCED_COLUMN_NAME
FROM information_schema.COLUMNS c
LEFT JOIN information_schema.KEY_COLUMN_USAGE k
ON (k.TABLE_SCHEMA=c.TABLE_SCHEMA
AND k.TABLE_NAME=c.TABLE_NAME
AND k.COLUMN_NAME=c.COLUMN_NAME
AND k.POSITION_IN_UNIQUE_CONSTRAINT IS NOT NULL)
WHERE c.TABLE_SCHEMA='PHPDAO'
AND c.TABLE_NAME='employees';
但我认为必须有更简单的解决方案。