Mysql基础语法-DDL

一、数据库级操作

1、登录数据库:mysql -uroot -p

登录说明:

1)mysql表示客户端命令,“-u”后面紧跟着要连接的数据库名称,“-p”表示需要输入密码

2)命令的结束符用“;”,或者“\g”表示

  客户端的连接id,记录了mysql服务到目前为止的连接次数,每个连接会自动加1,当前为4.

  mysql服务器的版本是5.6.40,含“beta”表示测试版,含“standard”表示标准版

  通过“help;”或“\h”命令来显示帮助内容

  通过“\c”命令来清除命令行buffer

D:\Program Files\mysql-5.6.40-winx64\bin>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.40 MySQL Community Server (GPL)

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

2、创建数据库:create database dbname;

create database test1;

3、删除数据库:drop databse test1;

drop database test1;

4、查看系统中数据库用户:show databases

说明:

1)information_schema:主要存储了系统中的一些数据库对象信息,比如用户表信息、列信息、权限信息、字符集信息、分区信息等。

2)cluster:存储系统的集群信息

3)mysql:存系统的用户权限信息

4)test:系统自动创建的测试数据库,任何用户都可以使用

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)

5、选择使用的数据库:use dbname

mysql> use information_schema
Database changed
mysql> use test
Database changed
mysql>

6、查看数据库中的表show tables;

mysql> show tables;
Empty set (0.00 sec)

 

二、表级操作

1、创建表:create table tablename(col type ……)

示例:

create table t(
id1 FLOAT(5,2) DEFAULT NULL,
id2 DOUBLE(5,2) NOT NULL,
id3 DECIMAL(5,2),
id4 INT(5) ZEROFILL,
id5 bit(1),
id6 DATE,
id7 time,
id8 datetime
)

2、查看表定义:desc tablename

3、查看创建表的SQL语句:show create table tablename \g;

说明:“\G”选项的含义是使得记录能够按照字段竖向排列,以便更好地显示内容较长的记录。

mysql> show create table t\g;

| Table | Create Table

| t | CREATE TABLE `t` (
`id1` float(5,2) DEFAULT NULL,
`id2` double(5,2) NOT NULL,
`id3` decimal(5,2) DEFAULT NULL,
`id4` int(5) unsigned zerofill DEFAULT NULL,
`id5` bit(1) DEFAULT NULL,
`id6` date DEFAULT NULL,
`id7` time DEFAULT NULL,
`id8` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

4、查看数据库下所有表mysql> show tables;

5、删除表:drop table tablename;

mysql> drop table t;
Query OK, 0 rows affected (0.16 sec)

***********************************

字段修改:modify

字段增加:add

字段删除:drop

改字段:change

**********************************

6、修改表类型

alter table tablename MODIFY [column] column_definition[FIRST|AFTER col_name]

alter table emp modify ename varchar(20);

7、修改表字段增

alter table tablename ADD [column] column_definition[FIRST|AFTER col_name]

alter table emp add column age int(3);

8、修改表字段删

alter table tablename DROP [column]  col_name

alter table emp drop column age;

9、修改表字段改名

alter table tablename CHANGE [column] old_col_name column_definition

注:change和modify都可以修改表的定义,但是change后面需要写两次列名,不方便。但change的优点是可以修改列名称,modify则不能。

alter table emp change age age1 int(4);

10、修改表字段顺序(add/change/modify中配合first|after使用)

alter table emp modify age int(3) first;--放到最前面

alter table emp birth date after ename;--放到ename之后

11、更改表名

alter table tablename RENAME [TO] new_tablename

alter table emp rename emp1;

  

报错:[Err] 1044 - Access denied for user ''@'localhost' to database 'information_schema'

解决办法:https://blog.csdn.net/alincexiaohao/article/details/38708043

 

posted @ 2018-05-23 00:07  航松先生  阅读(329)  评论(0)    收藏  举报