约束条件和用户管理
约束条件
主键约束:primary key
用于标识表中的主键列的值,而且这个值是全表当中唯一的,而且值不能为null。
一个表只能有一个主键
写法一: create table stu01 ( id int (5), name char (10), score decimal (5,2), address varchar (128), primary key (id) ); 写法二:create table stu01 ( id int (5) primary key, name char (10), score decimal (5,2), address varchar (128) );
外键:
用来建立表与表之间的关系。确保外键中的值与另一个表的主键值匹配。保证数据引用的完整性。
多表联查,不要超过三张。超过三张表,会降低查询效率。
create table if not exists student ( crad_id int (5) primary key auto_increment, stu_name char (10) not null, stu_email varchar (128) not null unique key ); create table class ( stu_id int(11) zerofill primary key auto_increment, address varchar(128) default '地址不详', crad_id int(5) not null, foreign key (crad_id)references student (crad_id) ); insert into student values (342423,'wbl','1234@qq.com'); insert into class values (01,'南京','342423');
删除外键
查找到索引进行删除 mysql> show create table class; | class | CREATE TABLE `class` ( `stu_id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT, `address` varchar(128) DEFAULT '地址不详', `crad_id` int NOT NULL, PRIMARY KEY (`stu_id`), KEY `crad_id` (`crad_id`), CONSTRAINT `class_ibfk_1` FOREIGN KEY (`crad_id`) REFERENCES `student` (`crad_id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb3 | alter table class drop foreign key class_ibfk_1;
外键就是和主表关联的列,不需要设置为从表的主键,但是不能为空,必须和主表的数据类型保持一致,
外键的值和主键的值要相同
先插入主表的数据,再插入从表的数据
删除表的外键不是直接删除外键的列名,二十删除外键的索引,show create table 表名,查看表信息
删除主键不需要加上主键名,如果有extra的额外属性,比如说自增长,要先移除属性在删除
删除主键的方式:alter进行修改
非空约束:
非空约束:保证列中的值不含null值
唯一性约束:
确保列中的所有值都是唯一的,类似主键,但是一个表可以有多个唯一约束。
create table stu02 ( id int (5) primary key, name char (10) not null unique key, score decimal (5,2), address varchar (128) );
自增约束:
在列生成的每一行都会自动生成一个唯一标识符,通常和主键一起使用,每次插入新的,自增列的值会自动增加
扩展语句:
create table if not exists test01 ( #在建表时自动检测表是否已经存在,如果不存在就创建。 id int (5) zerofill primary key auto_increment, #zerofill,5位,不足5位会自动补充0。auto_increment自增长字段, #每条记录自增1,自增长必须是主键,也不可以重复,如果添加失败 #吓一跳也会自动增长 name char (10), score decimal (5,2), address varchar (128) ); insert into test01 values (null,'灿弟',90,'南京');
insert插入数据时,时间类型
date
datetime
timestamp
create table stu02 ( id int (5) primary key, name char (10), date_time date not null, date_times datetime ); insert into stu02 values(1,2,'2023-7-16','2024-7-16 14:30:00');
复制表
先复制 create table stu_01 like student; 再导入内容 insert into stu_01 select * from student;
直接复制表和内容 create table stu_02 (select * from student);
临时表
create temporary table class1 ( stu_id int(11) zerofill primary key auto_increment, address varchar(128) default '地址不详', crad_id int(5) not null );
面试题
清空表
清空表的数据保留表的结构*
表结构还在数据被清空 drop删除 delete from 表名; #是一行一行的清空表的数据,速度慢,如果有自增长字段,delete清空之后会继续按照原来的序号将继续递增。 truncate table 表名; #清空表,保留表结构,但是清空之后原有的记录全部抹去,自增长也将从头开始,速度快
数据库的用户管理
创建用户
设置用户权限
取消权限
#root都是相同的,host:可以登录的主机,localhost指的是本机登录 #%:任意主机(ip地址) #权限上:localhost>%的权限 #root@localhost 安装完mysql之后就有了,不需要额外设置,其他都要人工创建,其他用户不设置成localhost 创建用户 create user 'wbl'@'192.168.65.12'; 设置密码 grant all privileges on *.* to 'wbl'@'192.168.65.12' identified by '123456' all:给予所有权限,远程登录,数据库的操作权限(增删改查) *.* *表示库 *库里的表 identified by '123456' 创建的密码
informatipn_schema:这个库的作用包含了mysq1服务器中所有其他数据库,表,列。索引权限等详细的元数据的信息可以查询数据库的结构和元信息。
performance_schema:包含mysql的服务性能和资源利用情况。查询语句的执行时间和锁定等信息。
删除权限 revoke all privileges on *.* from 'wbl'@'192.168.65.12'; 刷新权限 hlush privileges
赋予权限 mysql> grant insert,update,alter,delete on xy102.* to 'cq'@'192.168.65.12'; 删除权限 mysql> revoke insert,update,alter,delete on xy102.* to 'cq'@'192.168.65.12'; 删除用户 mysql> drop user'wbl'@'192.168.65.12';
创建用户和密码
mysql> create user 'wbl'@'192.168.65.12' identified by '123456';
查看权限
mysql> show grants for 'root'@'%';
刷新权限
刷新权限 hlush privileges
实验
增删改查日测题(上机实操) 1、创建库,库名 country
2、创建表,表名 province和city 要求如下:
1)、province包含字段: ct_id为区号,最多四位 主键; ct_name 城市名 最长5位,不能为空,不能重复 ct_scale 城市规模,可重复,不能为空。
2)、city包含字段 c_id 主键 c_street 不能为空,不能重复。 c_subway int 不能重复 可以为空 c_weather 字符串 可以空 默认值:晴
通过修改表city,创建外键关联province的主键,ct_id。
分别插入5条数据,任选。不限制
展示表1的前两行,展示表2的2-4行
有重复数据,去重查询。
修改表city,将c_weather的默认值改为:不详
create table province ( ct_id varchar(4) primary key, ct_name char (5) not null unique key, ct_scale varchar (255) not null ); select * from province; desc province; create table city1 ( c_id int (5), c_street char (10) not null unique key, c_subway int (10) unique key, c_weather varchar (5) default '晴', ct_id varchar(4) primary key, foreign key (ct_id) references province (ct_id) ); desc city1; select * from city1; insert into province values (1,'江苏','big'); insert into city values (2,'科建路',22,'晴'); #展示前两行 select * from province limit 0,2; #展示2-4行 select * from city limit 1,3; #修改c_weather alter table city1 modify c_weather varchar (5) default '不详';
1