Found 6702 Articles for Database

ALTER TABLE to add a composite primary key in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

1K+ Views

To add composite primary key, use the ALTER command. Let us first create a demo table The query to create a table. mysql> create table CompositePrimaryKey -> ( -> Id int, -> StudentName varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.56 sec) Haven’t added composite primary key above till now. Let us now check with the help of desc command. mysql> desc CompositePrimaryKey; The following is the output. +-------------+--------------+------+-----+---------+-------+ | Field ... Read More

How can I use MySQL replace() to replace strings in multiple records?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

1K+ Views

The replace() function can be used to replace a string with another string. To understand replace(), we need to create a table with some records. The following is the query to create a table. mysql> create table replaceDemo -> ( -> Name varchar(200) -> ); Query OK, 0 rows affected (0.55 sec) Insert some records with the help of INSERT command. The query to insert records is as follows − mysql> insert into replaceDemo values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into replaceDemo values('Demo'); ... Read More

How do I remove a MySQL database?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

263 Views

To remove any database, we need to use DROP command in MySQL. Here is the syntax. DROP DATABASE yourDatabaseName; First, let us check how many databases are present in MySQL. Here is the query for the same. mysql> SHOW DATABASES; The following is the output. +--------------------+ | Database | +--------------------+ | business | | database1 | | databasesample | | education ... Read More

Best data type for storing currency values in a MySQL database?

George John
Updated on 30-Jul-2019 22:30:23

600 Views

For representation of money, we need to use Decimal (TotalDigitsinteger, DigitsAfterDecimalinteger) method. Let’s say, we need to display the value 345.66. For that, count how many digits are available. In value 345.66, there are 5 digits in total and 2 digits after decimal point, which is 66. We can represent the same with the help of Decimal() method from MySQL. Here is the exact representation. DECIMAL(5, 2) Let us first create a table and consider the same above representation for our example − mysql> create table MoneyRepresentation -> ( -> Money ... Read More

How to set NOW() as default value for datetime datatype in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

6K+ Views

We can set the now() function as a default value with the help of dynamic default. First, we will create a table with data type” datetime”. After that, we will set now() as the default value for column “MyTime” as shown below. Creating a table. mysql> create table DefaultDateTimeDemo -> ( -> MyTime datetime default CURRENT_TIMESTAMP -> ); Query OK, 0 rows affected (0.59 sec) After creating the above table, we won’t insert any value while using the insert command. This is done so that we can get the ... Read More

What is the operator <=> in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

129 Views

Here are the usages of the operator in MySQL. Case 1 This operator is similar to = operator i.e. when the value is equal then the result will become true(1), otherwise false(0). In the first case both = and operators work same. Case 2 Whenever we compare any value with NULL then the operator gives the value 0 and when we compare with NULL NULL, then it returns 1. While in case of = operator, this does not happen. Whenever we compare any value with NULL, it returns NULL. If we compare NULL with NULL, then ... Read More

Rank function in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

533 Views

The rank() function can be used to give a rank for every row within the partition of a result set. First, let us create a table − mysql> create table RankDemo mysql> ( mysql> id int mysql> ); Query OK, 0 rows affected (0.53 sec) Inserting records into table. mysql> insert into RankDemo values(1); Query OK, 1 row affected (0.19 sec) mysql> insert into RankDemo values(3); Query OK, 1 row affected (0.12 sec) mysql> insert into RankDemo values(3); Query OK, 1 row affected (0.11 ... Read More

How to change MySQL column definition?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

158 Views

To change MySQL column definition, we can use modify or change clause with ALTER command. Let us first create a table with a column as ID, with int data type. We will modify the same column name with varchar data type. Creating a table. mysql> create table ModifyColumnDemo -> ( -> id int -> ); Query OK, 0 rows affected (0.52 sec) Now, let us write the syntax to change the column definition. The syntax is as follows − alter table yourTableName modify column columnName data type; ... Read More

How to select last row in MySQL?

Chandu yadav
Updated on 02-Sep-2023 12:47:00

53K+ Views

To select the last row, we can use ORDER BY clause with desc (descending) property and Limit 1. Let us first create a table and insert some records with the help of INSERT command. The query is as follows. mysql> create table getLastRecord -> ( -> Id int, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.61 sec) After creating the above table, we will insert records with the help of INSERT command. mysql> insert into getLastRecord values(1, 'John'); Query OK, 1 row affected (0.13 sec) ... Read More

How to cast from VARCHAR to INT in MySQL?

George John
Updated on 07-Sep-2023 01:05:54

36K+ Views

To cast VARCHAR to INT, we can use the cast() function from MySQL. Here is the syntax of cast() function. cast(anyValue as dataType) For our example, we will create a table with the help of CREATE command. mysql> create table VarchartointDemo -> ( -> Value varchar(100) -> ); Query OK, 0 rows affected (0.51 sec) After creating a table, let us insert some records into the table with the help of INSERT command. The query is as follows − mysql> insert into VarchartointDemo values('123'); Query OK, 1 row affected (0.26 sec) ... Read More

Advertisements