search
HomeDatabaseMysql Tutorialmysql数据库的文件创建方式

mysql数据库的文件创建方式

Jun 07, 2016 pm 03:33 PM
mysqlcreateDiscoverdatadatabasedocumentWay

最近发现对于数据库知识已经忘得差不多了,我的工作基本不涉及mysql数据库 年后人心都较为浮躁,因此希望找点事情弥补空虚的灵魂。MySQL数据库以前用过 这里将其拿出来深入研究了一下用文件创建数据库的方法,也许以后用得着。 #####-----------------------

      最近发现对于数据库知识已经忘得差不多了,我的工作基本不涉及mysql数据库
年后人心都较为浮躁,因此希望找点事情弥补空虚的灵魂。MySQL数据库以前用过
这里将其拿出来深入研究了一下用文件创建数据库的方法,也许以后用得着。

#####-----------------------mysql数据库开始(一)----------------#####
1、# 创建数据库语句

 create database mydb default character set utf8;
# 运用数据库语句

  use mydb;
# 创建表格,这里只简单的创建一张表格
# 设置InnoDB主要是为了事务操作的需要

create table mytable(
   id int primary key auto_increment,
   name varchar(20),
   count int not null
)type=InnoDB;

 

# cmd进入数据库方法
mysql -u root -p
#输入密码进入mysql命令行
#将以上sql语句放到.sql文件中,并导入数据库,这里是windows方式
#注意:这里路径不能添加引号,只需要直接放到source后即可,否则报错

#source $path\mydb.sql

mysql> source E:\mydb.sql
Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected, 1 warning (0.09 sec)


#describe mytable或者desc mytable检查表格字段是否有误
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
| count | int(11)     | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2、#向表格中导入数据或者导出数据
#首先采用.txt文本导入方式
#按照表格字段进行示例如下:
id name count
1 张三 1000
2 李四 500
3 王老虎 100
#将三组数据复制放到新建txt文本中,这么命名为:mydb.txt

mysql> load data infile 'E:\mydb.txt' into table mytable
    -> fields terminated by '\t' #表示字段间距
    -> lines terminated by '\n'; #表示行间距
Query OK, 3 rows affected (0.03 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
#由于数据较少这里通过简单的查询可以查看数据导入信息

mysql> select * from mytable;
+----+--------+-------+
| id | name   | count |
+----+--------+-------+
|  1 | 张三       |  1000 |
|  2 | 李四       |   500 |
|  3 | 王老虎      |   100 |
+----+--------+-------+
3 rows in set (0.05 sec)
#由于Id字段是自动增加的,所以这里尝试一下不添加字段
#预编写sql语句:load data local infile 'E:\mydb.txt' into table mytable(name,count);
#导入数据为:
赵大 1000
王二小 500
三亚子 100


mysql> load data local infile 'E:\mydb.txt' into table mytable(name,count);
Query OK, 3 rows affected (0.07 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
#查询新数据,这里的方法只适合数据较小的情况
mysql> select * from mytable;
+----+--------+-------+
| id | name   | count |
+----+--------+-------+
|  1 | 张三       |  1000 |
|  2 | 李四       |   500 |
|  3 | 王老虎      |   100 |
|  4 | 赵大      |  1000 |
|  5 | 王二小      |   500 |
|  6 | 三亚子       |   100 |
+----+--------+-------+
6 rows in set (0.00 sec)
#可以看到确实增加了三行数据,而且ID自增。


#这里尝试一下将表中数据导出到txt文件中,这里我们有选择的导出数据,如果全部导出与之类似
#下面方式会报错
select * into outfile 'E:\mydb1.txt'
lines terminated by '\r\n'
fields terminated by '\t'
from mytable;


#这个地方有一个奇特的现象fields行必须添加到lines上面即如下操作才不会报错:
mysql> select * into outfile 'E:\mydb1.txt'
    -> fields terminated by '\t' #在此例中该行可有可无
    -> lines terminated by '\r\n'
    -> from mytable ;
Query OK, 6 rows affected (0.00 sec)
#导出数据并不包括表字段。


#####----------------------mysql的分割线----------------------#####
这里先介绍这些,东西不是很多,但是还是相对较为实用。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)