I want to get a particular table's primary key using SQL query for SQL Server database.  In MySQL I am using following query to get table primary key:  SHOW KEYS FROM tablename WHERE Key_name = 'PRIMARY'   What is equivalent of above query for SQL Server ?.  If There is a query that will work for both MySQL and SQL Server then It will be an ideal case.
91893
Simple question, how do you list the primary key of a table with T-SQL? I know how to get indexes on a table, but can't remember how to get the PK.
20387
I have two tables A &amp; B. Table A id | name | num  Table B id | date | roll  id in table B is a foreign key which relates to the id in table A. I want to get the contents of table B where id is given and also along with that data, I need the name whose id is given from table A. NOTE: cannot use multiple queries separated by commas; need to do both the SELECTs in one query.
97492
I have a MySQL database table with nearly 23 million records. This table has no primary key, because nothing is unique. It has 2 columns, both are indexed. Below is its structure:    Below is some of its data:    Now, I ran a simple query:  SELECT `indexVal` FROM `key_word` WHERE `hashed_word`='001'   Unfortunately, this took more than 5 seconds to retrieve the data and show them to me. My future table will have 150 billion records, so this time is very very high.  I ran the Explain command to see what's going on. Result is below.    Then I ran the Profile using below command.  SET profiling=1; SELECT `indexVal` FROM `key_word` WHERE `hashed_word` = '001'; SHOW profile;   Below is the result of the profiling:    Below is some more information about my table:    So, why this is taking so long? They are indexed too! In future, I have to run lot of LIKE commands, so this is taking too much time. What has gone wrong?
99140
Can we have a SQL query which will basically help in viewing table and index sizes in SQl Server.  How SQL server maintains memory usage for  tables/indexes?
2165
What is a  free tool to compare two Sql Server tables (data and schema).   It would be great if the tool can script the differences found.  I also went through some older . The closest I have seen is  but I would love to try more options.
18713
When looking at the properties of a particular login, it's possible to see a list of users mapped to that login:   I profiled SQL Server Management Studio (SSMS) and I see that SSMS connects to every database one at a time and retrieves information from sys.database_permissions  Is it possible to write a single query that retrieves the user mapping information shown above or am I forced to use a cursor or sp_MSforeachdb or something like that?
44610
I need to create a background job that processes a table looking for rows matching on a particular id with different statuses. It will store the row data in a string to compare the data against a row with a matching id. I know the syntax to get the row data, but I have never tried comparing 2 rows from the same table before. How is it done? Would I need to use variables to store the data from each? Or some other way? (Using SQL Server 2008)
80985
We all know that a simple statement such as:  SELECT * FROM stuff;   should not produce an ordered result. However, when I try to demonstrate this, it always comes out in primary key order.  Moreover a statement such as:  SELECT thing,whatever FROM stuff GROUP BY thing,whatever;   always appears to sort thing by the last field in the GROUP BY clause, which is not at all helpful.  The question is, under what circumstances does SQL SERVER order the result even if unasked, and what can I do to stop this?  I am trying to demonstrate to my students that order is indeterminate unless specified, but this is not helping my case.  I admit that I am working with a small set of sample data.  Thanks
71359
Say, we have a query like this:  select a.*,b.* from  a join b on a.col1=b.col1 and len(a.col1)=10   Assuming the above query uses a Hash Join and has a residual, the probe key will be col1 and the residual will be len(a.col1)=10.  But while going through another example, I could see both the probe and the residual to be the same column. Below is an elaboration on what I am trying to say:  Query:  select * from T1 join T2 on T1.a = T2.a    Execution plan, with probe and residual highlighted:         Test data:    create table T1 (a int, b int, x char(200)) create table T2 (a int, b int, x char(200))  set nocount on declare @i int set @i = 0 while @i &lt; 1000   begin       insert T1 values (@i * 2, @i * 5, @i)     set @i = @i + 1   end  declare @i int set @i = 0 while @i &lt; 10000   begin     insert T2 values (@i * 3, @i * 7, @i)     set @i = @i + 1   end   Question:    How can a probe and a residual be the same column? Why can't SQL Server use the probe column only? Why does it have to use the same column as a residual to filter rows again?  References for test data:
38285
Both these joins will give me the same results:  SELECT * FROM table JOIN otherTable ON table.ID = otherTable.FK   vs  SELECT * FROM table INNER JOIN otherTable ON table.ID = otherTable.FK   Is there any difference between the statements in performance or otherwise?   Does it differ between different SQL implementations?
98355
With java.sql.ResultSet is there a way to get a column's name as a String by using the column's index? I had a look through the API doc but I can't find anything.
46559
One of my columns is called from. I can't change the name because I didn't make it. Am I allowed to do something like SELECT from FROM TableName or is there a special syntax to avoid the SQL Server being confused?
67461
What is the most efficient way to read the last row with SQL Server?  The table is indexed on a unique key -- the "bottom" key values represent the last row.
799
While trying to write a query, I found out (the hard way) that SQL Server parses WHEREs in a query long before parsing the SELECTs when executing a query.  The  say that the general logical parsing order is such that SELECT is parsed nearly last (thus resulting in "no such object [alias]" errors when trying to use a column alias in other clauses). There was even  to allow for aliases to be used anywhere, which was shot down by the Microsoft team, citing ANSI standards compliance issues (which suggests that this behavior is part of the ANSI standard).  As a programmer (not a DBA), I found this behavior somewhat confusing, since it seems to me that it largely defeats the purpose of having column aliases (or, at the very least, column aliases could be made significantly more powerful if they were parsed earlier in the query execution), since the only place you can actually use the aliases is in ORDER BY. As a programmer, it seems like it's missing a huge opportunity for making queries more powerful, convenient, and DRY.  It looks like it's such a glaring issue that it stands to reason, then, that there are other reasons for deciding that column aliases shouldn't be allowed in anything other than SELECT and ORDER BY, but what are those reasons?
20023
Is there are unified way of how to check for the existance of an INDEX for a given column irregardless of the actual SQL database system used?  For MySQL one could for instance check for the existance using SHOW CREATE TABLE mytable. In the result there would be something like this if column mycolumn has an index: KEY 'Index_1' ('mycolumn').  Is this indicator KEY unified among all SQL database systems?  Are there better ways to check for an index?
108409
When I write a query like this...  select * from table1 t1 join table2 t2 on t1.id = t2.id   Does the SQL optimizer, not sure if that is the correct term, translate that to...  select * from table1 t1, table2 t2 where t1.id = t2.id   Essentially, is the Join statement in SQL Server just an easier way to write sql? Or is it actually used at run-time?  Edit: I almost always, and will almost always, use the Join syntax. I am just curious what happens.
13602
I'm looking for the alternative of mysql_real_escape_string() for SQL Server. Is addslashes() my best option or there is another alternative function that can be used?  An alternative for mysql_error() would also be useful.