博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mysql常用DDL命令
阅读量:6117 次
发布时间:2019-06-21

本文共 5087 字,大约阅读时间需要 16 分钟。

Mysql常用命令:

--在Mysql中,语句的结尾要么使用;要么使用\g或者\G作为结束符。
进入Mysql
(---其中Your MySQL connection id is 5表示到当前为止连接到Mysql数据库的次数,Server version: 5.5.37-log Source distribution表示Mysql数据库的版本)
[wh42@e3ddba11 data]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, 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>

1.创建数据库

mysql> create database wison;
Query OK, 1 row affected (0.00 sec)
2.显示目前Mysql中存在多少数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wison |
+--------------------+
4 rows in set (0.00 sec)

mysql>

3.选择到某个数据库,之后创建表
mysql> use wison
Database changed
mysql> create table test(id int,name varchar(20),address nchar(10),age int)\G
Query OK, 0 rows affected (0.11 sec)

mysql>

4.显示某个数据库中有多少表
mysql> use wison
Database changed
mysql> show tables;
+-----------------+
| Tables_in_wison |
+-----------------+
| test |
+-----------------+
1 row in set (0.00 sec)

mysql>

5.删除数据库
mysql> drop database wison;
Query OK, 1 row affected (0.06 sec)

mysql>

6.查看表结构
mysql> desc test;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | char(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql>

7.查看创建表的脚本
mysql> show create table test;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`address` char(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

8.删除表
mysql> drop table test;
Query OK, 0 rows affected (0.03 sec)

mysql>

9修改表结构
9.1修改列类型
mysql> alter table test
-> modify name varchar(10);
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

9.2添加新列
mysql> alter table test add column country char(3);
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

9.3删除列
mysql> alter table test drop column address;
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

9.4重命名列名
mysql> alter table test change age nianling int;
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

9.5更改列的顺序---该功能比较嗨---我们先看下当前表的结构
mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| nianling | int(11) | YES | | NULL | |
| country | char(3) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table test add birth date after name ;

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

mysql> desc test;

+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| birth | date | YES | | NULL | |
| nianling | int(11) | YES | | NULL | |
| country | char(3) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table test modify name int(4) first;

Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| name | int(4) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| birth | date | YES | | NULL | |
| nianling | int(11) | YES | | NULL | |
| country | char(3) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql>

10.重命名表
mysql> alter table test rename test_Table;
Query OK, 0 rows affected (0.04 sec)

mysql>

转载于:https://www.cnblogs.com/Wison-Ho/p/3699263.html

你可能感兴趣的文章
云时代架构阅读笔记之四
查看>>
WEB请求处理一:浏览器请求发起处理
查看>>
Lua学习笔记(8): 元表
查看>>
PHP经典算法题
查看>>
LeetCode 404 Sum of Left Leaves
查看>>
醋泡大蒜有什么功效
查看>>
hdu 5115(2014北京—dp)
查看>>
数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)...
查看>>
PHP读取日志里数据方法理解
查看>>
第五十七篇、AVAssetReader和AVAssetWrite 对视频进行编码
查看>>
Vivado增量式编译
查看>>
一个很好的幻灯片效果的jquery插件--kinMaxShow
查看>>
微信支付签名配置正确,但返回-1,调不出支付界面(有的手机能调起,有的不能)...
查看>>
第二周例行报告
查看>>
Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)...
查看>>
实验八 sqlite数据库操作
查看>>
四种简单的排序算法(转)
查看>>
Quartz2D之着色器使用初步
查看>>
多线程条件
查看>>
Git [remote rejected] xxxx->xxxx <no such ref>修复了推送分支的错误
查看>>