ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

数据库的一些简单语句的应用(这是我的一些笔记,分享给大家,希望能够帮助大家)

2020-03-13 09:03:08  阅读:167  来源: 互联网

标签:语句 大家 数据库 student 表名 sc where id select


(关键字要大写)(小写也可以,但是大写显得我们更专业)
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 表名;
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( , );

标签:语句,大家,数据库,student,表名,sc,where,id,select
来源: https://www.cnblogs.com/Liang-Tsai/p/12484326.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有