1.创建数据库
create database 数据库名称;
2.查看已经存在的数据库
show databases;
3.查看某个已经创建的数据库信息
show create database 数据库名称;
4.修改数据库的编码
alter database 数据库名称 default character set 编码方式;
5.删除数据库
drop database 数据库名称;
6.创建数据表
create table 表名
(
字段名1,数据类型[完整性约束条件],
字段名2,数据类型[完整性约束条件],
………..
字段名n,数据类型[完整性约束条件],
);
7.查看数据表
查看建表语句:show create table 表名;
查看表结构:describe 表名;
查看当前数据库中所有的表:show tables;
8.修改表名
alter table 旧表名 rename 新表名;
9.修改字段名
alter table 表名change 旧字段名 新字段名 新数据类型;
10.修改字段的数据类型
alter table 表名 modify 字段名 数据类型;
11.添加字段
alter table 表名 add 新字段名 数据类型;
12.删除字段
alter table 表名 drop 字段名;
13.修改字段的排列位置
alter table 表名 modify 字段名1 数据类型 first|after 字段名2;
14.删除数据表
drop table 表名;
约束条件 | 说明 |
primary key | 主键约束,用于唯一标识对应的记录 |
foreign key | 外键约束 |
not null | 非空约束 |
unique | 唯一性约束 |
default | 默认值约束,用于设置字段的默认值 |
15.单字段主键
字段名 数据类型 primary key;
16.多字段主键
primary key(字段名1,字段名2,….字段名n);
17.非空约束
字段名 数据类型 not null;
18.唯一约束
字段名 数据类型 unique;
19.默认约束
字段名 数据类型 default 默认值;
20.设置表的字段值自动增加
字段名 数据类型 auto _increment;
21.创建索引
create table 表名
(
字段名1 数据类型 [完整性约束条件],
字段名1 数据类型 [完整性约束条件],
.......
字段名n 数据类型 [完整性约束条件],
[unique]|fulltext|spatial] index|key [别名](字段名1 [(长度)] [asc|desc])
);
22.使用create index语句在已经存在的表上创建索引
create [unique]|fulltext|spatial] index 索引名 on 表名 (字段名 [长度] [ASC|DESC]);
23.alter table语句在已经存在的表上创建索引
alter table 表名 add [unique]|fulltext|spatial] index 索引名(字段名 [长度] [ASC|DESC]);
24.删除索引
alter table 表名 drop index 索引名;
drop index 索引名 on 表名;
25.添加数据(insert语句中指定所有字段名)
insert into 表名(字段名1,字段名2,…….) values (值1,值2);
26.使用select语句查看student表中的数据
select * from 表名;
27.insert语句中不指定字段名
insert into 表名 values(值1,值2,……);
28.为表的指定字段添加数据
insert into 表名(字段1,字段2,……) values(值1,值2,……);
29.insert语句为表中指定字段或者全部字段添加数据
insert into 表名 set 字段名1=值1,字段名2=值2,字段名n=值n;
30.同时添加多条记录
insert into 表名(字段名1,字段名2,……) values
(值1,值2,…),
(值1,值2,…),
(值1,值2,…);
31.update语句更新表中的数据
update 表名
set 字段名1=值1,字段名2=值2,…… [where 条件表达式];
32.select查看数据表中的数据
select * from 表名 where 条件表达式;
33.删除数据
delete from 表名 [where 条件表达式];