用户表: 表名:user
CREATE TABLE user (
username varchar(20) NOT NULL,
password varchar(20) NOT NULL,
access int DEFAULT '0', --默认值为0——>游客, 1——>普通用户, 2———>VIP, 3——>管理员root
PRIMARY KEY (username)
);
--按权限列出所有用户 select username,access from user order by access asc
--审核通过 update user set access='1' where username=''
--删除用户 delete from user where username=''
--会员注册 update user set access='2' where username=''
站内公告: 表名:message
CREATE TABLE message(
ID int PRIMARY KEY AUTO_INCREMENT, --公告编号:ID(主键)
Title varchar(50) not null primary key, --公告内容:Title
pDate timestamp NOT NULL DEFAULT NOW(), --发送时间:PDate
zd char(2) NOT NULL DEFAULT '1' --置顶:若为0则置顶
);
--列出所有公告 select Title,ID from message;
--添加 insert into message(title) values(内容')
--更新 update message set Title ='更改成什么' where ID=''
--置顶 select * from message where zd='0' union all select * from message where zd<>'0'
--删除 delete from message where ID=''
语言课堂: 表名:resource
CREATE TABLE resource(
ID int PRIMARY KEY AUTO_INCREMENT, --课程编号:ID(主键)
lesson varchar(50) NOT NULL, --课程名:lesson
content varchar(100) NOT NULL, --发布内容:content
pDate timestamp NOT NULL DEFAULT NOW(), --发布时间:pdate
visit int (100) NOT NULL DEFAULT '0',--访问量:visit
PID varchar(20), -- 发布人:PID(管理者/VIP)
foreign key (PID) references user(username) --(外键)
);
--打印课程 课程名+内容 select ID,lesson,content from resource
--上传课程:insert into resource(lesson,content,PID) values('C','上传内容','yhs')
--更改课程:update resource set content ='更改成什么' where ID=''
--删除课程: delete from resource where ID=''
--置顶课程
--按PID的的访问量 select content, pDate,visit from resource where PID='yhs' order by visit desc
--按lesson的访问量 select content from resource where lesson='JAVA' order by visit desc
--按time排序 select content from resource where PID='yhs' order by pDate desc
ALTER TABLE Resource ADD CONSTRAINT HEE FOREIGN KEY(PID) REFERENCES VIP(username)
--此语句可在建成的表中建立外键约束
视频资源: 表名:video
CREATE TABLE video(
ID int PRIMARY KEY AUTO_INCREMENT,
lesson varchar(50) NOT NULL,
content varchar(100) NOT NULL,
pDate timestamp NOT NULL DEFAULT NOW(),
visit int (100) NOT NULL DEFAULT '0',
PID varchar(20),
foreign key (PID) references user(username)
);
--显示视频类型和视频名 select lesson,content from video
留言评论: 表名:comment
CREATE TABLE comment(
ID int PRIMARY KEY AUTO_INCREMENT,--楼层ID
CID varchar(20), --留言者:CID
foreign key (CID) references user(username),--(外键)
posttime timestamp NOT NULL DEFAULT NOW(),----评论时间:posttime
message varchar(100) NOT NULL,--评论内容:message
Ispass int NOT NULL DEFAULT '0'--审核情况:Ispass
);--屏蔽 即 更改对应的评论字段
----按是否通过列出所评论 select CID,message,Ispass from comment order by Ispass asc
--评论审核通过 update comment set Ispass='1' where ID=''
--删除此评论 delete from comment where ID=''
--屏蔽该用户 update comment set message='此评论已被屏蔽' where CID='yhs'
--屏蔽此条留言(某层)update comment set message='此评论已被屏蔽' where ID=''
--留言 insert into comment(CID,message) values('yhs','留言内容')
--按时间排序select CID,posttime,message from comment where Ispass = 1 order by posttime desc
查询当前的库
show databases
查看某个库的表
use 库名
+show tables
查看表的格式(定义了什么)
desc 表名
查看建表源代码
show create 表名\G
查看表内数据
select* from 表名
删除表格
DROP TABLE 表名
(只删除数据)TRUNCATE TABLE 表名
修改数据
update 表名 set 列名=‘新值’ where ‘xx’
向表内插入数据
insert into 表名(,列名,)values(,‘数据‘,)
修改表列名
alter table 表名 change column 新名 旧名 类型
删除某一列:
alter table 表名 drop column 列名
增加一列:
alter table 表名 add column 列名 类型 '1'
按降序排序
select* from 表名 order by 列名 desc
按升序排序select* from 表名 order by 列名 asc
条件查询
select* from 表名 where 列名='xx'
条件查询+排序
select* from 表名 where 列名='xx' order by 排序名 desc/asc; --条件查询+排序
置顶
select* from 表名 where 列名='0' union all select * from 表名 where 列名<>'0'