安装
Ubuntu安装
https://www.cnblogs.com/wuhou/archive/2008/09/28/1301071.html
sudo apt-get install mysql-server
1. 登录
[ mysql -h 主机名 -u 用户名 -p ]
mysql -u root -p
Enter password:
# 若密码存在, 输入密码登录, 不存在则直接按回车登录
2. 创建数据库
[ create database 数据库名 [其他选项]; ]
mysql> create database samp_db character set gbk;
# 为了便于在命令提示符下显示中文, 在创建时通过 character set gbk 将数据库字符编码指定为 gbk。
# 创建成功时会得到 Query OK, 1 row affected(0.02 sec) 的响应。
# 可以使用 show databases; 命令查看已经创建了哪些数据库。
3. 创建数据表
mysql> create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
# "id" 为列的名称;
# "int" 指定该列的类型为 int(取值范围为 -8388608到8388607), 在后面我们又用 "unsigned" 加以修饰, 表示该类型为无符号型, 此时该列的取值范围为 0到16777215;
# "not null" 说明该列的值不能为空, 必须要填, 如果不指定该属性, 默认可为空;
# "auto_increment" 需在整数列中使用, 其作用是在插入数据时若该列为 NULL, MySQL将自动产生一个比现存值更大的唯一标识符值。在每张表中仅能有一个这样的值且所在列必须为索引列。
# "primary key" 表示该列是表的主键, 本列的值必须唯一, MySQL将自动索引该列。
# 下面的 char(8) 表示存储的字符长度为8, tinyint的取值范围为 -127到128, default 属性指定当该列值为空时的默认值
4. 插入数据
[ insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...); ]
mysql> insert students (name, sex, age) values("孙丽华", "女", 21);
mysql> insert into students values(NULL, "王刚", "男", 20, "13811371377");
# 查看结果
mysql> select * from students;
5. 更新表中的数据
[ update 表名称 set 列名称=新值 where 更新条件 ]
mysql> update students set tel=default where id=5;
6. 删除表中的数据
| delete from 表名称 where 删除条件; |
mysql> delete from students where id=2;
7. 创建后表的修改
7.1 添加列
| alter table 表名 add 列名 列数据类型 [after 插入位置];
# 在表的最后追加列 address
mysql> alter table students add address char(60);
7.2 修改列
| alter table 表名 change 列名称 列新名称 新数据类型;
# 将表 tel 列改名为 telphone:
mysql> alter table students change tel telphone char(13) default "-";
7.3 删除列
| alter table 表名 drop 列名称;
mysql> alter table students drop birthday;
7.4 重命名表
| alter table 表名 rename 新表名;
mysql> alter table students rename workmates;
7.5 删除整张表
| drop table 表名;
mysql> drop table workmates;
7.6 删除整个数据库
| drop database 数据库名;
mysql> drop database samp_db;
8. 外键的使用
参考http://www.cppblog.com/wolf/articles/69089.html
https://www.cnblogs.com/microtiger/p/7814177.html
8.1 外键语法
[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]
ON DELETE、ON UPDATE表示事件触发限制,可设参数:
① RESTRICT(限制外表中的外键改动,默认值)
② CASCADE(跟随外键改动)
③ SET NULL(设空值)
④ SET DEFAULT(设默认值)
⑤ NO ACTION(无动作,默认的)
8.2 示例
mysql> create table repo_table(
repo_id char(13) not null primary key,
repo_name char(14) not null)
type=innodb;
mysql> create table busi_table(
busi_id char(13) not null primary key,
busi_name char(13) not null,
repo_id char(13) not null,
foreign key(repo_id) references repo_table(repo_id))
type=innodb;
## 增加级联操作
mysql> alter table busi_table
add constraint id_check
foreign key(repo_id)
references repo_table(repo_id)
on delete cascade
on update cascade;
教程参考:
https://zhuanlan.zhihu.com/p/21677363
错误提示
1. # MySQL Cannot Add Foreign Key Constraint
:: [Mybe]: I had set one field as "Unsigned" and other one not. Once I set both columns to Unsigned it worked.










网友评论