Browsed by
Tag: Complete Tutorials on MySQL

How to dump all databases for backup. Backup file is sqlcommands to recreate all db’s

How to dump all databases for backup. Backup file is sqlcommands to recreate all db’s

# [mysql dir]/bin/mysqldump -u root -ppassword –opt >/tmp/alldatabases.sql To dump all databases in MySQL for backup, you can use the mysqldump command-line utility. Here’s the command: bash mysqldump -u username -p –all-databases > backup.sql Replace username with your MySQL username. When you run this command, it will prompt you to enter your MySQL password. After entering the password, it will dump all databases into a file named backup.sql. This backup.sql file contains SQL commands to recreate all databases, including their…

Read More Read More

Import data into MySQL from any file

Import data into MySQL from any file

How to Import data into MySQL from any file: Mysql –u root <db.sql (for database and tables) Mysql –u root <data.sql (for data into tables) To import data into MySQL from a file, you can use the LOAD DATA INFILE statement. Here’s a basic example of how to use it: sql LOAD DATA INFILE ‘path/to/your/file.csv’ INTO TABLE your_table FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’ IGNORE 1 LINES; — if your file has a header and…

Read More Read More

How we will Show selected records sorted in an ascending (asc) or descending (desc)

How we will Show selected records sorted in an ascending (asc) or descending (desc)

mysql> SELECT col1,col2 FROM tablename ORDER BY col2 DESC; mysql> SELECT col1,col2 FROM tablename ORDER BY col2 ASC; In MySQL, you can use the ORDER BY clause to sort selected records in either ascending (ASC) or descending (DESC) order. Here’s the basic syntax: sql SELECT column1, column2, … FROM table_name ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], …; If you want to sort in ascending order, you can omit ASC as it is the default: sql SELECT…

Read More Read More

How to dump one database for backup

How to dump one database for backup

# [mysql dir]/bin/mysqldump -u username -ppassword –databases databasename >/tmp/databasename.sql To dump a MySQL database for backup, you can use the mysqldump command. Here’s the basic syntax: bash mysqldump -u username -p database_name > backup_file.sql Replace username with your MySQL username, database_name with the name of the database you want to backup, and backup_file.sql with the name you want to give to your backup file. After running this command, you’ll be prompted to enter your MySQL password. If you want to…

Read More Read More

Interview Scenario on MySQL:

Interview Scenario on MySQL:

Interview Scenario on MySQL: There is a table named SAMPLE, and we want to delete all the data from the table. Which is better option? delete * from SAMPLE truncate table SAMPLE In delete cursor is on the current location, data is deleted from the table but memory is not released by the table, by which searching and sorting operation may take so much time. While, in truncate cursor is on the starting location, data is deleted permanently and memory is released for…

Read More Read More

How to Return total number of rows

How to Return total number of rows

mysql> SELECT COUNT(*) FROM tablename; To return the total number of rows in a MySQL table, you can use the COUNT() function in a SQL query. Here’s an example: sql SELECT COUNT(*) AS total_rows FROM your_table_name; Replace your_table_name with the actual name of your table. This query will return a single value named total_rows, representing the total number of rows in the specified table.

Restore database (or database table) from backup

Restore database (or database table) from backup

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql To restore a database or a specific table from a backup in MySQL, you typically use the mysql command-line client or a similar tool. Here’s a general approach: Ensure you have a backup: First, make sure you have a recent backup of the database or table you want to restore. Access the MySQL command-line interface: Open your terminal or command prompt and log in to MySQL using a command like: css…

Read More Read More

How to do login in mysql with unix shell

How to do login in mysql with unix shell

By below method if password is pass and user name is root # [mysql dir]/bin/mysql -h hostname -u root -p pass To log in to MySQL using the Unix shell, you can use the mysql command along with the appropriate options. Here’s the general syntax: bash mysql -u your_username -p Replace your_username with your MySQL username. After running this command, you will be prompted to enter your MySQL password. Once you provide the correct password, you’ll be logged into the…

Read More Read More

How to Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs

How to Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs

# mysql -u root -p mysql> use mysql; mysql>INSERTINTO user (Host,User,Password) VALUES(‘%’,’username’,PASSWORD(‘password’)); mysql> flush privileges; To create a new user in MySQL, you can follow these steps: Login as root: bash mysql -u root -p You will be prompted to enter the root password. Switch to the MySQL database: sql USE mysql; Create the new user: sql CREATE USER ‘new_user’@’localhost’ IDENTIFIED BY ‘password’; Replace ‘new_user’ with the desired username and ‘password’ with the desired password. Update privileges: sql GRANT ALL…

Read More Read More

How to Create Table show Example

How to Create Table show Example

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255)); To create a table in MySQL, you can use the CREATE TABLE statement followed by the table name and the list of columns with their data types and any constraints. Here’s an example: sql CREATE TABLE employees ( employee_id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100) UNIQUE, hire_date DATE, salary DECIMAL(10, 2), department_id INT );…

Read More Read More