Found 4470 Articles for MySQLi

Which statement, other than ALTER TABLE statement, can be used to apply UNIQUE constraint to the field of an existing MySQL table?

Rishi Raj
Updated on 19-Jun-2020 11:48:53
CREATE UNIQUE INDEX statement can also be used to apply the UNIQUE constraint to the field of an existing MySQL table. The syntax of it is as follows −CREATE UNIQUE INDEX index_name ON table_name(Column_name);ExampleSuppose we have the following table named ‘Test5’ and we want to add UNIQUE constraint to the column ‘ID’ then it can be done with the help of CREATE UNIQUE INDEX command as follows −mysql> DESCRIBE TEST5; +-------+-------------+------+-----+---------+-------+ | Field | Type        | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID    | int(11)     | YES  |   ... Read More

How can we apply UNIQUE constraint to the field of an existing MySQL table?

Swarali Sree
Updated on 19-Jun-2020 11:47:43
We can apply the UNIQUE constraint to a column of an existing MySQL table with the help of ALTER TABLE statement.SyntaxALTER TABLE table_name MODIFY colum_name datatype UNIQUE;                     OR ALTER TABLE table_name ADD UNIQUE (colum_name);ExampleSuppose we have the following table named ‘Test4’ and we want to add UNIQUE constraint to the column ‘Name’ then it can be done with the help of ALTER TABLE command as follows −mysql> DESCRIBE test4; +-------+-------------+------+-----+---------+-------+ | Field | Type        | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID   ... Read More

What is MySQL UNIQUE constraint and how can we apply it to the field of a table?

Samual Sam
Updated on 28-Jan-2020 06:47:59
As the name suggests, MySQL UNIQUE constraint maintains the uniqueness of a column in the table and does not allow inserting the duplicate value. Basically, UNIQUE constraint creates an index such that all the values in the index column must be unique. It is pertinent to mention here that we can have more than one UNIQUE column in a MySQL table.We can apply UNIQUE constraint by mentioning the ‘UNIQUE’ keyword at the time of defining a column. It can be understood with the help of the following example −mysql> Create table test3(ID INT UNIQUE, Name Varchar(20)); Query OK, 0 rows ... Read More

How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?

Kumar Varma
Updated on 19-Jun-2020 11:31:09
It is quite possible to insert the NULL keyword as a value in a character type column having NOT NULL constraint because NULL is a value in itself. Following example will exhibit it −ExampleSuppose we have a table test2 having character type column ‘Name’ along with NOT NULL constraint on it. It can be checked from the DESCRIBE statement as follows −mysql> Describe test2\G *************************** 1. row ***************************   Field: id    Type: int(11)    Null: NO     Key: Default: NULL   Extra: *************************** 2. row ***************************   Field: NAME    Type: varchar(20)    Null: NO     Key: ... Read More

How can we remove NOT NULL constraint from a column of an existing MySQL table?

Lakshmi Srinivas
Updated on 19-Jun-2020 11:29:02
We can remove a NOT NULL constraint from a column of an existing table by using the ALTER TABLE statement.ExampleSuppose we have a table ‘test123’ having a NOT NULL constraint on column ‘ID’ as follows −mysql> DESCRIBE test123; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra    | +-------+---------+------+-----+---------+-------+ | ID    | int(11) | NO   |     |   NULL  |       | | Date  | date    | YES  |     |   NULL  |       | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.04 sec)Now if we ... Read More

What happens when we apply NOT NULL constraint, with ALTER TABLE statement, to a column contains NULL values?

karthikeya Boyini
Updated on 19-Jun-2020 11:26:11
In this case, MySQL will return an error message regarding data truncated for the column. Following is an example of demonstrating it −ExampleSuppose we have a table ‘test2’ which contains a NULL value in column ‘ID’ at 2nd row. Now, if we will try to declare the column ID to NOT NULL then MySQL will return the error as follows −mysql> Select * from test2; +------+--------+ | ID   | Name   | +------+--------+ | 1    | Gaurav | | NULL | Rahul  | +------+--------+ 2 rows in set (0.00 sec) mysql> ALTER TABLE TEST2 MODIFY ID INT NOT NULL; ERROR 1265 (01000): Data truncated for column 'ID' at row 2

How can we apply a NOT NULL constraint to a column of an existing MySQL table?

Jai Janardhan
Updated on 19-Jun-2020 11:27:51
We can apply the NOT NULL constraint to a column of an existing MySQL table with the help of ALTER TABLE statement. SyntaxALTER TABLE table_name MODIFY colum_name datatype NOT NULL; Examplemysql> Create table test123(ID INT, Date DATE); Query OK, 0 rows affected (0.19 sec) mysql> Describe test123; +-------+---------+------+-----+---------+-------+ | Field | Type    | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | ID    | int(11) | YES  |     | NULL    |       | | Date  | date    | YES  |     | NULL    |       | +-------+---------+------+-----+---------+-------+ ... Read More

What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?

Samual Sam
Updated on 19-Jun-2020 11:24:42
Actually, MySQL NOT NULL constraint restricts a column of the table from having a NULL value. Once we applied NOT NULL constraint to a column, then we cannot pass a null value to that column. It cannot be declared on the whole table i.e., in other words, we can say that NOT NULL is a column level constraint.For declaring a field NOT NULL, we have to use NOT NULL keyword while defining the column in CREATE TABLE statement.Examplemysql> Create table Employee(ID Int NOT NULL, First_Name Varchar(20), Last_name Varchar(20), Designation Varchar(15)); Query OK, 0 rows affected (0.59 sec)In the query above, ... Read More

What are MySQL constraints?

Arushi
Updated on 19-Jun-2020 11:25:44
As we know that constraint is a kind of limitation or restriction. Similarly, MySQL constraints are used to define some rules that limit the data that can go into a table. With the help of constraints, we can basically maintain the accuracy and integrity of the data inside the table.Types of MySQL constraintsMySQL constraints are of two types −Column Level constraints − The constraints which are applied only to a particular column of the table and limits only column data are called column level constraints.Table Level constraints − The constraints which are applied to the whole table and limits whole ... Read More

How to store and retrieve date into Sqlite3 database using Python?

Rajendra Dharmkar
Updated on 12-Jun-2020 14:00:29
You can very easily store and retrieve date into Sqlite3 database using the sqlite3 module. When inserting the date in the database, pass the date directly and Python handles it automatically.Exampleimport sqlite3 import datetime conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) conn.execute('''CREATE TABLE TEST (ID TEXT PRIMARY KEY NOT NULL, DATE DATE)''') # Save changes conn.commit() # Insert the object directly conn.execute("INSERT INTO TEST (ID, DATE) VALUES (?, ?)", ('My date', datetime.date(2018, 1, 4))) conn.commit() print("Record inserted")OutputThis will give the output −Record insertedNow when you will fetch the values from the database, you will get the date already parsed to the datetime object.Exampleimport ... Read More
Advertisements