Stored Procedure and System Tables

System Tables Which contains Stored Procedure Text
SYSCOMMENTS
System table located in all databases containing entries for each view, rule, default, trigger, CHECK constraint, DEFAULT constraint, and stored procedure in the database. The text column contains the original SQL definition statements, which are limited to a maximum size of 4 MB.

SYSCOLUMNS
System table located in all databases containing one row for every column in each table and each view, and a row for each parameter in a stored procedure.

SYSDEPENDS
System table located in all databases containing dependency information between objects (views, procedures, and triggers), and the objects (tables, views, and procedures) contained in their definition.

SYSCACHEOBJECTS
System table located in the master database containing information about how the cache is used.

SQL Server Database Coding Standards

Please visit http://sqldbpool.blogspot.com/

(for more database related articles)

Databases are the heart and soul of many of the recent enterprise applications and it is very essential to pay special attention to database programming. I’ve seen in many occasions where database programming is overlooked, thinking that it’s something easy and can be done by anyone. This is wrong. For a better performing database you need a real DBA and a specialist database programmer, let it be Microsoft SQL Server, Oracle, Sybase, DB2 or whatever! If you don’t use database specialists during your development cycle, database often ends up becoming the performance bottleneck. I decided to write this article, to put together some of the database programming best practices, so that my fellow DBAs and database developers can benefit!

