DDL-数据库操作定义语言
DML-数据库操作语言
DCL-数据库控制语言
DQL-数据库查询语言
查询所有数据
SHOW DATABASES;
查询当前数据库
SELECT DATABASE();
CREATE DATABSE [IF NOT EXISTS] 数据库名 [ DEFAULT CHARTSET 字符集] [COLLATE
排序规则] ;
mysql> CREATE DATABASE IF NOT EXISTS test;
Query OK, 1 row affected (0.01 sec)
设置字符集编码,utf8是3个字节,utf8mb4是四个字节
mysql> CREATE DATABASE itheima default charset utf8mb4;
Query OK, 1 row affected (0.02 sec)
DROP DATABASE [IF EXISTS] 数据库名;
mysql> DROP DATABASE IF EXISTS test;
Query OK, 0 rows affected (0.01 sec)
USE 数据库名;
SHOW TABLES;
DESC 表名;
SHOW CREATE TABLE 表名;
CREATE TABLE 表名(
字段1 字段1类型[ COMMENT 字段1注释],
字段2 字段2类型[ COMMENT 字段2注释],
字段n 字段n类型[ COMMENT 字段n注释] )[ COMMENT 表注释];
注意:[…]为可选参数, 最后一个字段后面没有逗号
create table it_user(
id int comment 'id',
name varchar(50) comment '姓名',
age int comment '年龄',
gender varchar(1) comment '性别'
) comment '用户表';
数据类型
score double(4,1) 4个字节,1位小数
age TINYINT UNSIGNED 无符号范围
字符串类型
日期类型
字符串eg
CREATE TABLE `emp` (
`id` int(11) DEFAULT NULL COMMENT '编号',
`workno` varchar(10) DEFAULT NULL COMMENT '工号',
`name` varchar(10) DEFAULT NULL COMMENT '姓名',
`gender` char(1) DEFAULT NULL COMMENT '性别',
`age` tinyint(3) unsigned DEFAULT NULL COMMENT '年龄',
`idcard` char(18) DEFAULT NULL COMMENT '身份证',
`entrydate` date DEFAULT NULL COMMENT '入职时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='员工表'
ALTER TABLE 表名 ADD 字段名 类型(长度) [comment 注释] [约束];
ALTER TABLE 表名 MODIFY 字段名 新数据类型(长度);
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度) [COMMENT 注释] [约束];
ALTER TABLE 表名 DROP 字段名;
ALTER TABLE 表名 RENAME TO 新表名;
DROP TABLE [IF EXISTS] 表名;
TRUNCATE TABLE 表名;
mysql> alter table emp add nickname varchar(10) comment '昵称';
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table emp modify nickname varchar(15) ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| workno | varchar(10) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| idcard | char(18) | YES | | NULL | |
| entrydate | date | YES | | NULL | |
| nickname | varchar(15) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> alter table emp change nickname username varchar(10);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| workno | varchar(10) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| idcard | char(18) | YES | | NULL | |
| entrydate | date | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> alter table emp drop username;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc ep;
ERROR 1146 (42S02): Table 'itcast.ep' doesn't exist
mysql> desc emp;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| workno | varchar(10) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| idcard | char(18) | YES | | NULL | |
| entrydate | date | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> alter table emp rename to employee;
Query OK, 0 rows affected (0.02 sec)
mysql>
SHOW DATABASES;
CREATE DATABASE 数据库名;
USE 数据库名;
SELECT DATABASE();
DROP DATABASE 数据库名;
SHOW TABLES;
CREATE TABLE 表名(字段 字段类型,字段 字段类型);
DESC 表名;
SHOW CREATE TABLE 表名;
ALTER TABLE 表名 ADD/MODIFY/CHANGE/DROP/RENAME TO …;
DROP TABLE 表名;