京津冀地区某机构内参,四月不能错过的MySQL数据定义语言

文章目录

简单DCL

-- 创建用户
create user 'xx'@'%' identified by 'password';
-- 赋权
grant all privileges on 数据库.表名 to 'xx'@'%' identified by 'password';
-- 刷新权限
flush privileges;
-- 创建数据库
create database day_02;
-- 使用day_02数据库
use day_02;
-- 创建t_user表
create table t_user(
    `username` varchar(20),
    `password` varchar(20)
)default charset = utf8;
-- 加字段注释
create table xx(
   id int comment '编号';
)
-- 插入数据
-- insert into 表名 (列,列...) values (值,值...)
insert into t_user(username,password) values ('admin','root');
insert into t_user(username) values('test1');
insert into t_user(username,password) values('test','2'),('test','3');
(格式必须严格一致)错误写法:insert into t_user values('test');
-- 删除数据
delete from t_user;
-- 删除时为了避免误删 最好加上where语句进行限定
delete from t_user where username = 'test';
-- 更新数据
update t_user set password = '123';
-- 更新时不加where限定会全部更改
update t_user set password = '123' where username = 'test1';
-- 先执行表限定,然后执行行限定,最后执行列限定
-- select 列限定 from 表限定 where 行限定
-- 表限定:查询那张表
-- 行限定:查询那些符合条件的数据,不加where是全表查询
-- 列限定:要查看的结果值,如果是所有列,就写*,多个列用逗号隔开
-- 查询所有用户信息
select * from t_user;
-- 查询表中用户名为admin的用户密码
select password from t_user where username = 'damin';
-- 查询表中用户名为admin的用户名和用户密码
select password,username from t_user where username = 'admin';
-- 撤权 授权和撤权的时候可以不叫identified by 'password'
revoke all privileges on 数据库.表名 from 'xx'@'%' identified by 'password';
-- 删除表
drop t_user;
-- 删除数据库
drop day_o2;
-- 删除用户
drop user 'xx'@'%';
-- 避免数据库已经有同名表已经存在不执行后续SQL语句
create table if not exists user_info(id int);
-- 避免删除不存在表报错
drop table if exists xx;
简单的DDL
-- 更改表名
alter table 表名 rename 新表名;
-- 更改字段名 (列名)
alter table 表名 change 列名 新表名 数据类型;
-- 添加字段 默认尾部添加
alter table user_info add nickname varchar(20);
-- 首部添加
alter table user_info add sex char(1) first;
-- 指定已有列后面
alter table user_info add id int after name;
-- 删除列
alter table user_ifo drop sex;
-- 更改字段类型
alter table user_info modify id varchar(10);
-- 更改字段注释
alter table user_info modify nickname varchar(10) comment '昵称';

增强版DDL

上面的DDL只是一系列的基本操作,让我们有库有表可以插入数据,但是对于插入的数据是否有效,并不能保证,比如我们能够插入毫无用处的数据,这种记录会浪费存储空间,为了避免类似情况,MySQL提供了一系列完整性验证机制;
约束类型
主键 外键 唯一 非空 自增 默认值 关键字
primary key
foreign key
unique
not null
auto_increment
default
在Java中我们使用equals()方法和hashCode方法来判断对象是否相等,那么怎么在数据库表述数据的唯一性?
主键通常用于唯一确定表中一条记录,设置主键的字段不能为NULL,而且不能重复;
主键可以设置在一个字段,也可以在多个字段,基本都是一个字段;
主键的设置可以分为两种:
创建表语句的时候,添加主键约束:

create table person(
    id int primary key,
    name varchar(20)
);
create table person(
    id int,
    name varchar(100),
    income decimal(18,2),
    primary key(id,name)
);
create table person(
    id int,
    name varchar(20),
    primary key(id,name)
);
-- 错误写法
create table person(
    id int primary key,
    name varchar(20) primary key
);

创建表完成后,通过alter添加主键约束:

create table person(
    id int,
    name varchar(20)
);
alter table person add primary key(id,name);

如果主键的值用户输入,很可能会导致输入一致不成功,于是自增出现,自增的字段必须是有主键约束的;

create table test_05(
    id int auto_increment,
    name varchar(20),
    primary key
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微-程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值