数据库简单语句

(关键字要大写)(小写也可以,但是大写显得我们更专业)
create database 数据库名 character set ‘gbk/utf8’;(创建一个数据库并且可以在数据库中插入中文)
修改字符集:
(语句之间不能有分号)
(语句结束后要加分号)
(#是mysql中的注释)
(结束错误语句用英文状态下的分号)
1.用管理员权限打开cmd
2.启用mysql(mysql -u root -p)
3.输入密码(root)
4.show databases;(展示所有数据库)
5.show tables;(展示所有表)
6.(1)show columns from 表名;
(2)desc 表名;
(3)describe 表名;
(查看表的大体框架)
7.select * from 表名;(查看表中的数据)(代表所有内容)(投影)
多列查询(把改为每列名字,中间用,隔开)
8.select 表中的一部分 from 表名;(查看表中的一部分内容)
9.数据库(增加、删除)
create /drop
10.创建/删除 数据库
create/drop database/schema 数据库名;
11.use 数据库名;(使用数据库)
12.show tables;(查看表)
13.drop table 表名;(删除表)
14.select distinct 字段 from 表名;(查看不重复的行)(字段必须指定哪一个)
15.select * from novel limit 1,2;(查看表的前两行)(限制输出第1到第2行)(从第一行开始,一共输出两行)
select * from novel limit 0,1;(限制输出第一行)(从第0行开始,一共输出一行)
select id,name from novel;(限制输出id和name两列)
16.select 表名.列名 from 数据库名.表名;()
17.select id,name from novel order by …(可用ID·或name或其它)…;(对id和name两列内容根据什么来顺序排序)
18.select id,name from novel order by …(可用ID·或name或其它)… desc;(对id和name两列内容根据什么来逆序排序)
19.按这个字段逆序排列,再限制输出第0行到第一行(如何输出某个字段的最大行)
20.select * from novel where id=40461;(查询ID=40461这一行的内容)
21.select * from novel where id(>,<,>=,<=,…)40461;(筛选表中的内容)
22.select * from novel where id between 1 and 20;(筛选出1到20的内容)
23.col(列);row(行)
24.alter table 旧表名 rename to 新表名;(修改表名)
25.alter table 表名 modify column 字段(列名) varchar(20);(修改字段类型)
26.alter table 表名 add 新增列名 字段类型 not null;(增加列/字段)
27.alter table 表名 change 旧列名 新列名 字段列名类型;(修改列名)
28.(前提:要有student表并且要有id和name两列)
insert into student(id,name) values (‘001’,‘小明’);(将数据(‘001’,‘小明’)插入student表(id,name)中)(插入一行数据)
insert into student(id,name) values(‘001’,‘小明’),(‘002’,‘小张’);
29. select * from xixi where id in(‘2’,‘3’);(查看表的id=2,3的行)
30. select * from novel where name like ‘三%’;(查询表中name这一列以三开头的行)(如果%再字的前面,则是前面差内容)
“%”可以代表若干个字符,但是用“_”只能代表一个字符
31.select * from novel where name like ‘%三%’;(查询表中name这一列含有‘三’的行)
32.进入information_schema数据库,通过select * from tables where TABLE_NAME like “novel”;语句查询之前创建过的表
33.查询表中一共有几条数据 select count(*) from 表名;(*表示属性名)
34. 查询表中id这一列的和 select sum(id) from 表名;(可以把sum改为avg和max,min)
35.查询表中id这一列的和 并把输出结果的名字暂时改为求和    select sum(id) as 求和 from 表名;
36.字段 date(日期 2018-10-25) time(时间 17:10:00) datetime(日期和时间 如果只插入了日期,则把时间补为00:00:00,但是只插入时间就会报错)
37.数据库的加减乘除(对两列的内容进行运算)
38.select 列名,sum(被统计的列名) from 表名 group by 列名;(统计相关求和数据)(把相同的数据拿出来统计在一起)

select col1+col2 from 表名;
select col1-col2 from 表名;
select col1*col2 from 表名;
select col1/col2 from 表名;
1
2
3
4
38.create database 数据库名 character set ‘gbk/utf8’;(创建一个数据库并且可以在数据库中插入中文)
修改字符集:
39.删除数据,删除单行数据
delete from 表名 where id=1;(删除id=1的这一行)
40. 多表查询(同时查询两个表)
select 表名.,表名. from 表名,表名 where 表名.列名=表名.列名;
41.(查询三张表的内容并用相同列名链接起来)(防止查询结果形成笛卡尔积)
select sc.,student.,course.* from sc,student,course where sc.sno=student.sno and sc.cno=course.cno;
(多表查询,每张表之间必须要有关系,如果没有关系,则要通过一张表把没有关系的两张表链接起来,)
42.(在group by中的限制条件只能用having 不能用where)
select cno,avg(grade) from sc group by cno having cno>1;
43.(自身连接 自己(表)和自己有关系)
select a.,b. from course a,course b where a.cno=b.cpno;
(条件写在on里面)(可以保留自己想要的数据)
44.(左外连接)(保证左边的表的内容全部都有,如果对应的内容在右边没有,则用null填补)
select student.,sc. from student left outer join sc on (student.sno=sc.sno);
45.(右外连接)(保证右边的表的内容全部都有,如果对应的内容在左边没有,则用null填补)
select student.,sc. from student right outer join sc on (student.sno=sc.sno);
(outer 和括号可以省略)
46.对三张表进行左链接(多表也可以参照此例)
select sname,cname,grade from (student left outer join sc on student.sno=sc.sno) left outer join course on sc.cno=course.cno;
47.(嵌套查询)(子查询)
select grade from sc where sno=(select sno from student where sage=20);
48.查询当前时间(select now();)
查询当前日期(select curdate()?
查询当前时间(select curtime()?
insert into 表名 values(now());(插入当前时间在表中)
49.select concat(sname,‘同学’) from student;(在字段sname的内容后加一个‘同学’)
50.不相关查询
select sno,cno from sc x where grade>(select avg(grade) from sc y where x.cno=y.cno);
51.view 视图create view 表名(IS_S1(字段一,字段二)) as select sname,sage from student;
更改视图字段名
删除视图:drop view 表名;
(select * from (select sname,sage from student) x;)相当于view代码
1.限制访问
2.将一些复杂的代码封装进去
create view 表名 as select sname,sage from student;
52.(修改单个字段内容)update student set sname=‘唐亮’ where sno=‘201215121’;
53.为某个表的某个字段创建索引
create index 索引名 on student(sno);
查询索引
show index from student;
删除索引
drop index 索引名;
54.show create table 表名;(查看表是如何创建的)
55.内模式:
innodb 插入数据,修改数据库比较快
myasim查询比较快
56. 插入数据后,id自增1(删除一条数据后,自增也会把其算在计数中)
create table b2(id int primary key auto_increment,time datetime);
primary key表示唯一,不能重复
foreign key
57. 默认sage为17
create table student1(sno char(11) primary key,sname char(11),sage int not null default 17);
58.使在插入数据时性别只能为男或女,其余不行,(但mysql不支持check)
create table student2(sno char(11),sname char(11),ssex char(2) check(ssex in(‘男’,‘女’)));
59.使在插入数据时性别只能为男或女,其余不行(mysql高版本可以)
create table student2(sno char(11),sname char(11),ssex char(2) check enum(‘男’,‘女’));
60.把结束符 ; 临时改为其他字符
delimiter 某一个字符;
61.存储过程的作用
(1)封装代码(提前用delimiter把结束符换为!)
create procedure 存储过程的名字(自己取)() begin select * from 表名; end!
(2)查询此过程(封装的代码):call 存储过程的名字()!
62. create procedure 存储过程的名字() begin declare 变量名 int;(定义一个变量)set 变量名=5;(为一个变量赋值)
if 变量名=5 then select now(); end if; end!
63.create procedure 存储过程的名字() begin declare 变量名 int;(定义一个变量)set 变量名=5;(为一个变量赋值)
while 变量名=5 do select now(); end while; end!
64.通过传入的参数值来控制循环

create procedure 存储过程的名字(in 变量名 int) begin … end;
call 存储过程的名字(变量值);
65.登录确认
create procedure 登录(in 用户 char(10),out 结果 int)
-> begin
-> select count(*)
-> into 结果
-> from student
-> where sname=用户;
-> end!
传参:    
call 登录(‘张立’,@i);
输出结果:
| @i |
66.查看创建的存储过程
show procedure status;
±-----+
| 1 |
66.show create procedure 存储过程的名字;
67.定义变量: set @i;
68.连接括号里的两个东西:select concat( , );
 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值