Home  >  Article  >  php教程  >  Two methods for MySQL command to execute sql files

Two methods for MySQL command to execute sql files

高洛峰
高洛峰Original
2016-12-14 10:53:351610browse

Friends who have studied SQL Server or Oracle will know that a sql script is a collection of sql statements containing one or more sql commands. We can put these sql scripts in a text file (we call it a "sql script file"), Then execute this sql script file through the relevant commands. The basic steps are as follows:

1. Create a sql script file, such as the following sql statement, copy them to Notepad, and then save it as a sql suffix file.

c-- phpMyAdmin SQL Dump  
-- version 2.10.0.2  
-- http://www.phpmyadmin.net  
--   
-- 主机: localhost  
-- 生成日期: 2007 年 10 月 27 日 06:38  
-- 服务器版本: 5.0.37  
-- PHP 版本: 5.2.1  
       
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";  
       
--   
-- 数据库: `votesystem`  
--   
CREATE DATABASE `votesystem` DEFAULT CHARACTER SET latin1 COLLATElatin1_general_ci;  
USE `votesystem`;  
       
-- --------------------------------------------------------  
       
--   
-- 表的结构 `admin`  
--   
       
CREATE TABLE `admin` (  
  `username` char(20) NOT NULL default '',  
  `passwd` char(20) NOT NULL default '',  
  PRIMARY KEY  (`username`)  
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;  
       
--   
-- 导出表中的数据 `admin`  
--   
       
INSERT INTO `admin` VALUES ('admin', 'admin');  
       
-- --------------------------------------------------------  
       
--   
-- 表的结构 `voteitem`  
--   
       
CREATE TABLE `voteitem` (  
  `voteitem_id` smallint(5) unsigned NOT NULL auto_increment,  
  `vote_id` smallint(5) unsigned NOT NULL default '0',  
  `vote_item` varchar(100) NOT NULL default '',  
  `vote_count` smallint(5) unsigned NOT NULL default '0',  
  PRIMARY KEY  (`voteitem_id`)  
) ENGINE=MyISAM  DEFAULT CHARSET=gb2312 AUTO_INCREMENT=34 ;  
       
--   
-- 导出表中的数据 `voteitem`  
--   
       
INSERT INTO `voteitem` VALUES (25, 6, 'perl', 10);  
INSERT INTO `voteitem` VALUES (24, 6, 'python', 20);  
INSERT INTO `voteitem` VALUES (23, 6, 'c++', 20);  
INSERT INTO `voteitem` VALUES (22, 6, 'c', 15);  
INSERT INTO `voteitem` VALUES (21, 6, 'php', 25);  
INSERT INTO `voteitem` VALUES (29, 6, 'shell', 5);  
INSERT INTO `voteitem` VALUES (28, 6, 'asm', 5);  
INSERT INTO `voteitem` VALUES (27, 6, 'java', 3);  
INSERT INTO `voteitem` VALUES (26, 6, 'c#', 4);  
INSERT INTO `voteitem` VALUES (33, 7, 'Mac OS', 60);  
INSERT INTO `voteitem` VALUES (32, 7, 'OS/2', 5);  
INSERT INTO `voteitem` VALUES (31, 7, 'Windows', 50);  
INSERT INTO `voteitem` VALUES (30, 7, 'Linux', 51);  
       
-- --------------------------------------------------------  
       
--   
-- 表的结构 `votemain`  
--   
       
CREATE TABLE `votemain` (  
  `vote_id` smallint(5) unsigned NOT NULL auto_increment,  
  `vote_name` varchar(100) NOT NULL default '',  
  `vote_time` datetime NOT NULL default '0000-00-00 00:00:00',  
  PRIMARY KEY  (`vote_id`)  
) ENGINE=MyISAM  DEFAULT CHARSET=gb2312 AUTO_INCREMENT=8 ;  
       
--   
-- 导出表中的数据 `votemain`  
--   
       
INSERT INTO `votemain` VALUES (7, '你最喜欢的系统', '2007-10-26 14:10:13');  
INSERT INTO `votemain` VALUES (6, '你最喜欢的语言', '2007-10-26 14:09:15');

2. Use the command to execute the sql script file

Method 1, use the cmd command under Windows (or Unix or Linux console)
[Mysql bin directory] mysql –u username –p password –D database C:MySQLbinmysql –uroot –p123456 -Dtest

Two methods for MySQL command to execute sql files

Note:
A. If the use database is used in the sql script file, Then the -D database option can be ignored
B. If the [Mysql bin directory] contains spaces, you need to use "" to include it, such as: "C:Program FilesMySQLbinmysql" –u username –p password –D databaseC. If sql does not have a statement to create a database, and the database does not exist in database management, you must first create an empty database with a command.

Method 2, enter the MySQL console (such as: MySQL 5.5 Command Line Client), use the source command to execute
Mysql>source [full path name of sql script file] or Mysql>. [full path name of sql script file], Example:
source C:test.sql or . C:test.sql
Open the MySQL Command Line Client, enter the database password to log in, and then use the source command or .

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