How to execute SQL scripts in mysql

Source: Internet
Author: User

The first step (generally omitted) for executing SQL scripts in mysql is to enable the mysql service (which runs automatically at startup by default): control panel --> management tools --> services, find the item named mysql in it, and double-click it to check whether its service status is starting or stopped, and set it to start the mysql connection: enter mysql-h localhost-u root-p in the command line and press enter, or directly run the mysql Connection Tool and enter the password. 1. write an SQL script, assuming the following content: SQL code create database dearabao; use dearabao; create table niuzi (name varchar (20); Save the script file, suppose I save it in the hello world Directory of drive F, so the path of this file is: F: \ hello world \ niuzi. SQL 2. there are two ways to execute an SQL Script: the first method: Under the command line (not connected to the database ), Enter mysql-h localhost-u root-p123456 <F: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path !!) Press enter. method 2: Enter source F: \ hello world \ niuzi in the command line (the database has been connected, and the prompt is mysql>. SQL (note that no quotation marks are required for the path) or \. f: \ hello world \ niuzi. SQL (note that the path does not need to be enclosed in quotation marks). You can press enter to put the SQL statement to be executed in an SQL file during program initialization, which is automatically executed during program installation, in fact, the SQL command is executed in batches. Similarly, you can manually execute the SQL file as follows: 1. log on to the MySQL server using the root account; 2. Run the source command: mysql> source c:/test. SQL note: "/" is recommended in the file path. If "\" is used, escape "\" first. Otherwise, an error may occur. There is also a load command that can insert data in batches, but this requirement on the file format is strict, otherwise it is prone to errors, rarely used. Mysql Command select user () to view all user statements. For example: (project Source: Shang) struts_training_itemmgr. SQL content: SQL code DROP TABLE t_items; DROP TABLE t_data_dict; create table t_items (item_no varchar (20) not null key, item_name varchar (20) not null, spec varchar (20), pattern varchar (10), category varchar (20 ), unit char (20); create table t_data_dict (id varchar (5) not null key, category varchar (20), name varchar (30 )); # t_data_dict initialization data insert into t_data_dict (id, category, name) values ('b01', 'item _ category ', 'proficient in Spring2.X Java Web developing '); insert into t_data_dict (id, category, name) values ('b02', 'item _ category ', 'java language and object-oriented programming'); insert into t_data_dict (id, category, name) values ('b03', 'item _ category ', '2b pencil'); insert into t_data_dict (id, category, name) values ('b04 ', 'item _ category ', 'hotrock NOTEBOOK'); insert into t_data_dict (id, category, name) values ('c01', 'item _ part', 'bene '); insert into t_data_dict (id, category, name) values ('c02 ', 'item _ unit', ''); insert into t_data_dict (id, category, name) values ('c03', 'item _ unit ', 'box'); go to MySql, select the database drpItem, and run source E: \ struts_training_itemmgr. SQL, as shown below: Enter password: * ****** Welcome to the MySQL monitor. commands end with; or \ g. your MySQL connection id is 110 Server version: 5.0.77-community-nt MySQL Community Edition (GPL) Type 'help; 'or' \ H' for help. type '\ C' to clear the buffer. mysql> use drpItem; Database changedmysql> source E: \ struts_training_itemmgr.sqlQuery OK, 0 rows affected (0.03 sec) Query OK, 0 rows affected (0.05 sec) Query OK, 0 rows affected (0.09 sec) Query OK, 0 rows affected (0.08 sec) Query OK, 1 row affected (0.03 sec) Query OK, 1 row affected (0.03 sec) Query OK, 1 row affected (0.03 sec) Query OK, 1 row affected (0.03 sec) Query OK, 1 row affected (0.02 sec) Query OK, 1 row affected (0.03 sec) Query OK, 1 row affected (0.03 sec) mysql>

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.