Home Database Mysql Tutorial mysql数据库的文件创建方式_MySQL

mysql数据库的文件创建方式_MySQL

Jun 01, 2016 pm 01:26 PM
mysql database Knowledge

bitsCN.com

最近发现对于数据库知识已经忘得差不多了,我的工作基本不涉及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的分割线----------------------#####
这里先介绍这些,东西不是很多,但是还是相对较为实用。

bitsCN.com
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

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.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

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.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

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.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

See all articles