2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > mysql常用命令添加外键主键约束存储过程索引

mysql常用命令添加外键主键约束存储过程索引

时间:2022-09-26 11:58:48

相关推荐

mysql常用命令添加外键主键约束存储过程索引

mysql常用命令添加外键主键约束存储过程索引

数据库连接

mysql -u root -p123456

查看表

show databases

创建数据库设置编码

create table books character set utf8;

创建用户

-- 特别需要注意,在 MySQL 中,账号由两部分组成:-- 1. user-- 2. host-- 即使 user 相同,只要 host 不同,也会被认为是不同账号。-- 这样可以非常方便对来自不同 ip 地址的访问进行精细的权限控制。 -- 默认情况下,创建的用户 host 为 '%',这是一个匹配符,跟模糊查询里的意思一样,表示匹配所有

create user [用户名] identified by '[密码]';

create user vip identified by 'vippp'; -- 所有连接 create user vip@'127.0.0.1' identified by 'xxx'; -- 本地连接 create user vip@'192.168.%' identified by 'yyy'; -- 192.168 网段的连接

修改密码

set passwor from '用户名' @host=password('新密码');

update mysql.user set password=password('新密码') where user='用户名' and host='%';

删除用户

drop user 用户名;

delete from mysql.user where user='用户名' and host='%'

给权限

grant all on *.* to vip@'127.0.0.1'; --将所有数据库上的所有权利都授予通过本机连接的VIP用户;

grant all privileges on books.* to vip@'%'; --将数据库books上的说有权利都授予所有连接的vip用户;

grant select on books.users to vip@'%'; --将books数据库上的users表的访问权限开发给vip用户;

grant all on *.* to vip@'%' with grant potions; --witgrant potionss的意思是可以给vip给予权限给别的用户

查看权限

show grants for vip@'%';

进行冲刷

flush privileges

查看当前用户

select user() ,current_user();

创建表

create table 表名

判断存在就删除然后创建

creata table if exists 表名

drop table if exists 表名

创建临时表

create temporary table 表名

mysql自增长

auto_increment

添加外键约束

alter table 表名 add constraint fk_引用id foreign key(引用id) references 被引用表名 (被引用id)

添加主键约束

alter table 表名 add constraint pk_id primary key (id);

删除约束

alter table 表名 drop forign key fk_引用id

添加表的字段

alter table 表名 add 字段名 类型 ;

修改表中的字段为空

alter table 表名modify 字段名类型 null

修改表中的字段不为空

alter table 表名modify 字段名类型 not null

添加表的字段自增主键

alter table 表名add column 字段名类型auto_increment not null, add primary key(cid);

删除表的字段

alter table 表名 drop column 字段;

删除表的主键

alter table 表名 drop primary key;

创建存储过程 mysql存储100相加的和

create procedure sum(a int)beginset @i=0;set @j=0;repeatset @i=@i+1;set @j=@i+@j;until a=@i endrepeat;end $

--能整除三 不能整除9

delimiter $create procedure asdw(sss int)beginset @i=0;set @cj=1;repeatset @i=@i+1;if @i%3=0 && @i%9!=0 then set @cj=@cj*@i;end if;until @i=sss end repeat;end

创建索引

CREATE INDEX 索引名字 ON 表名(字段名)

posted @ -09-07 16:36 韦邦杠 阅读(...) 评论(...) 编辑 收藏

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。