Here are some of the programming guidelines, best practices, keeping quality, performance and maintainability in mind.

  • Decide upon a database naming convention, standardize it across your organization and be consistent in following it. It helps make your code more readable and understandable.
  • Do not depend on undocumented functionality. The reasons being:
    – You will not get support from Microsoft, when something goes wrong with your undocumented code
    – Undocumented functionality is not guaranteed to exist (or behave the same) in a future release or service pack, there by breaking your code
  • Try not to use system tables directly. System table structures may change in a future release. Wherever possible, use the sp_help* stored procedures or INFORMATION_SCHEMA views. There will be situattions where you cannot avoid accessing system table though!
  • Make sure you normalize your data at least till 3rd normal form. At the same time, do not compromise on query performance. A little bit of denormalization helps queries perform faster.
  • Write comments in your stored procedures, triggers and SQL batches generously, whenever something is not very obvious. This helps other programmers understand your code clearly. Don’t worry about the length of the comments, as it won’t impact the performance, unlike interpreted languages like ASP 2.0.
  • Do not use SELECT * in your queries. Always write the required column names after the SELECT statement, like SELECT CustomerID, CustomerFirstName, City. This technique results in less disk IO and less network traffic and hence better performance.
  • Try to avoid server side cursors as much as possible. Always stick to ‘set based approach’ instead of a ‘procedural approach’ for accessing/manipulating data. Cursors can be easily avoided by SELECT statements in many cases. If a cursor is unavoidable, use a simpleWHILE loop instead, to loop through the table. I personally tested and concluded that a WHILE loop is faster than a cursor most of the times. But for a WHILE loop to replace a cursor you need a column (primary key or unique key) to identify each row uniquely and I personally believe every table must have a primary or unique key.
  • Avoid the creation of temporary tables while processing data, as much as possible, as creating a temporary table means more disk IO. Consider advanced SQL or views or table variables of SQL Server 2000 or derived tables, instead of temporary tables. Keep in mind that, in some cases, using a temporary table performs better than a highly complicated query.
  • Try to avoid wildcard characters at the beginning of a word while searching using the LIKE keyword, as that results in an index scan, which is defeating the purpose of having an index. The following statement results in an index scan, while the second statement results in an index seek:1. SELECT LocationID FROM Locations WHERE Specialities LIKE ‘%pples’
    2.
    SELECT LocationID FROM Locations WHERE Specialities LIKE ‘A%s’ Also avoid searching with not equals operators (<> and NOT) as they result in table and index scans. If you must do heavy text-based searches, consider using the Full-Text search feature of SQL Server for better performance.
  • Use ‘Derived tables’ wherever possible, as they perform better. Consider the following query to find the second highest salary from Employees table: SELECT MIN(Salary)
    FROM Employees
    WHERE EmpID IN
    (
    SELECT TOP 2 EmpID
    FROM Employees
    ORDER BY Salary Desc
    )
    The same query can be re-written using a derived table as shown below, and it performs twice as fast as the above query: SELECT MIN(Salary)
    FROM
    (
    SELECT TOP 2 Salary
    FROM Employees
    ORDER BY Salary Desc
    ) AS A
    This is just an example, the results might differ in different scenarios depending upon the database design, indexes, volume of data etc. So, test all the possible ways a query could be written and go with the efficient one. With some practice and understanding of ‘how SQL Server optimizer works’, you will be able to come up with the best possible queries without this trial and error method.
  • While designing your database, design it keeping ‘performance’ in mind. You can’t really tune performance later, when your database is in production, as it involves rebuilding tables/indexes, re-writing queries. Use the graphical execution plan in Query Analyzer or SHOWPLAN_TEXT or SHOWPLAN_ALL commands to analyze your queries. Make sure your queries do ‘Index seeks’ instead of ‘Index scans’ or ‘Table scans’. A table scan or an index scan is a very bad thing and should be avoided where possible (sometimes when the table is too small or when the whole table needs to be processed, the optimizer will choose a table or index scan).
  • Prefix the table names with owner names, as this improves readability, avoids any unnecessary confusions. Microsoft SQL Server Books Online even states that qualifying tables names, with owner names helps in execution plan reuse.
  • Use SET NOCOUNT ON at the beginning of your SQL batches, stored procedures and triggers in production environments, as this suppresses messages like ‘(1 row(s) affected)’ after executing INSERT, UPDATE, DELETE and SELECT statements. This in turn improves the performance of the stored procedures by reducing the network traffic.
  • Use the more readable ANSI-Standard Join clauses instead of the old style joins. With ANSI joins the WHERE clause is used only for filtering data. Where as with older style joins, the WHERE clause handles both the join condition and filtering data. The first of the following two queries shows an old style join, while the second one shows the new ANSI join syntax: SELECT a.au_id, t.title
    FROM titles t, authors a, titleauthor ta
    WHERE
    a.au_id = ta.au_id AND
    ta.title_id = t.title_id AND
    t.title LIKE ‘%Computer%’SELECT a.au_id, t.title
    FROM authors a
    INNER JOIN
    titleauthor ta
    ON
    a.au_id = ta.au_id
    INNER JOIN
    titles t
    ON
    ta.title_id = t.title_id
    WHERE t.title LIKE ‘%Computer%’
    Be aware that the old style *= and =* left and right outer join syntax may not be supported in a future release of SQL Server, so you are better off adopting the ANSI standard outer join syntax.
  • Do not prefix your stored procedure names with ‘sp_’. The prefix sp_ is reserved for system stored procedure that ship with SQL Server. Whenever SQL Server encounters a procedure name starting with sp_,, it first tries to locate the procedure in the master database, then looks for any qualifiers (database, owner) provided, then using dbo as the owner. So, you can really save time in locating the stored procedure by avoiding sp_ prefix. But there is an exception! While creating general purpose stored procedures that are called from all your databases go ahead and prefix those stored procedure names with sp_ and create them in the master database.
  • Views are generally used to show specific data to specific users based on their interest. Views are also used to restrict access to the base tables by granting permission on only views. Yet another significant use of views is that, they simplify your queries. Incorporate your frequently required complicated joins and calculations into a view, so that you don’t have to repeat those joins/calculations in all your queries, instead just select from the view.
  • Use ‘User Defined Datatypes’, if a particular column repeats in a lot of your tables, so that the datatype of that column is consistent across all your tables.
  • Do not let your front-end applications query/manipulate the data directly using SELECT or INSERT/UPDATE/DELETE statements. Instead, create stored procedures, and let your applications access these stored procedures. This keeps the data access clean and consistent across all the modules of your application, at the same time centralizing the business logic within the database.
  • Try not to use text, ntext datatypes for storing large textual data. ‘text‘ datatype has some inherent problems associated with it. You can not directly write, update text data using INSERT, UPDATE statements (You have to use special statements like READTEXT, WRITETEXT and UPDATETEXT). There are a lot of bugs associated with replicating tables containing text columns. So, if you don’t have to store more than 8 KB of text, use char(8000) or varchar(8000)datatypes.
  • If you have a choice, do not store binary files, image files (Binary large objects or BLOBs) etc. inside the database. Instead store the path to the binary/image file in the database and use that as a pointer to the actual binary file. Retrieving, manipulating these large binary files is better performed outside the database and after all, database is not meant for storing files.
  • Use char data type for a column, only when the column is non-nullable. If a char column is nullable, it is treated as a fixed length column in SQL Server 7.0+. So, a char(100), when NULL, will eat up 100 bytes, resulting in space wastage. So, use varchar(100) in this situation. Of course, variable length columns do have a very little processing overhead over fixed length columns. Carefully choose between char and varchar depending up on the length of the data you are going to store.
  • Avoid dynamic SQL statements as much as possible. Dynamic SQL tends to be slower than static SQL, as SQL Server must generate an execution plan every time at runtime. IF and CASE statements come in handy to avoid dynamic SQL. Another major disadvantage of using dynamic SQL is that, it requires the users to have direct access permissions on all accessed objects like tables and views. Generally, users are given access to the stored procedures which reference the tables, but not directly on the tables. In this case, dynamic SQL will not work. Consider the following scenario, where a user named ‘dSQLuser’ is added to the pubs database, and is granted access to a procedure named ‘dSQLproc’, but not on any other tables in the pubs database. The procedure dSQLproc executes a direct SELECT on titles table and that works. The second statement runs the same SELECT on titles table, using dynamic SQL and it fails with the following error:Server: Msg 229, Level 14, State 5, Line 1
    SELECT permission denied on object ‘titles’, database ‘pubs’, owner ‘dbo’.To reproduce the above problem, use the following commands:

    sp_addlogin ‘dSQLuser’
    GO
    sp_defaultdb ‘dSQLuser’, ‘pubs’
    USE pubs
    GO
    sp_adduser ‘dSQLUser’, ‘dSQLUser’
    GO
    CREATE PROC dSQLProc
    AS
    BEGIN
    SELECT * FROM titles WHERE title_id = ‘BU1032’
    –This works
    DECLARE @str CHAR(100)
    SET @str = ‘SELECT * FROM titles WHERE title_id = ”BU1032”’
    EXEC (@str)
    –This fails
    END
    GO
    GRANT EXEC ON dSQLProc TO dSQLuser
    GO
    Now login to the pubs database using the login dSQLuser and execute the procedure dSQLproc to see the problem.

  • Consider the following drawbacks before using IDENTITY property for generating primary keys. IDENTITY is very much SQL Server specific, and you will have problems if you want to support different database backends for your application.IDENTITY columns have other inherent problems. IDENTITY columns run out of numbers one day or the other. Numbers can’t be reused automatically, after deleting rows. Replication and IDENTITY columns don’t always get along well. So, come up with an algorithm to generate a primary key, in the front-end or from within the inserting stored procedure. There could be issues with generating your own primary keys too, like concurrency while generating the key, running out of values. So, consider both the options and go with the one that suits you well.
  • Minimize the usage of NULLs, as they often confuse the front-end applications, unless the applications are coded intelligently to eliminate NULLs or convert the NULLs into some other form. Any expression that deals with NULL results in a NULL output. ISNULL and COALESCE functions are helpful in dealing with NULL values. Here’s an example that explains the problem:Consider the following table, Customers which stores the names of the customers and the middle name can be NULL. CREATE TABLE Customers
    (
    FirstName varchar(20),
    MiddleName varchar(20),
    LastName varchar(20)
    )
    Now insert a customer into the table whose name is Tony Blair, without a middle name: INSERT INTO Customers
    (FirstName, MiddleName, LastName)
    VALUES (‘Tony’,NULL,’Blair’)
    The following SELECT statement returns NULL, instead of the customer name: SELECT FirstName + ‘ ‘ + MiddleName + ‘ ‘ + LastName FROM Customers

    To avoid this problem, use
    ISNULL as shown below: SELECT FirstName + ‘ ‘ + ISNULL(MiddleName + ‘ ‘,”) + LastName FROM Customers
  • Use Unicode datatypes like nchar, nvarchar, ntext, if your database is going to store not just plain English characters, but a variety of characters used all over the world. Use these datatypes, only when they are absolutely needed as they need twice as much space as non-unicode datatypes.
  • Always use a column list in your INSERT statements. This helps in avoiding problems when the table structure changes (like adding a column). Here’s an example which shows the problem.Consider the following table: CREATE TABLE EuropeanCountries
    (
    CountryID int PRIMARY KEY,
    CountryName varchar(25)
    )
    Here’s an INSERT statement without a column list , that works perfectly: INSERT INTO EuropeanCountries
    VALUES (1, ‘Ireland’)

    Now, let’s add a new column to this table:
    ALTER TABLE EuropeanCountries
    ADD EuroSupport bit
    Now run the above INSERT statement. You get the following error from SQL Server:Server: Msg 213, Level 16, State 4, Line 1
    Insert Error: Column name or number of supplied values does not match table definition.This problem can be avoided by writing an

    INSERT statement with a column list as shown below: INSERT INTO EuropeanCountries
    (CountryID, CountryName)
    VALUES (1, ‘England’)

  • Perform all your referential integrity checks, data validations using constraints (foreign key and check constraints). These constraints are faster than triggers. So, use triggers only for auditing, custom tasks and validations that can not be performed using these constraints. These constraints save you time as well, as you don’t have to write code for these validations and the RDBMS will do all the work for you.
  • Always access tables in the same order in all your stored procedures/triggers consistently. This helps in avoiding deadlocks. Other things to keep in mind to avoid deadlocks are: Keep your transactions as short as possible. Touch as less data as possible during a transaction. Never, ever wait for user input in the middle of a transaction. Do not use higher level locking hints or restrictive isolation levels unless they are absolutely needed. Make your front-end applications deadlock-intelligent, that is, these applications should be able to resubmit the transaction incase the previous transaction fails with error 1205. In your applications, process all the results returned by SQL Server immediately, so that the locks on the processed rows are released, hence no blocking.
  • Offload tasks like string manipulations, concatenations, row numbering, case conversions, type conversions etc. to the front-end applications, if these operations are going to consume more CPU cycles on the database server (It’s okay to do simple string manipulations on the database end though). Also try to do basic validations in the front-end itself during data entry. This saves unnecessary network roundtrips.
  • If back-end portability is your concern, stay away from bit manipulations with T-SQL, as this is very much RDBMS specific. Further, using bitmaps to represent different states of a particular entity conflicts with the normalization rules.
  • Consider adding a @Debug parameter to your stored procedures. This can be of bit data type. When a 1 is passed for this parameter, print all the intermediate results, variable contents using SELECT or PRINT statements and when 0 is passed do not print debug information. This helps in quick debugging of stored procedures, as you don’t have to add and remove these PRINT/SELECT statements before and after troubleshooting problems.
  • Do not call functions repeatedly within your stored procedures, triggers, functions and batches. For example, you might need the length of a string variable in many places of your procedure, but don’t call the LEN function whenever it’s needed, instead, call the LEN function once, and store the result in a variable, for later use.
  • Make sure your stored procedures always return a value indicating the status. Standardize on the return values of stored procedures for success and failures. The RETURN statement is meant for returning the execution status only, but not data. If you need to return data, use OUTPUT parameters.
  • If your stored procedure always returns a single row resultset, consider returning the resultset using OUTPUT parameters instead of a SELECT statement, as ADO handles output parameters faster than resultsets returned by SELECT statements.
  • Always check the global variable @@ERROR immediately after executing a data manipulation statement (like INSERT/UPDATE/DELETE), so that you can rollback the transaction in case of an error (@@ERROR will be greater than 0 in case of an error). This is important, because, by default, SQL Server will not rollback all the previous changes within a transaction if a particular statement fails. This behavior can be changed by executing SET XACT_ABORT ON. The @@ROWCOUNT variable also plays an important role in determining how many rows were affected by a previous data manipulation (also, retrieval) statement, and based on that you could choose to commit or rollback a particular transaction.
  • To make SQL Statements more readable, start each clause on a new line and indent when needed. Following is an example: SELECT title_id, title
    FROM titles
    WHERE title LIKE ‘Computing%’ AND
    title LIKE ‘Gardening%’
  • Though we survived the Y2K, always store 4 digit years in dates (especially, when using char or int datatype columns), instead of 2 digit years to avoid any confusion and problems. This is not a problem with datetime columns, as the century is stored even if you specify a 2 digit year. But it’s always a good practice to specify 4 digit years even with datetime datatype columns.
  • In your queries and other SQL statements, always represent date in yyyy/mm/dd format. This format will always be interpreted correctly, no matter what the default date format on the SQL Server is. This also prevents the following error, while working with dates: Server: Msg 242, Level 16, State 3, Line 2
    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
  • As is true with any other programming language, do not use GOTO or use it sparingly. Excessive usage of GOTO can lead to hard-to-read-and-understand code.
  • Do not forget to enforce unique constraints on your alternate keys.
  • Always be consistent with the usage of case in your code. On a case insensitive server, your code might work fine, but it will fail on a case sensitive SQL Server if your code is not consistent in case. For example, if you create a table in SQL Server or database that has a case-sensitive or binary sort order, all references to the table must use the same case that was specified in the CREATE TABLE statement. If you name the table as ‘MyTable’ in the CREATE TABLE statement and use ‘mytable’ in the SELECT statement, you get an ‘object not found’ or ‘invalid object name’ error.
  • Though T-SQL has no concept of constants (like the ones in C language), variables will serve the same purpose. Using variables instead of constant values within your SQL statements, improves readability and maintainability of your code. Consider the following example: UPDATE dbo.Orders
    SET OrderStatus = 5
    WHERE OrdDate < ‘2001/10/25’
    The same update statement can be re-written in a more readable form as shown below: DECLARE @ORDER_PENDING int
    SET @ORDER_PENDING = 5
    UPDATE dbo.Orders
    SET OrderStatus = @ORDER_PENDING
    WHERE OrdDate < ‘2001/10/25’


  • Do not use the column numbers in the ORDER BY clause as it impairs the readability of the SQL statement. Further, changing the order of columns in the SELECT list has no impact on the ORDER BY when the columns are referred by names instead of numbers. Consider the following example, in which the second query is more readable than the first one: SELECT OrderID, OrderDate
    FROM Orders
    ORDER BY 2SELECT OrderID, OrderDate
    FROM Orders
    ORDER BY OrderDate

