Mysql常用DDL命令

Mysql常用命令:
--在Mysql中,语句的结尾要么使用;要么使用\g或者\G作为结束符。
进入Mysql
(---其中Your
MySQL connection id is 5表示到当前为止连接到Mysql数据库的次数,Server version: 5.5.37-log Source
distribution表示Mysql数据库的版本)
[[email protected] 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>

Mysql常用DDL命令,布布扣,bubuko.com

时间: 2024-08-02 02:45:32

Mysql常用DDL命令的相关文章

MySQL 常用管理命令

1.连接服务器登录 >mysql -h 192.168.0.11 -u root -p 2. 修改用户密码 >mysqladmin -u root -p 654321 password 123456 3.用户权限管理 1) grant on命令用于增加新用户并控制其权限. grant select,insert,update,delete on *.* to [[email protected]”%][email protected]”%[/email]” Identified by “abc

MySQL的常用操作更改root密码、连接MySQL、MySQL常用的命令

MySQL的常用操作更改root密码 连接MySQL MySQL常用的命令 原文地址:http://blog.51cto.com/13515599/2090001

设置更改root密码、连接MySQL、MySQL常用的命令

设置更改root密码 首先查看一下mysql有没有启动ps aux |grep mysql 如果没启动,就先启动mysql/etc/init.d/mysqld start 先将mysql目录加入环境变量中export PATH=$PATH:/usr/local/mysql/bin/ 再将命令写入到配置文件中,永久生效vim /etc/profile 在文件中添加如下的内容export PATH=$PATH:/usr/local/mysql/bin/ 设置mysql 密码mysqladmin -u

MySQL 常用基础命令

一.启动与关闭 1.1 Linux下启动mysql 的命令: a. rpm包安装:service mysqld start b. 源码包安装:/usr/local/mysql/bin/mysqld_safe --user=mysql & 1.2 Linux下重启mysql 的命令: a. rpm包安装:service mysqld restart b. 源码包安装: 先关闭mysql /usr/local/mysql/bin/mysqladmin -uroot -p shutdown 再启动my

MYSQL常用必备命令

mysql常用命令(必备) 1)导出test_db数据库 命令:mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u root -p test_db > test_db.sql 1.1)导出所有数据库 mysqldump -u root -p –all-databases > mysql_all.sql 2)导出一个表 命令:mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名 mysqldump -u root -p te

[基础学习]MySQL常用语句命令总结

前言 相信平时大家在开发时都会使用MySQL数据库,它是目前比较火的一款数据库工具,对于大多数企业的业务来说,MySQL可以很完美地支持了. 很多时候我们都是借助mysql可视化工具操作mysql,虽然说是比较方便,但是记住一些常用的命令还是有必要的. 我们来总结一下,一些平时比较常用的语句命令: 1.更改root密码 mysqladmin -uroot password 'new_password' 2.登录MySQL服务器 mysql -h120.0.0.1 -uroot -p*** -p3

Mysql DBA 高级运维学习笔记-Mysql常用基础命令实战

7.1 单实例mysql启动和关闭方法 (1)常规方法启动数据库 1.启动mysql服务命令 [[email protected] ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! 2.查看mysql端口 [[email protected] ~]# ss -lnt|grep 3306 LISTEN 0 50*:3306 *:* 3.查看mysql进程 会启动两个进程第一个就是mysql_safe第二个是mysqld [[email pr

Mysql 常用show命令

show tables或show tables from database_name或show database_name.tables; 解释:显示当前数据库中所有表的名称 show databases; 解释:显示mysql中所有数据库的名称 show processlist; 解释:显示系统中正在运行的所有进程,也就是当前正在执行的查询.大多数用户可以查看 他们自己的进程,但是如果他们拥有process权限,就可以查看所有人的进程,包括密码. kill xx; 杀死进程,xx 为进程id

Mysql常用show命令,show variables like xxx 详解,mysql运行时参数

MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 a. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称. b. show databases; -- 显示mysql中所有数据库的名称. c. show columns from table_n