设为首页 收藏本站
查看: 566|回复: 0

[经验分享] Mysql 基础操作 DDL DML DCL

[复制链接]

尚未签到

发表于 2015-12-22 12:32:35 | 显示全部楼层 |阅读模式
mysql 基础操作  包括DDL DML DCL示例  
  
c:\Program Files\MySQL\MySQL Server 5.1>cd bin   c:\Program Files\MySQL\MySQL Server 5.1\bin>mysql -u root -pmysql  --登陆
  Welcome to the MySQL monitor.  Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.1.62-community MySQL Community Server (GPL)
  
  
  Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  
  
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
  
  
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  
  
  mysql> show databases;  --显示所有数据库
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | test               |
  | yws                |
  +--------------------+
  4 rows in set (0.07 sec)
  
  
  mysql> use yws
  Database changed
  mysql> show tables \g
  +---------------+
  | Tables_in_yws |
  +---------------+
  | userinfo      |
  | zzm           |
  +---------------+
  2 rows in set (0.04 sec)
  
  
  mysql>use mysql  --选择某个数据库
  Database changed
  mysql>show tables \g 查看本数据库下所有表
  +---------------------------+
  | Tables_in_mysql           |
  +---------------------------+
  | columns_priv              |
  | db                        |
  | event                     |
  | func                      |
  | general_log               |
  | help_category             |
  | help_keyword              |
  | help_relation             |
  | help_topic                |
  | host                      |
  | ndb_binlog_index          |
  | plugin                    |
  | proc                      |
  | procs_priv                |
  | servers                   |
  | slow_log                  |
  | tables_priv               |
  | time_zone                 |
  | time_zone_leap_second     |
  | time_zone_name            |
  | time_zone_transition      |
  | time_zone_transition_type |
  | user                      |
  +---------------------------+
  23 rows in set (0.29 sec)
  
  
  mysql>
  mysql> use yws \g
  Query OK, 0 rows affected (0.00 sec)
  
  
  
  mysql> create table emp(ename varchar(10) ,hiredate date,sal decimal(10,2),deptno int(2)); --创建表
  Query OK, 0 rows affected (0.16 sec)
  
  
  mysql> desc emp; --描述表结构
  +----------+---------------+------+-----+---------+-------+
  | Field    | Type          | Null | Key | Default | Extra |
  +----------+---------------+------+-----+---------+-------+
  | ename    | varchar(10)   | YES  |     | NULL    |       |
  | hiredate | date          | YES  |     | NULL    |       |
  | sal      | decimal(10,2) | YES  |     | NULL    |       |
  | deptno   | int(2)        | YES  |     | NULL    |       |
  +----------+---------------+------+-----+---------+-------+
  4 rows in set (0.02 sec)
  
  
  
  mysql> show create table emp \G;  --查看创建表SQL 利用\G选项更加美观
  *************************** 1. row ***************************
  Table: emp
  Create Table: CREATE TABLE `emp` (
  `ename` varchar(10) DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` decimal(10,2) DEFAULT NULL,
  `deptno` int(2) DEFAULT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  1 row in set (0.00 sec)
  
  
  ERROR:
  No query specified
  
  
  mysql> alter table emp modify ename varchar(20); --修改字段类型
  Query OK, 0 rows affected (0.26 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql>>
  Query OK, 0 rows affected (0.22 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql>>
  Query OK, 0 rows affected (0.19 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  

  mysql>>  Query OK, 0 rows affected (0.18 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql> alter table emp change age age1 int(4); --修改列名称和列类型
  Query OK, 0 rows affected (0.15 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  note:change和modify都可以修改,不同的是 change需要写两次列名称,但是可以修改列的名称,modify只可以修改类型。
  
  mysql> alter table emp add birth date after ename; --修改列字段顺序
  Query OK, 0 rows affected (0.20 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  note:mysql独特的功能,可以调整表字段顺序。
  mysql>
  
  
  mysql> alter table emp modify age1 int(3) first;--修改列字段顺序
  Query OK, 0 rows affected (0.17 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql> desc emp;
  +----------+---------------+------+-----+---------+-------+
  | Field    | Type          | Null | Key | Default | Extra |
  +----------+---------------+------+-----+---------+-------+
  | age1     | int(3)        | YES  |     | NULL    |       |
  | ename    | varchar(20)   | YES  |     | NULL    |       |
  | birth    | date          | YES  |     | NULL    |       |
  | hiredate | date          | YES  |     | NULL    |       |
  | sal      | decimal(10,2) | YES  |     | NULL    |       |
  | deptno   | int(2)        | YES  |     | NULL    |       |
  +----------+---------------+------+-----+---------+-------+
  6 rows in set (0.01 sec)
  
  
  mysql>
  mysql>>
  Query OK, 0 rows affected (0.06 sec)
  
  
  
  

  mysql>>  Query OK, 0 rows affected (0.17 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql> desc y_emp;
  +----------+---------------+------+-----+---------+-------+
  | Field    | Type          | Null | Key | Default | Extra |
  +----------+---------------+------+-----+---------+-------+
  | ename    | varchar(20)   | YES  |     | NULL    |       |
  | birth    | date          | YES  |     | NULL    |       |
  | hiredate | date          | YES  |     | NULL    |       |
  | sal      | decimal(10,2) | YES  |     | NULL    |       |
  | deptno   | int(2)        | YES  |     | NULL    |       |
  | age1     | int(3)        | YES  |     | NULL    |       |
  +----------+---------------+------+-----+---------+-------+
  6 rows in set (0.01 sec)
  
  

  mysql>>  Query OK, 0 rows affected (0.22 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql> desc y_emp;
  +----------+---------------+------+-----+---------+-------+
  | Field    | Type          | Null | Key | Default | Extra |
  +----------+---------------+------+-----+---------+-------+
  | ename    | varchar(20)   | YES  |     | NULL    |       |
  | birth    | date          | YES  |     | NULL    |       |
  | hiredate | date          | YES  |     | NULL    |       |
  | sal      | decimal(10,2) | YES  |     | NULL    |       |
  | deptno   | int(2)        | YES  |     | NULL    |       |
  | age      | int(4)        | YES  |     | NULL    |       |
  +----------+---------------+------+-----+---------+-------+
  6 rows in set (0.01 sec)
  
  

  mysql>>  Query OK, 0 rows affected (0.20 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  

  mysql>>  Query OK, 0 rows affected (0.21 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  
  
  mysql>
  
  
  mysql>insert into y_emp values ('yws','2015-03-16',5000,2);  --插入数据
  Query OK, 1 row affected (0.07 sec)
  
  
  mysql>insert into y_emp values ('yws','2015-03-16',5000,2),('xy','2015-03-02',3000,2); --可以插入多列数据
  Query OK, 2 rows affected (0.04 sec)
  Records: 2  Duplicates: 0  Warnings: 0
  
  
  mysql> select count(*) from y_emp;
  +----------+
  | count(*) |
  +----------+
  |        3 |
  +----------+
  1 row in set (0.03 sec)
  
  
  mysql> commit;
  Query OK, 0 rows affected (0.00 sec)
  
  limit的简单用法
  limit (start,start_offerset)
  mysql>
  mysql> select * from y_emp limit 1; 
  +-------+------------+---------+--------+
  | ename | hiredate   | sal     | deptno |
  +-------+------------+---------+--------+
  | yws   | 2015-03-16 | 5000.00 |      2 |
  +-------+------------+---------+--------+
  1 row in set (0.00 sec)
  
  
  mysql>select * from y_emp limit 1,2; 
  +-------+------------+---------+--------+
  | ename | hiredate   | sal     | deptno |
  +-------+------------+---------+--------+
  | yws   | 2015-03-16 | 5000.00 |      2 |
  | xy    | 2015-03-02 | 3000.00 |      2 |
  +-------+------------+---------+--------+
  2 rows in set (0.00 sec)
  
  
  mysql> select * from y_emp limit 3;;
  +-------+------------+---------+--------+
  | ename | hiredate   | sal     | deptno |
  +-------+------------+---------+--------+
  | yws   | 2015-03-16 | 5000.00 |      2 |
  | yws   | 2015-03-16 | 5000.00 |      2 |
  | xy    | 2015-03-02 | 3000.00 |      2 |
  +-------+------------+---------+--------+
  3 rows in set (0.00 sec)
  
  
  ERROR:
  No query specified
  
  
  mysql>
  
  
  mysql> select * from y_emp order by sal limit 2; --配合order by 实现排序和分页
  +-------+------------+---------+--------+
  | ename | hiredate   | sal     | deptno |
  +-------+------------+---------+--------+
  | xy    | 2015-03-02 | 3000.00 |      2 |
  | yws   | 2015-03-16 | 5000.00 |      2 |
  +-------+------------+---------+--------+
  2 rows in set (0.03 sec)
  
  
  mysql> select * from y_emp order by sal desc limit 2;
  +-------+------------+---------+--------+
  | ename | hiredate   | sal     | deptno |
  +-------+------------+---------+--------+
  | yws   | 2015-03-16 | 5000.00 |      2 |
  | yws   | 2015-03-16 | 5000.00 |      2 |
  +-------+------------+---------+--------+
  2 rows in set (0.00 sec)
  
  
  mysql>
  
  
  mysql> create table dept (deptno int,ename varchar(20));
  Query OK, 0 rows affected (0.10 sec)
  
  
  
  mysql> insert into dept select deptno,ename from y_emp; --复制记录
  Query OK, 3 rows affected (0.06 sec)
  Records: 3  Duplicates: 0  Warnings: 0
  
  
  mysql> commit;
  Query OK, 0 rows affected (0.00 sec)
  
  union all和 union 写法
  mysql>select deptno from y_emp
  -> union all
  -> select deptno from dept;
  +--------+
  | deptno |
  +--------+
  |      2 |
  |      2 |
  |      2 |
  |      2 |
  |      2 |
  |      2 |
  +--------+
  6 rows in set (0.02 sec)
  
  
  mysql> select deptno from y_emp
  -> union
  -> select deptno from dept;
  +--------+
  | deptno |
  +--------+
  |      2 |
  +--------+
  1 row in set (0.00 sec)
  
  DCL授权
  mysql>
  mysql> grant select,insert on yws.* to 'scott'@'localhost'>
  Query OK, 0 rows affected (0.04 sec)
  
  
  mysql帮助文档可通过 help ?command方式 
  ?show
  mysql> ? data types;
  You asked for help about help category: "Data Types"
  For more information, type 'help ', where  is one of the following
  topics:
  AUTO_INCREMENT
  BIGINT
  BINARY
  BIT
  BLOB
  BLOB DATA TYPE
  BOOLEAN
  CHAR
  CHAR BYTE
  DATE
  DATETIME
  DEC
  DECIMAL
  DOUBLE
  DOUBLE PRECISION
  ENUM
  FLOAT
  INT
  INTEGER
  LONGBLOB
  LONGTEXT
  MEDIUMBLOB
  MEDIUMINT
  MEDIUMTEXT
  SET DATA TYPE
  SMALLINT
  TEXT
  TIME
  TIMESTAMP
  TINYBLOB
  TINYINT
  TINYTEXT
  VARBINARY
  VARCHAR
  YEAR DATA TYPE
  
  
  
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-154762-1-1.html 上篇帖子: Mysql 基础数据类型(无时间相关类型) 下篇帖子: MySQL 中 show full processlist 详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表