Pros and Cons of MySQL Table Types

Please visit http://sqldbpool.blogspot.com/

(for more database related articles)

Of all the positive things that MySQL brings to the table, probably the most overlooked is multiple table types. This facet of the application is overlooked as a feature and more importantly is overlooked at design time.

MySQL has six distinct table types.

  • MyISAM
  • MERGE
  • ISAM
  • HEAP
  • InnoDB
  • BDB or BerkeleyDB Tables

Finding a transaction table that’s just my type

Two of these table types currently support transactions. Transactions give the user greater control when working with data. You would use syntax similar to the following for a manual transaction.

START TRANSACTION;

SELECT @A:=SUM(salary) FROM table1 WHERE type=1;

UPDATE table2 SET summmary=@A WHERE type=1;

COMMIT;

Of the two commonly used transaction table types, the first is BerkeleyDB transactional tables provided by SleepyCat (www.sleepycat.com). In order to use BDB tables use a binary with BDB support or configure the source with the withberkeleydb option. If you don’t want to use BDB tables, start the MySQL server with the skipbdb option. This will save a lot of memory, since the BDB library won’t be included. However, you won’t be able to use BDB tables. BDB is not used nearly as much as our second alternative which is InnoDB. InnoDB features rowlevel locking, consistent nonlocking read in SELECTs and common tablespace for all tables.

