mysql普通使用

create

-- 創建表操作
--  創建News_user數據庫表
create table if not exists  News_user(
    id int not null primary key auto_increment,
    username varchar(50) not null unique,
    tel varchar(50),
    mail varchar(50),
    imgfile varchar(50) not null
);

-- 創建News_info數據庫表
create table if not exists News_info(
    id int not null primary key auto_increment,
    title varchar(100) not null,
    info longtext not null,
    Pubtime timestamp default CURRENT_TIMESTAMP not null,
    classify varchar(50) not null,
    mark int default 0 not null,
    check (mark=0 or mark=1)
);

-- 創建用戶與數據對應表News_relation
create table if not exists News_relation(
    id int not null primary key auto_increment,
    userid int not null,
    infoid int not null,
    mark int default 0 not null,
    foreign key (userid) references `news_user`(id),
    foreign key (infoid) references `news_info`(id),
    check (mark=0 or mark=1)
);

select

-- 單表查詢
select title,Pubtime from `news_info` where id<2;
-- 多表聯鎖查詢
select `news_user`.id,`news_user`.username,`news_info`.title from 
`news_user`,`news_info`,`news_relation` where
`news_user`.username='張三' and `news_user`.id=`news_relation`.userid and `news_relation`.infoid=`news_info`.id;

-- join多表查詢
select `news_user`.id,`news_user`.username,`news_info`.title from
`news_user` inner join `news_relation` on `news_user`.username='張三' and `news_user`.id=`news_relation`.userid
inner join `news_info` on `news_relation`.infoid=`news_info`.id;

-- 左連接left join
select `news_user`.id,`news_user`.username,`news_relation`.infoid from
`news_user` left join `news_relation` on `news_user`.username='張三' and `news_user`.id=`news_relation`.userid;

-- 右連接right join
select `news_user`.id,`news_user`.username,`news_relation`.infoid from
`news_user` right join `news_relation` on `news_user`.username='張三' and `news_user`.id=`news_relation`.userid;

-- distinct關鍵詞
select distinct title from `news_info`;

-- order by
select title,info from `news_info` order by id;
select title,info from `news_info` order by title desc;
select title,info from `news_info` order by title asc;
select title,info from `news_info` order by info asc,title desc;

-- limit
select title,info from `news_info` limit 5;

-- alias表名稱別名
select title as titlename from `news_info`;

-- 創建新表複製舊錶
create table news_infos(select *from `news_info`);
-- union命令 all允許重複
select title from `news_info` union all select info from `news_info`;

-- 導出數據 需要設置路徑
select *from `news_info` into outfile "xxx\out.txt"

-- group
select id,title,info from `news_info` group by title;
-- having
select id,title,info from `news_info` group by title having id>3;

insert

insert into `news_user` (username,tel,mail,imgfile) values("張三","12345678","[email protected]","img/q.jpg");
insert into `news_user` (username,imgfile) values("李四","img/b.jpg");
insert into `news_info`(title,info,classify) values("人工智能","例子等","高科技技術");
insert into `news_info`(title,info,Pubtime,classify,mark) values("百度廣告","推廣等",now(),"百度廣告",1);
insert into `news_info`(title,info,classify) values("算法分析","算法講解等","高科技技術");
insert into `news_info`(title,info,Pubtime,classify,mark) values("網易廣告","推廣等",now(),"網易廣告",1);
insert into `news_relation`(userid,infoid) values(1,1);
insert into `news_relation`(userid,infoid,mark) values(1,2,1);
insert into `news_relation`(userid,infoid,mark) values(2,2,1),(2,1,0);

-- 多數插入操作
insert into `news_info`(title,info,classify) values("算法詳解","算法認識等","高科技技術"),("大數據認識","大數據認識等","高科技技術");

alter

-- 聲明外鍵
alter table `news_relation` add foreign key (userid) references `news_user`(id);
-- 撤銷外鍵 news_relation_ibfk_1約束名稱
alter table `news_relation` drop foreign key 'news_relation_ibfk_1';
-- check約束
alter table `news_info` add check (mark=0 or mark =1);
-- 撤銷check
alter table `news_info` drop check (mark=0 or mark=1);
-- default約束
alter table `news_info` alter mark set default 0;
-- 撤銷default
alter table `news_info` alter mark drop default;
-- 增加新列
alter table `news_info` add test int;
-- 刪除列
alter table `news_info` drop column test;
-- 改變數據類型
alter table `news_info` modify column test varchar(50);

drop

drop tables `news_info`,`news_user`,`news_relation`;
drop database work1;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章