SQL Data Types and Constraints Guide
SQL Data Types and Constraints Guide
A 'PRIMARY KEY' uniquely identifies each record in a table, ensuring no duplicate entries and that the field is not null. It combines both 'UNIQUE' and 'NOT NULL' constraints. The document uses 'Rollno varchar(10) primary key', implying that each Rollno must be unique and compulsorily filled, effectively maintaining data integrity .
A 'CHECK' constraint ensures that all values in a column satisfy a specific condition. It is used for data validation within a table. In the provided example, the 'Age' column is constrained with 'CHECK(Age > 18)', which means that only values greater than 18 can be inserted into the 'Age' column .
'TINYINT' is beneficial when dealing with very small numerical ranges (0-255 in unsigned mode), offering space efficiency due to its single-byte storage requirement. However, its limited range makes it impractical for larger datasets or counts. The document does not directly use 'TINYINT', indicating preference for broader ranged integer types like 'INT' for accommodating more extensive data where space isn't a constraint .
SQL allows columns defined as 'UNIQUE' to contain a NULL value because NULL is considered a unique unknown value. This unique handling is seen in the document where 'Id int unique' is described, allowing one NULL in the column while preventing other duplicates, thus balancing uniqueness with flexibility .
A 'NOT NULL' constraint ensures that a column cannot have a NULL value. This is critical for columns that require a value for every row in a table. In the document, the 'Name' column is defined as 'varchar(100) NOT NULL', meaning that every entry must have a name value, disallowing any NULL entries .
Dropping a table involves permanently deleting it from the database, which might be necessary to remove obsolete or duplicate data, reclaim storage, or when restructuring the database design. The document illustrates this by first checking if 'Emp1' exists and dropping it if so, to create a table with possibly updated structure, showcasing preventative maintenance and version control .
'VARCHAR' stores variable-length strings, which can save space if the actual data is shorter than the maximum specified length, but it may result in fragmentation and increased processing time. In contrast, 'CHAR' stores fixed-length strings, which can waste space if data does not use the full length but provides faster access speed due to fixed-size rows .
A 'FOREIGN KEY' constraint is used to link two tables together. It ensures referential integrity by requiring that each value in a column (or combination of columns) exists in another table's primary key column(s). This prevents actions such as invalid data deletions that would disrupt relationships between tables. While the document doesn’t directly demonstrate a 'FOREIGN KEY', it explains the importance of such constraints in maintaining consistent data integrity .
'INT' is efficient for numerical operations and indexing, uses fixed memory, and prevents errors that occur with string manipulation. The document demonstrates this by using 'Studentid INT', which can be incremented easily, sorted rapidly, and occupies less memory compared to 'VARCHAR' .
A 'DEFAULT' constraint provides a default value for a column when no value is specified during an insert operation. It ensures that the column has a value even when it is not explicitly set. In the document, the 'Joindate' column uses 'datetime default getdate()', which assigns the current date and time by default if no value is provided during insertion .