InnoDB Tables are made by Innobase Oy (www.innodb.com), and are distributed under the GPL as well as commercially. If you need commercial support or commercial licenses for your application and cost is a concern, not using InnoDB will save you about 20-50 % for licenses and support contracts. If data integrity is a concern InnoDB provides MySQL with a transactional storage engine and crash recovery capabilities. InnoDB has been designed for maximum performance when processing large data volumes and any other diskbased relational database engine does probably not match CPU efficiency. There are other transactional table types out there (such as Gemini), but they do not seem to be used any more than BDB. So, in a nutshell, most users prefer the speed and features of InnoDB.

A Database is no fun when you are locked out

The default table type for MySQL is MyISAM. It has table level locking, which means during an UPDATE, nobody can access any other record of the same table. BDB uses Page level locking, and during an UPDATE, nobody can access any other record residing in the same database page of that table, until the locking transaction issues a COMMIT.

InnoDB however, uses Row level locking. Row level locking ensures that during an UPDATE, nobody can access that particular row, until the locking transaction issues a COMMIT. Any of the above table types will probably be fine for a web server, but in a LAN application can cause unecessary issues.

Special circumstances call for special tools (or tables)

MERGE tables are a collection of identical MyISAM tables that can be used as one. You can only SELECT, DELETE, and UPDATE from the collection of tables. If you DROP the MERGE table, you are only dropping the MERGE specification. One reasons why you would use MERGE tables is to get more speed. You can split a big read-only table and then put the different table parts on different disks. You could do more efficient searches. If you know exactly what you are looking for, you can search in just one of the split tables for some queries and use a MERGE table for others. Repairs are more efficient. It’s easier to repair the individual files that are mapped to a MERGE file than trying to repair a really big file. MyISAM and therefore MERGE tables are represented as individual files on the harddrive. You can go around the file-size limit for the operating system.

