我想列出所有表和每个表计数列和我的数据库中使用的(主键+外键)的每个表计数
EG:表1包含2列和一个主键,结果应如下所示
Tables List Total columns Primary+foreign Key count 1 2 1
答案 0 :(得分:1)
试试这个
for i in 1...n {
doubles.append(myFunction(i))
}
答案 1 :(得分:0)
-- All info
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
ORDER BY TABLE_NAME
--Count Info
SELECT
table_name,count(column_name) ColCount,count(constraint_name) ConstraintCount
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
group by table_name
--Seperate Count Info
SELECT
a.table_name,count(a.column_name) ColCount,count(a.constraint_name) ConstraintCount,
(SELECT
count(b.constraint_name)
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE b where b.constraint_name like 'PK%'
AND b.table_name = a.table_name
group by table_name ) PKCount,
(SELECT
count(b.constraint_name)
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE b where b.constraint_name like 'FK%'
AND b.table_name = a.table_name
group by table_name ) FKCount
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE a
group by a.table_name
答案 2 :(得分:0)
试试这个:
select name,
count_columns = (select COUNT(*) from syscolumns where id = S.ID),
count_FK_PK = (select COUNT(*) from sysobjects where (xtype = 'F' OR xtype = 'PK') AND parent_obj = S.id)
from sysobjects S
where xtype='U'
答案 3 :(得分:0)
尝试下面一个
SELECT COUNT(DISTINCT T.NAME) TABLECOUNT, COUNT(C.NAME) COLUMNCOUNT, SUM(CASE WHEN T.TYPE LIKE 'PK' THEN 1 ELSE 0 END) PRIMARYCOUNT,
SUM(CASE WHEN T.TYPE = 'F' THEN 1 ELSE 0 END) FORIEGNCOUNT
FROM SYS.OBJECTS T LEFT JOIN SYS.COLUMNS C
ON T.OBJECT_ID = C.OBJECT_ID
或
SELECT COUNT(DISTINCT T.NAME) TABLECOUNT, COUNT(C.NAME) COLUMNCOUNT,
(SUM(CASE WHEN T.TYPE LIKE 'PK' THEN 1 ELSE 0 END) +
SUM(CASE WHEN T.TYPE = 'F' THEN 1 ELSE 0 END)) 'PK+FK'
FROM SYS.OBJECTS T LEFT JOIN SYS.COLUMNS C
ON T.OBJECT_ID = C.OBJECT_ID
答案 4 :(得分:0)
select q1.TABLE_NAME,q2.cnt,q1.pkandfk from (select scr.TABLE_NAME, sum(scr.pkandfk) as pkandfk from (select itc.TABLE_NAME,case
when itc.CONSTRAINT_TYPE in ('FOREIGN KEY','PRIMARY KEY') then 1
else 0
end as pkandfk from INFORMATION_SCHEMA.TABLE_CONSTRAINTS itc
join INFORMATION_SCHEMA.KEY_COLUMN_USAGE ikcu
on ikcu.CONSTRAINT_NAME=itc.CONSTRAINT_NAME) scr
group by scr.TABLE_NAME)
q1 join (select TABLE_NAME,count(column_name)as cnt from INFORMATION_SCHEMA.COLUMNS group by table_name) q2
on q1.TABLE_NAME=q2.TABLE_NAME