02-mysql第二节

mysql(二)


数据准备

~~~python
create table students(
number int UNIQUE KEY auto_increment,
name varchar(20) UNIQUE KEY,
age int not null,
birth date not null
)auto_increment=201804001;

number 起始值为201804001

insert into students (name, age, birth) value
( ‘刘一’, 16, ‘2002-01-01’),
( ‘陈二’, 17, ‘2002-01-01’),
( ‘张三’, 18, ‘2002-01-01’),
( ‘李四’, 19, ‘2002-01-01’),
( ‘王五’, 20, ‘2002-01-01’),
( ‘赵六’, 21, ‘2002-01-01’),
( ‘孙七’, 22, ‘2002-01-01’),
( ‘周八’, 23, ‘2002-01-01’),
( ‘吴九’, 23, ‘2002-01-01’),
( ‘郑十’, 23, ‘2002-01-01’);

create table subjects(
number int(4) unsigned zerofill NOT NULL auto_increment,
title varchar(20),
duration int,
PRIMARY KEY (number)
);

zerofill,表示自动填0,和默认值为0差不多. unsigned,表示该字段存放一个无符号值,只存正数,不存负数。 故UNSIGNED ZEROFILL是无符号补零的意思。

insert into subjects (title, duration) value
(‘python基础’, 32),
(‘python进阶’, 16),
(‘web前端’, 16),
(‘python框架’, 32),
(‘python项目’, 32);

create table grades(
student_number int,
subject_number int(4) unsigned zerofill NOT NULL,
grade int not null
);

insert into grades values
(201804001, 0001, 90),
(201804002, 0001, 89),
(201804003, 0001, 88),
(201804004, 0001, 87),
(201804005, 0001, 86),
(201804006, 0001, 85),
(201804007, 0001, 84),
(201804008, 0001, 83),
(201804009, 0001, 82),
(201804010, 0001, 81),
(201804001, 0002, 80),
(201804002, 0002, 79),
(201804003, 0002, 78),
(201804004, 0002, 77),
(201804005, 0002, 76),
(201804006, 0002, 75),
(201804007, 0002, 74),
(201804008, 0002, 73),
(201804009, 0002, 72),
(201804010, 0002, 71);

~~~

约束条件

1.默认约束 (default)

  • 初始值设置,插入记录时,如果没有明确为字段赋值,则自动赋予默认值
create table tb(
    id int,
    name varchar(20),
    age int default 18
);

insert into tb (name) value('句号');
insert into tb values(1, '句号');     #报错
insert into tb values(1, '句号',17);
insert into tb values(1, '句号',17),(2,'a',16);
# 后续手动添加默认约束
alter table tb modify age int default 20;
alter table tb change age age int default 20;   #可以改名字

# 删除默认约束
alter table tb modify age int;

2.非空约束 (not null)

例子:
create table tb1(
id int,     
name varchar(20) not null  
);

insert into tb1 (id) value(1);      #name设置了非空约束,一定要添加值,
insert into tb1 (id, name) value(1, '');    #可以插入空字符

alter table tb1 modify id int not null; #手动添加
alter table tb1 modify id int;      #删除

3.唯一约束 (unique key)

  • 确保字段中的值的唯一
例子:
create table tb2(
id int unique key,  # 防止id相同
name varchar(20)
);

insert into tb2 value(1, '句号');
insert into tb2 value(2, '句号'); #多次插入报错
insert into tb2 value(NULL, '句号');#null值可以多次插入

4.自增长 (auto_increment)

  • auto_increment:自动编号,一般与主键组合使用。一个表里只有一个自增默认情况下,起始值为1,每次的增量为1。
create table tb3(
id int primary key auto_increment, 
name varchar(10)
)auto_increment 100;





#非空唯一的列才能添加自增长
create table tb3(
id double not null unique, 
name varchar(10) not null unique
)auto_increment 100;