Some of the disadvantages of using MERGE tables are:

  • You can only use identical MyISAM tables for a MERGE table.
  • REPLACE doesn’t work.
  • Key reads are slower.

Also, you can’t do DROP TABLE, ALTER TABLE, DELETE FROM table_name without a WHERE clause, REPAIR TABLE, TRUNCATE TABLE, OPTIMIZE TABLE, or ANALYZE TABLE on any of the table that is mapped by a MERGE table that is “open”. If you do this, the MERGE table may still refer to the original table and you will get unexpected results. The easiest way to get around this deficiency is to issue the FLUSH TABLES command, ensuring no MERGE tables remain “open”.

Well, that should make you think twice about using MERGE tables. ISAM tables will disappear in MySQL version 5.0, so it wouldn’t be a good idea to use them. Last but not least is the HEAP table type. HEAP tables use hashed indexes and are stored in memory. This makes them very fast, but if MySQL crashes you will lose all data stored in them. They are very useful for temporary tables. HEAP sounds cool but I don’t think the risk justifies the performance.

The Lowdown on MySQL Table Types

Most people use MyISAM if they need speed and InnoDB for data integrity. You can use more than one or any combination of these table types in your database. Remember to asses the needs of your application before building it. Even though MyISAM is faster than InnoDB in the MySQL world, InnoDB is fast compared to any database engine. With InnoDB you get transactions, speed and integrity three features not usually used in the same sentence. Most of my customers want as much speed as they can get, but at the end of the day, good data integrity lets them sleep at night.

