| Author |
Topic |
|
twinklestar0802
Starting Member
17 Posts |
Posted - 2005-10-24 : 00:26:33
|
| Hi. I would like to retrieve the table names of a database, the column names nad its contraints of each table in a database.. How can this be achieved??? |
|
|
activecrypt
Posting Yak Master
165 Posts |
Posted - 2005-10-24 : 00:45:24
|
| Hi,you may fire the query as :select * from information_schema.tables where table_catalog ='dpms'select * from INFORMATION_SCHEMA.COLUMNSto get the desired output .-----------------------------------------------------------MSSQL Server encryption software http://www.activecrypt.com |
 |
|
|
activecrypt
Posting Yak Master
165 Posts |
Posted - 2005-10-24 : 00:49:00
|
| Hi,you may fire the query as :select * from information_schema.tables where table_catalog ='dpms'select * from INFORMATION_SCHEMA.COLUMNSto get the desired output .-----------------------------------------------------------MSSQL Server encryption software http://www.activecrypt.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-24 : 01:23:26
|
| Try this alsoselect CU.table_name, CU.Column_Name,CU.constraint_Namefrom INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU inner joinINFORMATION_SCHEMA.TABLE_CONSTRAINTS TC on CU.Constraint_Name=TC.Constraint_NameMadhivananFailing to plan is Planning to fail |
 |
|
|
twinklestar0802
Starting Member
17 Posts |
Posted - 2005-10-24 : 01:36:09
|
| Hi, Thank you very much to those who replied but I do not understand.Can you guys give me an example? I have a northwind database.. I want to get all the tables and columns of each tables with their contraints specifying if they are primary or foreign keys...Can you guys give me an example |
 |
|
|
activecrypt
Posting Yak Master
165 Posts |
Posted - 2005-10-24 : 01:42:45
|
| Hi,select * from information_schema.tables where table_catalog ='northwind'gouse northwindgoselect * from INFORMATION_SCHEMA.COLUMNSgoHTH-----------------------------------------------------------MSSQL Server encryption software http://www.activecrypt.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-24 : 01:50:55
|
| Use northwindselect CU.table_name, CU.Column_Name,CU.constraint_Namefrom INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU inner joinINFORMATION_SCHEMA.TABLE_CONSTRAINTS TC on CU.Constraint_Name=TC.Constraint_NameMadhivananFailing to plan is Planning to fail |
 |
|
|
twinklestar0802
Starting Member
17 Posts |
Posted - 2005-10-24 : 02:07:17
|
| Hi I just got it.. Thanks guys...What does Information_Schema mean? Where can i get more information on this? I also want to know how to determine if it allows null values and what kind of data type it is...Please help |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-24 : 02:13:10
|
| >>What does Information_Schema mean? Its the owner of the System ViewsSee more info in Books On Line, SQL Server help file>>I also want to know how to determine if it allows null values and what kind of data type it is...Did you run this query?select * from INFORMATION_SCHEMA.COLUMNS where table_Name='YourTable'MadhivananFailing to plan is Planning to fail |
 |
|
|
activecrypt
Posting Yak Master
165 Posts |
Posted - 2005-10-24 : 02:14:31
|
| Hi,suggest to read it in BOL you will get almost all you wants .HTH-----------------------------------------------------------MSSQL Server encryption software http://www.activecrypt.com |
 |
|
|
twinklestar0802
Starting Member
17 Posts |
Posted - 2005-10-25 : 03:27:22
|
Hi.. I did it but I have results which I cant understand..I tried it with the pubs database. The constraints column gave me results such as UPKCL_titleidind, FK__titleauth__au_id__0519C6AF andPK__jobs__117F9D94.. What do they mean?? This is my query thanks to the people who helped me:----SELECT T.TABLE_NAME AS [Table], COL, DATATYPE, NULLABLE, DEFAULTVALUE, MAXIMUM,CONSTRAINTSFROM INFORMATION_SCHEMA.Tables T LEFT OUTER JOIN(SELECT INFORMATION_SCHEMA.Columns.COLUMN_NAME AS COL, iNFORMATION_SCHEMA.Columns.TABLE_NAME AS TAB, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.CONSTRAINT_NAME AS CONSTRAINTS,INFORMATION_SCHEMA.Columns.COLUMN_DEFAULT AS DEFAULTVALUE,INFORMATION_SCHEMA.Columns.IS_NULLABLE AS NULLABLE, INFORMATION_SCHEMA.Columns.DATA_TYPE AS DATATYPE,INFORMATION_SCHEMA.Columns.CHARACTER_MAXIMUM_LENGTH AS MAXIMUMFROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE RIGHT JOIN INFORMATION_SCHEMA.ColumnsON iNFORMATION_SCHEMA.Columns.COLUMN_NAME=INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.COLUMN_NAMEANDiNFORMATION_SCHEMA.Columns.TABLE_NAME=INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.TABLE_NAME)DATABASE_INFO ONDATABASE_INFO.TAB=T.TABLE_NAMEWHERE T.TABLE_NAME NOT LIKE 'sys%'AND T.TABLE_NAME <> 'dtproperties'AND T.TABLE_SCHEMA <> 'INFORMATION_SCHEMA'AND T.TABLE_TYPE!='VIEW'ORDER BY T.TABLE_NAME----Can someone also edit the query for me? I want to combine and foreign key and primary key constraints into one row.. This is the result of my current query:Example:Column Constraintsau_id PKau_id FKI want to do this: (Can someone help me?) Column Constraintsau_id PK and FK |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-25 : 04:09:01
|
| Why do you want to do this?Where do you want to show these data?MadhivananFailing to plan is Planning to fail |
 |
|
|
twinklestar0802
Starting Member
17 Posts |
Posted - 2005-10-25 : 05:52:09
|
| I am making a program that will store data in the database. My program will put all of the columns and tables in a list for reference... Whenever the user inputs a certain data for a specific field, I check the validity through the constraints, nullable etc... I want to put the FK and PK in a single column |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-10-25 : 06:37:26
|
| "The constraints column gave me results such as UPKCL_titleidind, FK__titleauth__au_id__0519C6AF andPK__jobs__117F9D94.. What do they mean?? "You didn't provide a constraint name when you created the object - so SQL Server allocated a "computer generated" nameThey are a nuisance because, assuming you used a script, rather than the GUI AND you want to run the script on multiple servers (e.g. Development, Test, Production) then each server will have a different name for the constraint ... so when you want to drop it you won't be able to have a script that runs on all the servers [well, you can, but it will be harder than if they had a human-allocated name]Kristen |
 |
|
|
twinklestar0802
Starting Member
17 Posts |
Posted - 2005-10-25 : 23:42:01
|
TNX!!!!   |
 |
|
|
|