SQL语言包括四种主要程序设计语言类的语句,如:
数据定义语言DDL
数据操作语言DML
数据控制语言DQL
事务控制语言TCL
这里总结下DDL语言基础
1,数据库操作
show databases;查看所有的库
create databases+库名;创造指定名的库
use + 库名; 进入到指定库中
select database(); 查看当前所在库。注意末尾的小括号
drop database + 库名;删除指定库
2,表的操作
show tables; 查看所有表
create table + 表名(名字,类型,备注……);这里演示下:
mysql> create table biao(
-> id int unsigned comment '编号',
-> name varchar(10) comment '姓名',
-> gender char(1) comment '性别',
-> age tinyint unsigned comment '年龄'
-> )comment '信息表';
Query OK, 0 rows affected (0.03 sec)
注意最后一句不加逗号,备注可有可无。
desc + 表名; 查询该表,主要是看表中的名称类型等;形式如下:
mysql> desc biao;
+--------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+-------+
| id | int unsigned | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | tinyint unsigned | YES | | NULL | |
+--------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
show create table+ 表名字; 看创建表的具体语句
mysql> show create table biao;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| biao | CREATE TABLE `biao` (
`id` int unsigned DEFAULT NULL COMMENT '编号',
`name` varchar(10) DEFAULT NULL COMMENT '姓名',
`gender` char(1) DEFAULT NULL COMMENT '性别',
`age` tinyint unsigned DEFAULT NULL COMMENT '年龄'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='信息表' |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
alter table + 表名 + add/rename to/change/drop/modief + 语句名; 修改表中的内容
add表示在表中增添新的语句
mysql> alter table biao add workno tinyint unsigned comment '工龄';
Query OK, 0 rows affected (0.01 sec)
modief表示修改表中的语句的数据类型
mysql> alter table user modify id tinyint;
Query OK, 0 rows affected (0.05 sec)
rename to 对表进行改名
mysql> alter table biao rename to biao_new;
Query OK, 0 rows affected (0.01 sec)
change可以改变表中的语句
mysql> alter table biao_new change name name_new varchar(20) comment '昵称';
Query OK, 0 rows affected (0.01 sec)
drop表示删除表中的语句
mysql> alter table biao_new drop name_new ;
Query OK, 0 rows affected (0.01 sec)
modief对列进行修改
drop table + 表名; 删除该表