1 --数据库的操作
2
3 --链接数据库
4 mysql -uroot -p5 mysql -uroot -p密码6
7 --退出数据库
8 exit/q/ctrl+d9
10 --sql语句最后需要有分号;结尾
11 --显示数据库版本
12 selectversion();13
14 --显示时间
15 selectnow();16
17 --查看所有数据库
18 show databases;19
20 --创建数据库
21 --create database 数据库名 charset=utf8;
22 create database python04 charset=utf8;23
24 --查看创建数据库的语句
25 --show create database....
26 show create databasepython04;27
28 --查看当前使用的数据库
29 select database();30
31 --使用数据库
32 --use 数据库名;
33 usepython04;34
35 --删除数据库
36 --drop database 数据库名;
37 drop databasepython04;38
39
40 --数据表的操作
41
42
43 --查看当前所有的数据库表
44 show tables;45
46 --创建表
47 --auto_increment 表示自动增长
48 --not null 表示不能为空
49 --primary key 表示主键
50 --default 默认值
51 --create table 数据表名称(字段 类型 约束[,字段 类型 约束]);
52 --创建classes表(id, name)
53 create table`classes` (54 `id` int primary key not nullauto_increment,55 `name` varchar(30) not null
56 );57
58 --查看数据表结果
59 --desc数据库名称;
60 descclasses;61
62 --创建students表(id,name,age,high,gender,cls_id)
63 --unsigned表示无符号范围(正数)
64 create tablestudents(65 id int unsigned not null auto_increment primary key,66 name varchar(30) not null,67 age tinyintunsigned,68 high decimal(5,2),69 gender enum('男','女','保密') default '保密',70 cls_id intunsigned71 );72
73 --添加表数据
74 --insert into 表名 values(按创建时顺序分别插入数据);
75 insert into students values(0, '老王', 18, 185, '男', 0);76
77 --查询表数据
78 select * fromstudents;79
80 --查看表的创建语句
81 --show create table 表名;
82 show create tablestudents;83
84 --查看表结构
85 desc tablestudents;86
87 --修改表——添加字段
88 --alter table 表名 add 列名 类型;
89 alter table students add birthday datetime;90
91 --修改表——修改字段:不重命名版
92 --alter table 表名 modify 列名 类型及约束;
93 alter tablestudents modify birthday date;94
95 --修改表——修改字段:重命名版
96 --alter table 表名 change 原名 新名 类型及约束;
97 alter table students change birthday birth date default '2000-01-01';98
99 --修改表——删除字段
100 --alter table 表名 drop 列名;
101 alter table students drophigh;102
103 --删除表
104 --drop table 表名;
105 drop tablestudents;106
107
108 --增删改查
109
110 --增加
111 --全列插入
112 --insert [into] 表名 values(...)
113 --主键字段 可以用0 null default 来占位
114 --向classes表中插入 一个班级
115 insert into classes values(1, '菜鸟班')116
117 --向students表中插入 一个学生信息
118 insert into students values(null, '小乔', 18, '女', 0, '2002-01-01')119
120 --部分插入
121 --insert into 表名(列1,...) values(值1,...)
122 insert into students (name, age, gender) values ('张飞', 19, '男')123
124 --多行插入
125 insert into students (name, age, gender) values('李白', 20, '男'),('貂蝉', 18, '女')126
127 --修改
128 --update 表名 set 列1=值1,列2=值2...where 条件;
129 --不加条件修改全部
130 update students set age=20 where name='张飞'
131
132 --删除
133 --物理删除
134 --delete from 表名 where 条件;
135 --不加where条件将会清空表
136 delete from students where name='小乔';137
138 --逻辑删除
139 --用一个字段来表示,这条消息是否不能再使用了
140 --给students表中添加一个is_delete字段
141 --alter table students add is_delete bit default 0;
142 alter table students add is_delete bit default=0;143 --update students set is_delete=1 where id=6;
144 update students set is_delete=1 where id=6;145 --查询显示数据加上is_delete=0的条件
146
147 --查询基本使用
148 --查询所有列
149 --select * from 表名;
150 select * fromstudents;151
152 --指定条件查询
153 --select * from students where 条件;
154 select * from students where name='张飞';155 select * from students where id>6;156
157 --查询指定列
158 --select 列1,列2... from 表名;
159 select name,gender fromstudents;160
161 --可以使用as为列或表指定别名
162 --select 字段[as 别名],字段[as 别名] from 数据表 where ...;
163 select name as 姓名,gender as 性别 from students where id<9;164 select id as 序号,name as 姓名,gender as 性别 fromstudents;165
166 --字段的顺序
167 --select * from 表名 where 条件 order by 列 [ASC]或[DESC] ASC顺序,DESC倒序
168 select * from students where gender='女' order by id desc;169
170 --外键相关
171 --同步表A数据到表B,表B是表A的外键表
172 --表A中的班级的种类放到表B的name中
173 insert into classes (name) select cls_name from students group bycls_name;174
175 --将表A中班级信息修改为 表B中同一班级信息对应的id
176 update students as s inner join classes as c on s.cls_name=c.name set s.cls_name=c.id177
178 --修改表A中班级信息字段的结构 和表B中id字段的结构一样
179 alter table students change cls_name cls_id int not null;180
181 --关联外键用于设置后续表A中添加数据 对班级信息的约束
182 alter table students add foreign key (cls_id) referencesclasses (id);183
184 --创表时关联外键
185 create tablestudents(186 id int unsigned not null auto_increment primary key,187 name varchar(30) not null,188 age tinyintunsigned,189 high decimal(5,2),190 gender enum('男','女','保密') default '保密',191 cls_id intunsigned,192 foreign key(cls_id) referencesclasses(id),193 );194
195 --取消外键
196 --先获取外键约束名称,该名称系统会自动生成,可以通过查看表创建语句来获取名称
197 show create tablestudents;198 --获取名称之后就可以根据名称来删除外键约束
199 alter table students drop foreign key 外键名称;