Detailed introduction and examples of mysql operations (2)
Foreword: Relational database management systems are absolutely unavoidable in back-end development. Common database management systems include MySql
, SQLServer
, Oracle
, etc.
Mysql is the most commonly used database management system.
Recommended mysql related video tutorials: https://www.php.cn/course/list/51/type/4.html
I will introduce it to you today Check out Mysql.
I will summarize and organize relevant knowledge for you in the future, thank you!
Official website download address: https://dev.mysql.com/downloads/mysql/
Note: During the database installation process, we need to enable management You must have user rights to install, otherwise the installation will fail due to insufficient rights.
1. Create a data table
Syntax: create table table name (field 1, field 2...);
Use PHP script to create a data table:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //创建数据表 $sql = "CREATE TABLE demo02(id SMALLINT(10) PRIMARY KEY, age TINYINT(3), name VARCHAR(10))CHARSET=utf8"; $query = mysqli_query($conn, $sql); //判断是否创建成功 if(!$query) { die('创建失败!'.mysqli_error($conn)); } //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
2. Delete the data table
Syntax: drop table table name;
Use PHP script to delete the data table:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //删除数据表 $sql = "DROP TABLE runoob_tbl"; $query = mysqli_query($conn, $sql); if(!$query) { die('删除失败!'.mysqli_error($conn)); } //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
3. Insert data
Syntax: insert into table name (field 1, field 2 ...)values(value1,value2...);
Use PHP script to insert data:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //插入数据 $sql = "INSERT INTO demo02(id,age,name) VALUES(4,20,'mm')"; $query = mysqli_query($conn,$sql); //var_dump($query); //die(); //判断是否插入成功 if(!$query) { die('插入失败!'.mysqli_error($conn)); } //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
4. Query data
Syntax: select * from table;
Use PHP script to query data:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //判断连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //查询数据 $sql = "SELECT * FROM demo02"; $query = mysqli_query($conn, $sql); //判断是否查询成功 if(!$query) { die('查询失败!'.mysqli_error($conn)); } echo 'mysql查询语句'; echo '<table border=1>'; echo '<tr> <td>id</td> <td>age</td> <td>name</td> </tr>'; //循环数据 while($row = mysqli_fetch_assoc($query)) { echo '<tr> <td>'.$row['id'].'</td> <td>'.$row['age'].'</td> <td>'.$row['name'].'</td> </tr>'; } echo '</table>'; //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
5, update statement
Syntax: update table name set field1 = new value;
Use PHP to update data
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //更新数据 $sql = "UPDATE demo02 SET name='zylmy' WHERE id=1"; $query = mysqli_query($conn, $sql); //var_dump($query); //die(); //判断是否更新成功 if(!$query) { die('更新失败!'.mysqli_error($conn)); } //关闭连接 mysqli_close($conn); ?>
The above are some basic operations about mysql compiled for you. I hope it will be useful to you. Everyone helps.
For more information, please pay attention to the relevant tutorials on the php Chinese website.
Recommended related articles: https://www.php.cn/php-weizijiaocheng-428722.html
The above is the detailed content of Detailed introduction and examples of mysql operations (2). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;
