zoukankan      html  css  js  c++  java
  • MySQL基础技能

    登录退出MySQL

    • 登录命令
    mysql -u 用户名 -p 密码 -h IP -P 端口
    mysql -u root -p password -h 127.0.0.1 -P 3306
    
    • 一般登录命令
    mysql -u 用户名 -p 密码
    mysql -u root -p	#如果在-p后没有提供密码,则会在执行命令后要求用户输入密码
    

    完整的命令行选项和参数列表可以用mysql --help获得。

    • 退出命令
    quit 或 exit
    

    注意事项

    • 命令用 ; 或 g 结束,换句话说,Enter不执行命令;
    • 输入 help 或 h 获得帮助,也可以输入更多的文本获得特定命令帮助;
    • 想要取消命令重新输入的话直接输入c;
    • 主机名如果连接到本地服务器,为localhost;
    • 默认端口为3306;

    导入到处数据

    • 导出整个数据库中的所有数据
    mysql -u root -p  dabaseName > fileName.sql	#fileName.sql最好加上路径名
    
    • 导出数据库中的某个表的数据
    mysqldump -u userName -p  dabaseName tableName > fileName.sql 
    
    • 导出整个数据库中的所有的表结构
    mysqldump -u userName -p -d dabaseName  > fileName.sql
    
    • 导出整个数据库中某个表的表结构
    mysqldump -u userName -p -d dabaseName tableName > fileName.sql
    
    • 导入mysql
    mysql -uroot -p database < fileName.sql	#注意fileName.sql要有路径名
    
    • 导入mysql
    mysql>source fileName.sql	#注意fileName.sql要有路径名
    
    • 这里有官方sql代码,试着导入MySQL,以下将以这个为例。
    /*
     Navicat Premium Data Transfer
    
     Source Server         : localhost_3306
     Source Server Type    : MySQL
     Source Server Version : 50612
     Source Host           : localhost:3306
     Source Schema         : zlx_mysql
    
     Target Server Type    : MySQL
     Target Server Version : 50612
     File Encoding         : 65001
    
     Date: 19/12/2019 23:03:46
    */
    
    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    -- ----------------------------
    -- Table structure for customers
    -- ----------------------------
    DROP DATABASE IF EXISTS customers;
    CREATE DATABASE IF NOT EXISTS customers;
    use customers;
    DROP TABLE IF EXISTS `customers`;
    CREATE TABLE `customers`  (
      `cust_id` int(11) NOT NULL AUTO_INCREMENT,
      `cust_name` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
      `cust_address` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `cust_city` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `cust_state` char(5) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `cust_zip` char(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `cust_country` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `cust_contact` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `cust_email` char(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      PRIMARY KEY (`cust_id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 10006 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;
    
    -- ----------------------------
    -- Records of customers
    -- ----------------------------
    INSERT INTO `customers` VALUES (10001, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com');
    INSERT INTO `customers` VALUES (10002, 'Mouse House', '333 Fromage Lane', 'Columbus', 'OH', '43333', 'USA', 'Jerry Mouse', NULL);
    INSERT INTO `customers` VALUES (10003, 'Wascals', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'rabbit@wascally.com');
    INSERT INTO `customers` VALUES (10004, 'Yosemite Place', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Y Sam', 'sam@yosemite.com');
    INSERT INTO `customers` VALUES (10005, 'E Fudd', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'E Fudd', NULL);
    
    -- ----------------------------
    -- Table structure for orderitems
    -- ----------------------------
    DROP TABLE IF EXISTS `orderitems`;
    CREATE TABLE `orderitems`  (
      `order_num` int(11) NOT NULL,
      `order_item` int(11) NOT NULL,
      `prod_id` char(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
      `quantity` int(11) NOT NULL,
      `item_price` decimal(8, 2) NOT NULL,
      PRIMARY KEY (`order_num`, `order_item`) USING BTREE,
      INDEX `fk_orderitems_products`(`prod_id`) USING BTREE,
      CONSTRAINT `fk_orderitems_products` FOREIGN KEY (`prod_id`) REFERENCES `products` (`prod_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `fk_orderitems_orders` FOREIGN KEY (`order_num`) REFERENCES `orders` (`order_num`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;
    
    -- ----------------------------
    -- Records of orderitems
    -- ----------------------------
    INSERT INTO `orderitems` VALUES (20005, 1, 'ANV01', 10, 5.99);
    INSERT INTO `orderitems` VALUES (20005, 2, 'ANV02', 3, 9.99);
    INSERT INTO `orderitems` VALUES (20005, 3, 'TNT2', 5, 10.00);
    INSERT INTO `orderitems` VALUES (20005, 4, 'FB', 1, 10.00);
    INSERT INTO `orderitems` VALUES (20006, 1, 'JP2000', 1, 55.00);
    INSERT INTO `orderitems` VALUES (20007, 1, 'TNT2', 100, 10.00);
    INSERT INTO `orderitems` VALUES (20008, 1, 'FC', 50, 2.50);
    INSERT INTO `orderitems` VALUES (20009, 1, 'FB', 1, 10.00);
    INSERT INTO `orderitems` VALUES (20009, 2, 'OL1', 1, 8.99);
    INSERT INTO `orderitems` VALUES (20009, 3, 'SLING', 1, 4.49);
    INSERT INTO `orderitems` VALUES (20009, 4, 'ANV03', 1, 14.99);
    
    -- ----------------------------
    -- Table structure for orders
    -- ----------------------------
    DROP TABLE IF EXISTS `orders`;
    CREATE TABLE `orders`  (
      `order_num` int(11) NOT NULL AUTO_INCREMENT,
      `order_date` datetime(0) NOT NULL,
      `cust_id` int(11) NOT NULL,
      PRIMARY KEY (`order_num`) USING BTREE,
      INDEX `fk_orders_customers`(`cust_id`) USING BTREE,
      CONSTRAINT `fk_orders_customers` FOREIGN KEY (`cust_id`) REFERENCES `customers` (`cust_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB AUTO_INCREMENT = 20010 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;
    
    -- ----------------------------
    -- Records of orders
    -- ----------------------------
    INSERT INTO `orders` VALUES (20005, '2005-09-01 00:00:00', 10001);
    INSERT INTO `orders` VALUES (20006, '2005-09-12 00:00:00', 10003);
    INSERT INTO `orders` VALUES (20007, '2005-09-30 00:00:00', 10004);
    INSERT INTO `orders` VALUES (20008, '2005-10-03 00:00:00', 10005);
    INSERT INTO `orders` VALUES (20009, '2005-10-08 00:00:00', 10001);
    
    -- ----------------------------
    -- Table structure for productnotes
    -- ----------------------------
    DROP TABLE IF EXISTS `productnotes`;
    CREATE TABLE `productnotes`  (
      `note_id` int(11) NOT NULL AUTO_INCREMENT,
      `prod_id` char(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
      `note_date` datetime(0) NOT NULL,
      `note_text` text CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL,
      PRIMARY KEY (`note_id`) USING BTREE,
      FULLTEXT INDEX `note_text`(`note_text`)
    ) ENGINE = MyISAM AUTO_INCREMENT = 115 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Records of productnotes
    -- ----------------------------
    INSERT INTO `productnotes` VALUES (101, 'TNT2', '2005-08-17 00:00:00', 'Customer complaint:
    Sticks not individually wrapped, too easy to mistakenly detonate all at once.
    Recommend individual wrapping.');
    INSERT INTO `productnotes` VALUES (102, 'OL1', '2005-08-18 00:00:00', 'Can shipped full, refills not available.
    Need to order new can if refill needed.');
    INSERT INTO `productnotes` VALUES (103, 'SAFE', '2005-08-18 00:00:00', 'Safe is combination locked, combination not provided with safe.
    This is rarely a problem as safes are typically blown up or dropped by customers.');
    INSERT INTO `productnotes` VALUES (104, 'FC', '2005-08-19 00:00:00', 'Quantity varies, sold by the sack load.
    All guaranteed to be bright and orange, and suitable for use as rabbit bait.');
    INSERT INTO `productnotes` VALUES (105, 'TNT2', '2005-08-20 00:00:00', 'Included fuses are short and have been known to detonate too quickly for some customers.
    Longer fuses are available (item FU1) and should be recommended.');
    INSERT INTO `productnotes` VALUES (106, 'TNT2', '2005-08-22 00:00:00', 'Matches not included, recommend purchase of matches or detonator (item DTNTR).');
    INSERT INTO `productnotes` VALUES (107, 'SAFE', '2005-08-23 00:00:00', 'Please note that no returns will be accepted if safe opened using explosives.');
    INSERT INTO `productnotes` VALUES (108, 'ANV01', '2005-08-25 00:00:00', 'Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils.');
    INSERT INTO `productnotes` VALUES (109, 'ANV03', '2005-09-01 00:00:00', 'Item is extremely heavy. Designed for dropping, not recommended for use with slings, ropes, pulleys, or tightropes.');
    INSERT INTO `productnotes` VALUES (110, 'FC', '2005-09-01 00:00:00', 'Customer complaint: rabbit has been able to detect trap, food apparently less effective now.');
    INSERT INTO `productnotes` VALUES (111, 'SLING', '2005-09-02 00:00:00', 'Shipped unassembled, requires common tools (including oversized hammer).');
    INSERT INTO `productnotes` VALUES (112, 'SAFE', '2005-09-02 00:00:00', 'Customer complaint:
    Circular hole in safe floor can apparently be easily cut with handsaw.');
    INSERT INTO `productnotes` VALUES (113, 'ANV01', '2005-09-05 00:00:00', 'Customer complaint:
    Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead.');
    INSERT INTO `productnotes` VALUES (114, 'SAFE', '2005-09-07 00:00:00', 'Call from individual trapped in safe plummeting to the ground, suggests an escape hatch be added.
    Comment forwarded to vendor.');
    
    -- ----------------------------
    -- Table structure for products
    -- ----------------------------
    DROP TABLE IF EXISTS `products`;
    CREATE TABLE `products`  (
      `prod_id` char(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
      `vend_id` int(11) NOT NULL,
      `prod_name` char(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
      `prod_price` decimal(8, 2) NOT NULL,
      `prod_desc` text CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL,
      PRIMARY KEY (`prod_id`) USING BTREE,
      INDEX `fk_products_vendors`(`vend_id`) USING BTREE,
      CONSTRAINT `fk_products_vendors` FOREIGN KEY (`vend_id`) REFERENCES `vendors` (`vend_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;
    
    -- ----------------------------
    -- Records of products
    -- ----------------------------
    INSERT INTO `products` VALUES ('ANV01', 1001, '.5 ton anvil', 5.99, '.5 ton anvil, black, complete with handy hook');
    INSERT INTO `products` VALUES ('ANV02', 1001, '1 ton anvil', 9.99, '1 ton anvil, black, complete with handy hook and carrying case');
    INSERT INTO `products` VALUES ('ANV03', 1001, '2 ton anvil', 14.99, '2 ton anvil, black, complete with handy hook and carrying case');
    INSERT INTO `products` VALUES ('DTNTR', 1003, 'Detonator', 13.00, 'Detonator (plunger powered), fuses not included');
    INSERT INTO `products` VALUES ('FB', 1003, 'Bird seed', 10.00, 'Large bag (suitable for road runners)');
    INSERT INTO `products` VALUES ('FC', 1003, 'Carrots', 2.50, 'Carrots (rabbit hunting season only)');
    INSERT INTO `products` VALUES ('FU1', 1002, 'Fuses', 3.42, '1 dozen, extra long');
    INSERT INTO `products` VALUES ('JP1000', 1005, 'JetPack 1000', 35.00, 'JetPack 1000, intended for single use');
    INSERT INTO `products` VALUES ('JP2000', 1005, 'JetPack 2000', 55.00, 'JetPack 2000, multi-use');
    INSERT INTO `products` VALUES ('OL1', 1002, 'Oil can', 8.99, 'Oil can, red');
    INSERT INTO `products` VALUES ('SAFE', 1003, 'Safe', 50.00, 'Safe with combination lock');
    INSERT INTO `products` VALUES ('SLING', 1003, 'Sling', 4.49, 'Sling, one size fits all');
    INSERT INTO `products` VALUES ('TNT1', 1003, 'TNT (1 stick)', 2.50, 'TNT, red, single stick');
    INSERT INTO `products` VALUES ('TNT2', 1003, 'TNT (5 sticks)', 10.00, 'TNT, red, pack of 10 sticks');
    
    -- ----------------------------
    -- Table structure for vendors
    -- ----------------------------
    DROP TABLE IF EXISTS `vendors`;
    CREATE TABLE `vendors`  (
      `vend_id` int(11) NOT NULL AUTO_INCREMENT,
      `vend_name` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
      `vend_address` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `vend_city` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `vend_state` char(5) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `vend_zip` char(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      `vend_country` char(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
      PRIMARY KEY (`vend_id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 1007 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;
    
    -- ----------------------------
    -- Records of vendors
    -- ----------------------------
    INSERT INTO `vendors` VALUES (1001, 'Anvils R Us', '123 Main Street', 'Southfield', 'MI', '48075', 'USA');
    INSERT INTO `vendors` VALUES (1002, 'LT Supplies', '500 Park Street', 'Anytown', 'OH', '44333', 'USA');
    INSERT INTO `vendors` VALUES (1003, 'ACME', '555 High Street', 'Los Angeles', 'CA', '90046', 'USA');
    INSERT INTO `vendors` VALUES (1004, 'Furball Inc.', '1000 5th Avenue', 'New York', 'NY', '11111', 'USA');
    INSERT INTO `vendors` VALUES (1005, 'Jet Set', '42 Galaxy Road', 'London', NULL, 'N16 6PS', 'England');
    INSERT INTO `vendors` VALUES (1006, 'Jouets Et Ours', '1 Rue Amusement', 'Paris', NULL, '45678', 'France');
    
    SET FOREIGN_KEY_CHECKS = 1;
    

    USE

    选择数据库

    mysql> USE customers;
    Database changed
    
    • USE 语句表示选择数据库;
    • 例如在数据库操作软件中,会看到被选择的数据库(黑体加亮),而且应用标题栏将显示所选择的数据库名;
    • 必须先使用USE打开数据库,才能读取其中的数据。

    SHOW

    显示数据库的列表

    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | customers          |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.00 sec)
    
    • SHOW DATABASES;返回可用数据库的一个列表;
    • 包含在这个列表中的可能是MySQL内部使用的数据库。

    显示数据库内表的列表

    mysql> SHOW TABLES;
    +---------------------+
    | Tables_in_customers |
    +---------------------+
    | customers           |
    | orderitems          |
    | orders              |
    | productnotes        |
    | products            |
    | vendors             |
    +---------------------+
    6 rows in set (0.01 sec)
    
    • SHOW TABLES;返回当前选择的数据库内可用表的列表。

    显示数据表的列

    mysql> SHOW COLUMNS FROM customers;
    +--------------+-----------+------+-----+---------+----------------+
    | Field        | Type      | Null | Key | Default | Extra          |
    +--------------+-----------+------+-----+---------+----------------+
    | cust_id      | int       | NO   | PRI | NULL    | auto_increment |
    | cust_name    | char(50)  | NO   |     | NULL    |                |
    | cust_address | char(50)  | YES  |     | NULL    |                |
    | cust_city    | char(50)  | YES  |     | NULL    |                |
    | cust_state   | char(5)   | YES  |     | NULL    |                |
    | cust_zip     | char(10)  | YES  |     | NULL    |                |
    | cust_country | char(50)  | YES  |     | NULL    |                |
    | cust_contact | char(50)  | YES  |     | NULL    |                |
    | cust_email   | char(255) | YES  |     | NULL    |                |
    +--------------+-----------+------+-----+---------+----------------+
    9 rows in set (0.00 sec)
    
    mysql> DESCRIBE customers;
    +--------------+-----------+------+-----+---------+----------------+
    | Field        | Type      | Null | Key | Default | Extra          |
    +--------------+-----------+------+-----+---------+----------------+
    | cust_id      | int       | NO   | PRI | NULL    | auto_increment |
    | cust_name    | char(50)  | NO   |     | NULL    |                |
    | cust_address | char(50)  | YES  |     | NULL    |                |
    | cust_city    | char(50)  | YES  |     | NULL    |                |
    | cust_state   | char(5)   | YES  |     | NULL    |                |
    | cust_zip     | char(10)  | YES  |     | NULL    |                |
    | cust_country | char(50)  | YES  |     | NULL    |                |
    | cust_contact | char(50)  | YES  |     | NULL    |                |
    | cust_email   | char(255) | YES  |     | NULL    |                |
    +--------------+-----------+------+-----+---------+----------------+
    9 rows in set (0.00 sec)
    
    • SHOW COLUMNS要求给出一个表名,它对每个字段返回一行,行中包含字段名,数据类型,是否允许NULL,键信息,默认值以及其他信息。
    • DESCRIBE customers;是它的一种快捷方式。

    其他SHOW语句

    • SHOW STATUS,用于显示广泛的服务器状态信息;
    • SHOW CREATE DATABASE和SHOW CREATE TABLE,分别用来显示特定数据库或表的MySQL语句;
    • SHOW GRANTS,用来显示授予用户(所有用户或特定用户)的安全权限;
    • SHOW ERRORS和SHOWWARNINGS,用来显示服务器错误或警告消息。
    • 更多的执行HELP SHOW;显示允许的SHOW语句。
  • 相关阅读:
    文档加载完后执行相关事件
    流程步骤(备用)
    浏览器常见内核
    修改!important定义的样式(2)
    样式被!important 后修改的方法
    产生BFC环境的几种方式
    当我们访问一个网址后发生了什么?
    Java并发基础--线程通信
    Java 集合学习--ArrayList
    Java并发基础--线程安全
  • 原文地址:https://www.cnblogs.com/chengmf/p/13139438.html
Copyright © 2011-2022 走看看