微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql学习笔记-基本语法-表结构创建

一、数据库连接

命令行工具 MysqL [-h 127.0.0.1][-p3306] -uroot -p xxxx

二、DLL语法

C:\Users>MysqL -uroot -p xxxx

MysqL> # 查询所有数据库
MysqL> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| itcast             |
| MysqL              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
7 rows in set (0.00 sec)

MysqL> # 查询当前数据库
MysqL> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

MysqL> # 创建数据库
MysqL> CREATE DATABASE if not exists test default charset utf8;
Query OK, 1 row affected, 1 warning (0.02 sec)

MysqL> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| itcast             |
| MysqL              |
| performance_schema |
| sakila             |
| sys                |
| test               |
| world              |
+--------------------+
8 rows in set (0.00 sec)

MysqL> # 删除数据库,if exists 如果存在
MysqL> drop database if exists test;
Query OK, 0 rows affected (0.01 sec)

MysqL> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| itcast             |
| MysqL              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
7 rows in set (0.00 sec)

MysqL> # 切换使用数据库use
MysqL> use itcast;
Database changed
MysqL> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| itcast     |
+------------+
1 row in set (0.00 sec)

# DDL 表创建

CREATE TABLE tb_uset(
	id int comment '编号',
	name varchar(50) comment '姓名',
	age int comment'年龄',
	gender varchar(1) comment '性别'
)comment '用户表';

MysqL> # 查询当前数据库所有表
MysqL> SHOW TABLES;
+------------------+
| Tables_in_itcast |
+------------------+
| tb_uset          |
+------------------+
1 row in set (0.00 sec)

MysqL> # 查询表结构
MysqL> DESC tb_uset;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | YES  |     | NULL    |       |
| name   | varchar(50) | YES  |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
| gender | varchar(1)  | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

MysqL> # 查询指定表的建表语句
MysqL> SHOW CREATE TABLE tb_uset;
+---------+------------------------------------------------------+
| Table   | Create Table      |
+---------+------------------------------------------------------+
| tb_uset | CREATE TABLE `tb_uset` (
  `id` int DEFAULT NULL COMMENT '编号',
  `name` varchar(50) DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  `gender` varchar(1) DEFAULT NULL COMMENT '性别'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表' |
+---------+------------------------------------------------------+
1 row in set (0.00 sec)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