MySQL command two ways to execute SQL file

Source: Internet
Author: User
Tags mysql command line phpmyadmin

    • MySQL command two ways to execute SQL file

    • Summary: As with other databases, MySQL also provides commands for executing SQL script files, making it easy to perform various operations such as databases, tables, and data. Below the author explains MySQL executes sql file command two methods, hope can give just start to learn MySQL database of friends.

  • Friends who have studied SQL Server or Oracle will know that SQL script is a collection of SQL statements containing one or more SQL commands, and we can put these SQL scripts in a text file (what we call "SQL script files") and execute the SQL script with related commands. File. The basic steps are as follows:

    1. Create SQL script files, such as the following SQL statements, copy them to Notepad, and save them as SQL suffix files.

    1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586 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 COLLATE latin1_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. Execute SQL script file with command

    Method one, executed under Windows using the cmd command (or under Unix or Linux console)
    "MySQL Bin directory" \mysql–u user name –p password –d database < "SQL script file path full name" Example:
    C:\mysql\bin\mysql–uroot–p123456-dtest<c:\test.sql


    Attention:
    A, if the use database is used in a SQL script file, the-d database option can be ignored
    B, if "MySQL Bin directory" contains spaces, you need to use "" "include, such as:" C:\Program files\mysql\bin\mysql "–u username –p password –d database <" SQL script file path full name "
    C, if SQL does not have a statement to create a database, and the database is not in the database management, then you must first create an empty database with a command.

    Method Two, enter the MySQL console (e.g. MySQL 5.5 command line Client) and use the source command to execute
    Mysql>source "path full name of SQL script file" or mysql>\. "Path full name of SQL script file", example:
    source C:\test.sql or \. C:\test.sql
    Open MySQL command line Client, enter the database password to log in, and then use the source command or \.

    Or

MySQL command two ways to execute SQL file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.