Free learning recommendations: mysql video tutorial
Article Directory
- Advanced operations on data tables
- Preparation: Install MySQL database
- 1. Clone table
- Method 1
- Method 2
- 2. Clear the table and delete all data in the table
- Method one
- Method two
- 3. Create a temporary table
- 4. Create a foreign key constraint
-
- 6 common constraints in MySQL
- 5. Database user management
- 1. Create new user
- 2. View user information
- 3. Rename user
- 4. Delete user
- 5. Modify current login user password
- 6. Modify Other user passwords
- 7. Solutions for forgetting the root password
- 6. Database user authorization
- 1. Grant permissions
- 2. View permissions
- 3. Revoke permissions
##Advanced data table operations
Preparation: Install MySQL database
Shell script one-click deployment - source code compilation and installation of MySQLcreate database CLASS; use CLASS; create table TEST (id int not null,name char(20) not null,cardid varchar(18) not null unique key,primary key (id)); insert into TEST(id,name,cardid) values (1,'zhangsan','123123'); insert into TEST(id,name,cardid) values (2,'lisi','1231231'); insert into TEST(id,name,cardid) values (3,'wangwu','12312312'); select * from TEST;
1. Clone the table
Generate the data records of the data table into a new table
Method 1
例:create table TEST01 like TEST; select * from TEST01; desc TEST01; insert into TEST01 select * from TEST; select * from TEST01;
Method 2
例:create table TEST02 (select * from TEST); select * from TEST02;
2. Clear the table and delete all entries in the table Data
Method 1
delete from TEST02;
#After DELETE clears the table, the returned result contains deleted record entries; DELETE works row by row Deleting record data; if there are self-increasing fields in the table, after using DELETE FROM to delete all records, the newly added records will continue to increment and write records from the original largest record ID
例:create table if not exists TEST03 (id int primary key auto_increment,name varchar(20) not null,cardid varchar(18) not null unique key); show tables; insert into TEST03 (name,cardid) values ('zhangsan','11111'); select * from TEST03; delete from TEST03; insert into TEST03 (name,cardid) values ('lisi','22222'); select * from TEST03;
Method 2
例:select * from TEST03; truncate table TEST03; insert into TEST03 (name,cardid) values ('wangwu','33333'); select * from TEST03;
#TRUNCATE After clearing the table, no deleted entries are returned; When TRUNCATE works, the table structure is re-established as it is, so TRUNCATE will clear the table faster than DELETE in terms of speed; after using TRUNCATE TABLE to clear the data in the table, the ID will be recorded again from 1.
3. Create a temporary table
After the temporary table is successfully created, use SHOW TABLES The command cannot see the temporary table created, and the temporary table will be destroyed after the connection exits. If before exiting the connection, you can also perform operations such as addition, deletion, modification, and query, such as using the DROP TABLE statement to manually delete the temporary table directly.CREATE TEMPORARY TABLE 表名 (字段1 数据类型,字段2 数据类型[,...][,PRIMARY KEY (主键名)]); 例:create temporary table TEST04 (id int not null,name varchar(20) not null,cardid varchar(18) not null unique key,primary key (id)); show tables; insert into TEST04 values (1,'haha','12345'); select * from TEST04;
4. Create foreign key constraints
Ensure data integrity and consistency Definition of foreign key: If the same attribute field x is the primary key in table one but not the primary key in table two, then field x is called the foreign key of table two.
1. Tables with public keywords as primary keys are primary key tables (parent tables, primary tables)
2. Use public keywords as foreign keys The table is a foreign key table (slave table, external table)
例:create table TEST04 (hobid int(4),hobname varchar(50)); create table TEST05 (id int(4) primary key auto_increment,name varchar(50),age int(4),hobid int(4)); alter table TEST04 add constraint PK_hobid primary key(hobid); alter table TEST05 add constraint FK_hobid foreign key(hobid) references TEST04(hobid);
例:添加数据记录 insert into TEST05 values (1,'zhangsan','20',1); insert into TEST04 values (1,'sleep'); insert into TEST05 values (1,'zhangsan',20,1);
例:drop table TEST04; drop table TEST05; drop table TEST04;
Note: If you want to delete the foreign key Constraint field
Delete the foreign key constraint first, and then delete the foreign key name. This is not demonstrated here
show create table TEST05; alter table TEST05 drop foreign key FK_hobid; alter table TEST05 drop key FK_hobid; desc TEST05;
6 common constraints in MySQL
主键约束 | primary key |
---|---|
外键约束 | foreign key |
非空约束 | not null |
唯一约束 | unique [key |
默认值约束 | default |
自增约束 | auto_increment |
五、数据库用户管理
1、新建用户
CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];
‘用户名’:指定将创建的用户名
‘来源地址’:指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录可用通配符%
‘密码’:若使用明文密码,直接输入’密码’,插入到数据库时由Mysql自动加密;
------若使用加密密码,需要先使用SELECT PASSWORD(‘密码’); 获取密文,再在语句中添加 PASSWORD ‘密文’;
------若省略“IDENTIFIED BY”部分,则用户的密码将为空(不建议使用)
例:create user 'zhangsan'@'localhost' identified by '123123'; select password('123123'); create user 'lisi'@'localhost' identified by password '*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1';
2、查看用户信息
创建后的用户保存在 mysql 数据库的 user 表里
USE mysql; SELECT User,authentication_string,Host from user;
3、重命名用户
RENAME USER 'zhangsan'@'localhost' TO 'wangwu'@'localhost'; SELECT User,authentication_string,Host from user;
4、删除用户
DROP USER 'lisi'@'localhost'; SELECT User,authentication_string,Host from user;
5、修改当前登录用户密码
SET PASSWORD = PASSWORD('abc123'); quit mysql -u root -p
6、修改其他用户密码
SET PASSWORD FOR 'wangwu'@'localhost' = PASSWORD('abc123'); use mysql; SELECT User,authentication_string,Host from user;
7、忘记 root 密码的解决办法
1、修改 /etc/my.cnf 配置文件,不使用密码直接登录到 mysql
vim /etc/my.cnf [mysqld] skip-grant-tables #添加,使登录mysql不使用授权表 systemctl restart mysqld mysql #直接登录
2、使用 update 修改 root 密码,刷新数据库
UPDATE mysql.user SET AUTHENTICATION_STRING = PASSWORD('112233') where user='root'; FLUSH PRIVILEGES; quit 再把 /etc/my.cnf 配置文件里的 skip-grant-tables 删除,并重启 mysql 服务。 mysql -u root -p 112233
六、数据库用户授权
1、授予权限
GRANT语句:专门用来设置数据库用户的访问权限。当指定的用户名不存在时,GRANT语句将会创建新的用户;当指定的用户名存在时,GRANT 语句用于修改用户信息。 GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];
#权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。使用“all”表示所有权限,可授权执行任何操作。 #数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符“*”。*例如,使用“kgc.*”表示授权操作的对象为 kgc数据库中的所有表。 #'用户名@来源地址':用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。来源地址可以是域名、IP 地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.lic.com”、“192.168.184.%”等。 #IDENTIFIED BY:用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略“IDENTIFIED BY”部分, 则用户的密码将为空。
#允许用户wangwu在本地查询 CLASS 数据库中所有表的数据记录,但禁止查询其他数据库中的表的记录。
例: GRANT select ON CLASS.* TO 'wangwu'@'localhost' IDENTIFIED BY '123456'; quit; mysql -u wangwu -p 123456 show databases; use information_schema; show tables; select * from INNODB_SYS_TABLESTATS;
#允许用户wangwu在本地远程连接 mysql ,并拥有所有权限。
quit; mysql -u root -p112233 GRANT ALL PRIVILEGES ON *.* TO 'wangwu'@'localhost' IDENTIFIED BY '123456'; flush privileges; quit mysql -u wangwu -p123456 create database SCHOOL;
2、查看权限
SHOW GRANTS FOR 用户名@来源地址; 例: SHOW GRANTS FOR 'wangwu'@'localhost';
3、撤销权限
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址; 例:quit; mysql -u root -p112233 SHOW GRANTS FOR 'wangwu'@'localhost'; REVOKE SELECT ON "CLASS".* FROM 'wangwu'@'localhost'; SHOW GRANTS FOR 'wangwu'@'localhost';
#USAGE权限只能用于数据库登陆,不能执行任何操作;USAGE权限不能被回收,即 REVOKE 不能删除用户。
flush privileges;
更多相关免费学习推荐:mysql教程(视频)
The above is the detailed content of Take a look at MySQL database advanced operations. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.