alter table tb3 modify name  double  auto_increment;    #这个也能添加
desc tb3;

5.主键约束 (primary key)

  • 主键作用:可以唯一标识一条数据,每张表里只能有一个主键

  • 主键特性:非空且唯一,表没有主键时,第一个出现的非空且唯一

create table tb4(
id int not null unique auto_increment, 
name varchar(10)
)auto_increment 100;

desc tb4;            # 此时可以看到id的key变为pri,且Null下变为NO

#这个时候你如果要在给name添加主键是不可以的,要把id的主键删除才可以
alter table tb4   drop primary key;
alter table tb4 add primary key(name);

6.外键约束 (foreign key)

  • 保持数据的一致性、完整性,实现一对一或一对多关系。

  • 外键必须关联到键上面去,一般情况是,关联到另一张表的主键。

  • 因为一个表只在一类信息。用外键来做参照,保证数据的一致性,可以减少数据冗余。


# 表a
create table a(
a_id int primary key auto_increment,
a_name varchar(20) not null
);



# 表b
create table b(
b_id int primary key,
b_name varchar(20) not null,
constraint AB_foregin foreign key(b_id) references a(a_id)  # 外键约束定义
);
# 删除外键约束
alter table b  drop foreign key AB_foregin;
约束类型: 默认 非空 唯一 自增长 主键 外键
关键字: default not null unique key auto_increment primary key foreign

表结构修改

~~~python
create table tb1(
id int,
name char(4));

修改表结构,增加字段 年龄

alter table tb1 add age int first; #增加年级字段到第一列;
alter table tb1 drop age ; #删除年龄字段;
alter table tb1 add age int after id;#在id后面增加一个字段age,age在第个字段;

修改已经存在字段的类型

alter table tb1 modify age char(4) #把age字段修改成char类型;

修改列名(字段名)

alter table tb1 change age sex char(4); #将age字段修改为sex;

修改表名

alter table tb1 rename tb2 #将表tb1修改为tb2

修改字段的相对位置

ALTER TABLE test MODIFY name1 int first|after name2;
~~~

子查询

~~~python
select number from students where name=’张三’;

SELECT grade
FROM grades
WHERE student_number = (SELECT number FROM students WHERE name=’张三’);

内连接

SELECT student_number, title,grade
FROM subjects
join grades ON subjects.number=grades.subject_number;

两张表查询

select name, subject_number as number,grade
FROM students
JOIN grades ON students.number = grades.student_number;

最终结果

select name, title, grade
from students
join grades on students.number = grades.student_number
join subjects on grades.subject_number=subjects.number;

~~~


卸载

sudo apt-get remove mysql-*     #删除mysql
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 
                                        #删除残留数据,跳出一个对话框,选择yes

安装

apt update
apt install mysql-server    #中间会提示设置root 账户的密码

用户权限

show variables like '%char%';       #查看编码

select user,host from mysql.user;       #查看用户
show grants for 'juhao'@'%'             #查看用户权限
flush privileges;                   #刷新授权;

create user 'juhao'@'%' identified by '123456'; #创建用户
grant all on *.* to juhao@'%' identified by 'lujinze';      #给所有权限
revoke all on *.* from 'juhao'@'%';                         #移除权限

update user set host ='localhost' where user='juhao'    ;#修改远程权限

修改密码:
    set password for '用户名'@'登录地址'=password('密码')
    set password = password("newpassword");     #修改当前账号密码

删除用户:
    drop user 'test'@'localhost';

远程连接

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

bind 0.0.0.0    #修改监听IP


mysql -uroot -p

select user,host from mysql.user;       #查看用户

use mysql

update user set host ='%' where user='root; #给用户添加远程访问权限

select user,host from mysql.user;

quit

service mysql restart   #重启数据库 

ip:127.0.0.1
port3306
用户:root
密码:

猜你喜欢

转载自blog.csdn.net/qq_14993591/article/details/82459268
今日推荐