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)
begin
set @i=0;
set @j=0;
repeat
set @i=@i+1;
set @j=@i+@j;
 until a=@i end
repeat;
 end $

 

 --能整除三 不能整除9

 

delimiter $
create procedure asdw(sss int)
begin
set @i=0;
set @cj=1;
repeat
set @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 表名(字段名)

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM