Apa itu constraint di mysql?
Show Introduction to MySQL ConstraintsMySQL constraints are statements that can be applied at the column level or table level to specify rules for the data that can be entered into a column or data table, i.e constraints are basically limitations or restrictions on the type of data and hence they ensure reliability, consistency, and accuracy of the data. In SQL, we will come across the following types of constraints. Here is an overview of the generic purpose that these constraints serve.
Types of Constraints in MySQL with ExamplesFollowing are the types and examples of constraints which are given below: 1. NOT NULL CONSTRAINTWhen NOT NULL constraint is applied to a column, it ensures that the column will not accept NULL values. The syntax for NOT NULL constraint in MYSQL is as follows : Syntax:
Parameters: The parameters used in the syntax are :
ExampleSQL query to create NOT NULL constraint while creating a table. Query:
Output: Here, we have created a student table with three columns that accept non-NULL values. Let’s check if these columns accept null values or not. We will try to insert a student record in the table which contains data for three columns only. The insert statement will fail because we did not supply any values for class_name.
Output: 2. CHECK CONSTRAINTWhen CHECK constraint is applied to a column, it ensures that the column will not accept data values outside the specified range. The CHECK constraint can also be mentioned to ensure that data meets a specified condition like less than, equal to, not, etc. Syntax:
Parameters:
ExampleSQL query to create CHECK constraint while creating a table. Query:
Output: Here, we have created a student table with three columns that accept non-NULL values and the fourth column which will age greater than 9 only. Let’s check if the columns meet the specified conditions. We will try to insert a student record in the table which contains age less than 9. The insert statement will fail.
Output: 3. UNIQUE KEY CONSTRAINTA unique key is a constraint in SQL which helps in uniquely identifying a record in the datatable. It can be considered somewhat similar to the Primary key as both of them guarantees the uniqueness of a record. But unlike primary key, a unique key can accept NULL values and it can be used on more than one column of the datatable. Syntax:
Parameters:
ExampleSQL query to create unique constraints while creating a table. Query:
Output: 4. PRIMARY KEY CONSTRAINTA primary key constraint is used to uniquely identify a record or row in the data table. It does not accept NULL values and accepts only unique values. Syntax:
Parameters:
ExampleThe SQL query to create primary key constraints while creating a table. Query:
Output: 5. FOREIGN KEY CONSTRAINTA foreign key constraint is used to uniquely identify a record or row in another data table. It refers to the primary key in the other table. So, it basically acts as a link between two tables. Syntax:
Parameters:
ExampleSQL query to create foreign key constraint while creating a table. Query:
Output: In the above example, we have created a foreign key constraint on Class_Name which references the classes table. 6. DEFAULT CONSTRAINTIn order to ensure consistency in data, we sometimes require missing data or non-entered values to be set to a default value. We can use DEFAULT constraint in such cases to ensure that the column is set to default. Syntax:
ExampleSQL query to create default constraint while creating a table. Query:
Output: In the above example, the Class_Name column in the student’s table will set the class name to IV by default if nothing is specified. We are inserting values for two columns only.
Output: We just inserted values for two columns but we can see in the table that class name has been set to an IV default value.
Output: Recommended ArticlesThis is a guide to MySQL Constraints. Here we discuss the Introduction and types of constraints along with different examples and its code implementation. You may also look at the following articles to learn more –
Apa itu constraint pada mysql?Constraints SQL digunakan untuk menentukan aturan yang memperbolehkan atau membatasi nilai / data apa yang akan disimpan di tabel. Mereka menyediakan metode yang sesuai untuk memastikan keakuratan dan integritas data di dalam tabel.
Apa fungsi constraint?Anda dapat menggunakan constraint untuk membatasi tipe data yang disimpan ke dalam tabel. Constraint dapat digunakan pada saat pertama kali membuat table dengan statement CREATE TABLE atau setelah tabel dibuat dengan perintah statement ALTER TABLE.
Apa yang dimaksud constraint dalam database?Konstraint/ batasan dalam database pada dasarnya adalah pembatasan nilai-nilai yang diperbolehkan untuk diisikan dalam sebuah kolom atau kolom-kolom yang terdapat dalam sebuah tabel.
Apa itu constraint foreign key?Foreign Keys adalah key yang menghubungkan suatu relasi terhadap relasi referensinya. Constraint (batasan) adalah batasan yang menjaga hubungan (relationship) antara elemen data yang perlu diberlakukan pada DBMS.
|