1. 程式人生 > >四種關係型資料庫的基本操作sql命令語句

四種關係型資料庫的基本操作sql命令語句

select COLUMN_NAME AS columnName, DATA_TYPE AS dataType, CHARACTER_OCTET_LENGTH colLength, IS_NULLABLE AS isNull, COLUMN_DEFAULT AS defaultValue, COLUMN_KEY AS isPrimarykey from information_schema.columns where table_name = 'tableName' and table_schema = (select database()) sqlserver: SELECT a.name columnName, b.name datatype, ( CASE
WHEN ( SELECT COUNT (*) FROM sysobjects WHERE ( name IN ( SELECT name FROM sysindexes WHERE (id = a.id) AND ( indid IN ( SELECT indid FROM sysindexkeys WHERE (id = a.id) AND ( colid IN ( SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND (xtype = 'PK')) > 0 THEN 1 ELSE 0
END ) isPrimaryKey, CASE WHEN (charindex('int', b.name) > 0) OR (charindex('time', b.name) > 0) THEN NULL ELSE COLUMNPROPERTY(a.id, a.name, 'PRECISION') END AS colLength, ( CASE WHEN a.isnullable = 1 THEN 1 ELSE 0 END ) isNull, Replace( Replace(IsNull(e. TEXT, ''), '(', ''), ')', '' ) defaultValue FROM
syscolumns a LEFT JOIN systypes b ON a.xtype = b.xusertype INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.name <> 'dtproperties' LEFT JOIN syscomments e ON a.cdefault = e.id LEFT JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id LEFT JOIN sys.extended_properties f ON d.id = f.class AND f.minor_id = 0 WHERE b.name IS NOT NULL AND d.name = N'tableName' oracle: 待定 postgresql: select column_name as columnName,data_type as dataType, coalesce(character_maximum_length,numeric_precision,-1) as colLength, case is_nullable when 'NO' then 0 else 1 end as canNUll,column_default as defaultValue, case when position('nextval' in column_default)>0 then 1 else 0 end as IsIdentity, case when b.pk_name is null then 0 else 1 end as isPrimaryKey from information_schema.columns left join ( select pg_attr.attname as colname,pg_constraint.conname as pk_name from pg_constraint inner join pg_class on pg_constraint.conrelid = pg_class.oid inner join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid and pg_attr.attnum = pg_constraint.conkey[1] inner join pg_type on pg_type.oid = pg_attr.atttypid where pg_class.relname = 'tableName' and pg_constraint.contype='p' ) b on b.colname = information_schema.columns.column_name where table_schema='public' and table_name='tableName'