Database Articles

Page 496 of 546

What is the syntax for input parameters (variables) in a MySQL query?

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 2K+ Views

To set a variable in MySQL, you need to use the SET command. Following is the syntax:set @yourVariableName:=yourValue; select *from yourTableName where yourColumnName=@yourVariableName;Let us first create a table:mysql> create table DemoTable (    Id int,    FirstName varchar(20),    LastName varchar(20) ); Query OK, 0 rows affected (0.83 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(10, 'Carol', 'Taylor'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, 'John', 'Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(30, 'John', 'Smith'); Query OK, 1 row ...

Read More

How to update a column of varchar type in MySQL to increase its length?

George John
George John
Updated on 30-Jul-2019 708 Views

Let us first create a table. Here, we have two columns with varchar type −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserFirstName varchar(10),    UserLastName varchar(20) ,    UserAge int ); Query OK, 0 rows affected (0.96 sec)Let us check the description of table using DESC command −mysql> desc DemoTable;This will produce the following output −+---------------+-------------+------+-----+---------+----------------+ | Field         | Type        | Null | Key | Default | Extra          | +---------------+-------------+------+-----+---------+----------------+ | UserId        | int(11)     | NO   ...

Read More

How to GROUP BY in a select query on positive or negative values?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 606 Views

Following is the syntax to GROUP BY in a select query on positive or negative values:select *from yourTableName group by -yourColumnName;Let us first create a table:mysql> create table DemoTable (Value int); Query OK, 0 rows affected (0.60 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(-10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(-20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert ...

Read More

How to order by certain part of a string in MySQL?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 2K+ Views

You can use ORDER BY SUBSTRING() to order by certain part of a string in MySQL. Let us first create a table:mysql> create table DemoTable (UserId varchar(200)); Query OK, 0 rows affected (0.68 sec)Following is the query to insert records in the table using insert command:mysql> insert into DemoTable values('USER_1234'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('USER_John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('USER_Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('USER_Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('USER_Bob'); Query OK, ...

Read More

Which datatype should I use for flag in MySQL?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 2K+ Views

To set a flag, you can set the type as tinyint(1) type. Following is the syntax −yourColumnName tinyint(1) DEFAULT 1;Let us first create a table −mysql> create table DemoTable (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    isMarried tinyint(1) DEFAULT 1 ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> INSERT INTO DemoTable(ClientName, isMarried) values('Larry', 0); Query OK, 1 row affected (0.16 sec) mysql> INSERT INTO DemoTable(ClientName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO DemoTable(ClientName, isMarried) values('Mike', 1); Query OK, 1 row affected (0.19 ...

Read More

How can I remove every column in a table in MySQL?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 184 Views

In order to remove every column in a table in MySQL, you can use DROP TABLE command. Following is the syntax:DROP TABLE yourTableName;Let us first create a table:mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentLastName varchar(20),    StudentAge int,    StudentAddress varchar(200),    StudentCountryName varchar(30),    StudentDateOfBirth datetime ); Query OK, 0 rows affected (0.85 sec)Let us check the description of table using DESC command:mysql> desc DemoTable;This will produce the following output:+--------------------+--------------+------+-----+---------+----------------+ | Field              | Type         | Null | Key ...

Read More

Update one column data to another column in MySQL if the second column is NOT NULL?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 3K+ Views

To update one column data to another column, you can use UPDATE command. Let us first create a table −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserFirstName varchar(20),    ListOfName varchar(20) ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserFirstName, ListOfName) values('John', 'Larry'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(UserFirstName, ListOfName) values('Carol', null); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(UserFirstName, ListOfName) values('David', 'Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(UserFirstName, ListOfName) ...

Read More

Can I write my own MySQL functions to use in MySQL queries?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 449 Views

Yes, you can write own MySQL function to use in MySQL queries. Following is the syntax:DELIMITER // CREATE FUNCTION yourFunctionName(optional parameters)) RETURNS yourDataType DETERMINISTIC NO SQL BEGIN yourStatements1 . . . . N END // DELIMITER ;We have used the CREATE FUNCTION above to create a custom function.Let us create a custom MySQL function to use in MySQL query:mysql> DELIMITER // mysql> CREATE FUNCTION get_First_Name(Name VARCHAR(255)) RETURNS VARCHAR(255)    DETERMINISTIC    NO SQL    BEGIN       RETURN LEFT(Name, LOCATE(' ', Name) - 1);    END    // Query OK, 0 rows affected (0.20 sec) mysql> DELIMITER ;Now call ...

Read More

How to create MySQL user with limited privileges?

George John
George John
Updated on 30-Jul-2019 610 Views

To create MySQL user with limited privileges, following is the syntax −CREATE USER 'yourUserName'@'yourHostName' IDENTIFIED BY 'yourPassword';Following is the syntax to set limited privileges for user −GRANT SELECT, INSERT, UPDATE, etc. REFERENCES ON yourDatabaseName.* TO 'yourUserName'@'yourHostName';Let us implement the above syntaxes in order to create a MySQL user with limited privileges −mysql> CREATE USER 'David'@'localhost' IDENTIFIED BY 'david'; Query OK, 0 rows affected (0.20 sec) mysql> GRANT SELECT, INSERT, UPDATE, DELETE, ALTER, CREATE, REFERENCES ON test.* TO 'David'@'localhost'; Query OK, 0 rows affected (0.21 sec)Let us check the user has been created with name ‘David’ or not.mysql> ...

Read More

How to SELECT records if the absolute value of the difference between two values is greater than a certain number?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 263 Views

To SELECT records if the absolute value of the difference between two values is greater than a certain number, following is the syntax:select *from yourTableName where abs(yourColumnName1-yourColumnName2) >= yourCertainNumber;Let us first create a table:mysql> create table DemoTable (    Number1 int ,    Number2 int ); Query OK, 0 rows affected (0.59 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(10, 20); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(100, 200); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(400, 300); Query OK, 1 ...

Read More
Showing 4951–4960 of 5,456 articles
« Prev 1 494 495 496 497 498 546 Next »
Advertisements