MySQL Question and Answers

  1. How do you start and stop MySQL on Windows? – net start MySQL, net stop MySQL
  2. How do you start MySQL on Linux? – /etc/init.d/mysql start
  3. Explain the difference between mysql and mysqli interfaces in PHP? – mysqli is the object-oriented version of mysql library functions.
  4. What’s the default port for MySQL Server? – 3306
  5. What does tee command do in MySQL? – tee followed by a filename turns on MySQL logging to a specified file. It can be stopped by command notee.
  6. Can you save your connection settings to a conf file? – Yes, and name it ~/.my.conf. You might want to change the permissions on the file to 600, so that it’s not readable by others.
  7. How do you change a password for an existing user via mysqladmin? – mysqladmin -u root -p password “newpassword”
  8. Use mysqldump to create a copy of the database? – mysqldump -h mysqlhost -u username -p mydatabasename > dbdump.sql
  9. Have you ever used MySQL Administrator and MySQL Query Browser? Describe the tasks you accomplished with these tools.
  10. What are some good ideas regarding user security in MySQL? – There is no user without a password. There is no user without a user name. There is no user whose Host column contains % (which here indicates that the user can log in from anywhere in the network or the Internet). There are as few users as possible (in the ideal case only root) who have unrestricted access.
  11. Explain the difference between MyISAM Static and MyISAM Dynamic. – In MyISAM static all the fields have fixed width. The Dynamic MyISAM table would include fields such as TEXT, BLOB, etc. to accommodate the data types with various lengths. MyISAM Static would be easier to restore in case of corruption, since even though you might lose some data, you know exactly where to look for the beginning of the next record.
  12. What does myisamchk do? – It compressed the MyISAM tables, which reduces their disk usage.
  13. Explain advantages of InnoDB over MyISAM? – Row-level locking, transactions, foreign key constraints and crash recovery.
  14. Explain advantages of MyISAM over InnoDB? – Much more conservative approach to disk space management – each MyISAM table is stored in a separate file, which could be compressed then with myisamchk if needed. With InnoDB the tables are stored in tablespace, and not much further optimization is possible. All data except for TEXT and BLOB can occupy 8,000 bytes at most. No full text indexing is available for InnoDB. TRhe COUNT(*)s execute slower than in MyISAM due to tablespace complexity.
  15. What are HEAP tables in MySQL? – HEAP tables are in-memory. They are usually used for high-speed temporary storage. No TEXT or BLOB fields are allowed within HEAP tables. You can only use the comparison operators = and <=>. HEAP tables do not support AUTO_INCREMENT. Indexes must be NOT NULL.
  16. How do you control the max size of a HEAP table? – MySQL config variable max_heap_table_size.
  17. What are CSV tables? – Those are the special tables, data for which is saved into comma-separated values files. They cannot be indexed.
  18. Explain federated tables. – Introduced in MySQL 5.0, federated tables allow access to the tables located on other databases on other servers.
  19. What is SERIAL data type in MySQL? – BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT
  20. What happens when the column is set to AUTO INCREMENT and you reach the maximum value for that table? – It stops incrementing. It does not overflow to 0 to prevent data losses, but further inserts are going to produce an error, since the key has been used already.
  21. Explain the difference between BOOL, TINYINT and BIT. – Prior to MySQL 5.0.3: those are all synonyms. After MySQL 5.0.3: BIT data type can store 8 bytes of data and should be used for binary data.
  22. Explain the difference between FLOAT, DOUBLE and REAL. – FLOATs store floating point numbers with 8 place accuracy and take up 4 bytes. DOUBLEs store floating point numbers with 16 place accuracy and take up 8 bytes. REAL is a synonym of FLOAT for now.
  23. If you specify the data type as DECIMAL (5,2), what’s the range of values that can go in this table? – 999.99 to -99.99. Note that with the negative number the minus sign is considered one of the digits.
  24. What happens if a table has one column defined as TIMESTAMP? – That field gets the current timestamp whenever the row gets altered.
  25. But what if you really want to store the timestamp data, such as the publication date of the article? – Create two columns of type TIMESTAMP and use the second one for your real data.
  26. Explain data type TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP – The column exhibits the same behavior as a single timestamp column in a table with no other timestamp columns.
  27. What does TIMESTAMP ON UPDATE CURRENT_TIMESTAMP data type do? – On initialization places a zero in that column, on future updates puts the current value of the timestamp in.
  28. Explain TIMESTAMP DEFAULT ‘2006:09:02 17:38:44′ ON UPDATE CURRENT_TIMESTAMP. – A default value is used on initialization, a current timestamp is inserted on update of the row.
  29. If I created a column with data type VARCHAR(3), what would I expect to see in MySQL table? – CHAR(3), since MySQL automatically adjusted the data type.
  30. Explain MySQL architecture. – The front layer takes care of network connections and security authentications, the middle layer does the SQL query parsing, and then the query is handled off to the storage engine. A storage engine could be either a default one supplied with MySQL (MyISAM) or a commercial one supplied by a third-party vendor (ScaleDB, InnoDB, etc.)
  31. Explain MySQL locks. – Table-level locks allow the user to lock the entire table, page-level locks allow locking of certain portions of the tables (those portions are referred to as tables), row-level locks are the most granular and allow locking of specific rows.
  32. Explain multi-version concurrency control in MySQL. – Each row has two additional columns associated with it – creation time and deletion time, but instead of storing timestamps, MySQL stores version numbers.
  33. What are MySQL transactions? – A set of instructions/queries that should be executed or rolled back as a single atomic unit.
  34. What’s ACID? – Automicity – transactions are atomic and should be treated as one in case of rollback. Consistency – the database should be in consistent state between multiple states in transaction. Isolation – no other queries can access the data modified by a running transaction. Durability – system crashes should not lose the data.
  35. Which storage engines support transactions in MySQL? – Berkeley DB and InnoDB.
  36. How do you convert to a different table type? – ALTER TABLE customers TYPE = InnoDB
  37. How do you index just the first four bytes of the column? – ALTER TABLE customers ADD INDEX (business_name(4))
  38. What’s the difference between PRIMARY KEY and UNIQUE in MyISAM? – PRIMARY KEY cannot be null, so essentially PRIMARY KEY is equivalent to UNIQUE NOT NULL.
  39. How do you prevent MySQL from caching a query? – SELECT SQL_NO_CACHE …
  40. What’s the difference between query_cache_type 1 and 2? – The second one is on-demand and can be retrieved via SELECT SQL_CACHE … If you’re worried about the SQL portability to other servers, you can use SELECT /* SQL_CACHE */ id FROM … – MySQL will interpret the code inside comments, while other servers will ignore it.

SQL Server DBA Checklist

1. Check OS Event Logs, SQL Server Logs, and Security Logs for unusual events.
2. Verify that all scheduled jobs have run successfully.
3. Confirm that backups have been made and successfully saved to a secure location.
4. Monitor disk space to ensure your SQL Servers won’t run out of disk space.
5. Throughout the day, periodically monitor performance using both System Monitor and Profiler.
6. Use Enterprise Manager/Management Studio to monitor and identify blocking issues.
7. Keep a log of any changes you make to servers, including documentation of any performance issues you identify and correct.
8. Create SQL Server alerts to notify you of potential problems, and have them emailed to you. Take actions as needed.
9. Run the SQL Server Best Practices Analyzer on each of your server’s instances on a periodic basis.
10. Take some time to learn something new as a DBA to further your professional development.
11. Verify the Backups and Backup file size
12. Verifying Backups with the RESTORE VERIFYONLY Statement
13. In OFF-Peak Hours run the database consistency checker commands if possible