diff options
author | Aaron Ball <nullspoon@iohq.net> | 2015-05-31 14:51:03 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2015-05-31 23:05:08 -0600 |
commit | c36a2dca1a39e25055c33dc179de4cf58e056fe7 (patch) | |
tree | 9b258b06a09fa09a5aa58b5fe8754c6a361fd372 /src/MySql:Find_all_Required_Columns.ascii | |
parent | 1cae83d63794091ed5cc1248a39073a37979e571 (diff) | |
download | oper.io-c36a2dca1a39e25055c33dc179de4cf58e056fe7.tar.gz oper.io-c36a2dca1a39e25055c33dc179de4cf58e056fe7.tar.xz |
A few more articles
Diffstat (limited to 'src/MySql:Find_all_Required_Columns.ascii')
-rw-r--r-- | src/MySql:Find_all_Required_Columns.ascii | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/MySql:Find_all_Required_Columns.ascii b/src/MySql:Find_all_Required_Columns.ascii new file mode 100644 index 0000000..62ba323 --- /dev/null +++ b/src/MySql:Find_all_Required_Columns.ascii @@ -0,0 +1,43 @@ +MySql:Find all Required Columns +=============================== +:author: Aaron Ball +:email: nullspoon@iohq.net + + +== {doctitle} + +I am currently working on a functionality for an app that automatically +enforces database null constraints in the client side code and in the server +side code (if null='no' then print * next to title and require the value be set +in the form postback). Basically, what I need to do is to query the database +for all columns that are Null="NO". Initially, I looked into the show +command... + +---- +'SHOW Columns FROM dbname.tablename where `Null`='NO'; +---- + +That does almost what I want. However, this unfortunately returns more data +than I need, and I'd like to avoid parsing the data if I can get MySql to give +me only the data I want. After searching around a bit more, I discovered that +one of the default databases in MySql contains exactly what I needed: +**information_schema**. + +The query to grab all fields that cannot be null is not actually too +complicated thankfully. + +---- +SELECT column_name FROM information_schema.columns WHERE is_nullable='NO' && table_name='mytable' && table_schema='mydatabase'; +---- + +So here, we're grabbing the column_name field from the columns table within the +information_schema database provided the is_nullable field is equal to 'no'; +The rest is simply filtering it all down so it only returns the column names +for our particular table (the table_name field) inside of our particular +database (the table_schema field). + + +Category:MySQL + + +// vim: set syntax=asciidoc: |