SQL基础

常见数据库

​ • Oracle Database : 甲骨文
​ • SQL Server : 微软
​ • DB2 : IBM
​ • PostgreSQL : 开源
​ • MySQL : 开源
​ • Access : 微软

数据库结构:

  1. 服务端:用于接收并处理其他程序发出的请求的程序,或者是安装此类程序的设备
  2. 客户端:向服务器发送请求的程序,或者是安装此类程序的设备
  3. 库:就是一堆表组成的数据集合
  4. 表:类似于Excel,有行和列组成的二维表
  5. 字段:字段就是表格的表头
  6. 记录:表里面的数据

SQL基础语法

增删改查

增:

CREATE DATABASE <库名>;     //增加数据库
create table table_name (column_name column_type);  //增加表

删:

DROP DATABASE  <库名>;      //删除数据库
drop table table_name; //删除表
alter table table_name drop i;    //删除表里的字段

查:

Show databases;     //查看所有数据库
Show tables;            //查看所有表
desc table_name //查看表的类型

改:

alter table ta change b bbb int; //修改字段名和属性
insert into 表名 (字段名1,字段名2,......) values (值1,值2,....);  //为表中所有字段插入数据
insert into 表名 [(字段名1,字段名2,...)] values (值 1,值 2,…),(值 1,值 2,…),...; //同时插入多条数据

数据类型:

• 整形
	Tinyint 	1字节	范围(-128~127)
	Smallint	2字节	范围(-32768~32767)
	Mediumint	3字节	范围(-8388608~2147483647)
	Int	4字节	范围(-2147483648~2147483647)
	Bigint	8字节	范围(+-9.22*10的18次方+-9,220,000,000,000,000,000
	
• 浮点型
	Float(m,d)	4字节	单精度浮点型,m总个数,d小数位
	Double(m,d)	8字节	双精度浮点型,m总个数,d小数位
	Decimal(m,d)		decimal是存储为字符串的浮点数

• 字符串型
	Char(n)	固定长度	最多255个字符
	Varchar(n)	可变长度	最多65535个字符,常用的变量string
	Tinytext	可变长度	最多255个字符,2^8-1,保存微型文本
	Text	可变长度	最多65535个字符,2^16-1,保存大文本
	Mediumtext	可变长度	最多2^24-1字符
	Longtext	可变长度	最多2^32-1个字符

mysql基础查询语句

select*from 表 order by 字段 [ASC(默认)/DESC];
    order by 排序
     	(select*from aaa order by aa;) //知道字段的情况
      	(select*from aaa order by 1;)  //不知道字段可以用数字代替
    limit n,m 分页
    	(select*from aaa limit n,m;)   //n表示从第几行开始,m表示取第几条
     	(select*from aaa limit 0,1;)
    like	模糊查询
    	(select*from aaa where like '%%';) //%代表任意字符(这能like用)
    	(select*from aaa where like '1';)
    运算符号:
    	+ - * / %
     	select 1+1; select 1-1; select 1*1; select 1/1; select 9%2;
    逻辑运算:
    	NOT(!)
     	AND(&)
      	OR(|)
           select*from aaa  where a='1' and b='1'; //and=和 必须满足a和b才能被查询
           select*from aaa where a='1' or b='1';   //or=或 满足其中一个就能被查询
           select*from aaa where not a='1';        //not=不等于    

联合查询

联合查询就是将两台语句查询的内容整合到一张表里
select*from table_name a=1 union select 1,1;       //无重复
select*from table_name a=1 union all select 1,1;   //有重复

子查询

子查询就是优先执行,然后执行其他语句。
    select*from user where username in (select username from admin);
    	//检查admin表和user表是否有用户名相等
posted @ 2022-07-02 14:53  eXN5  阅读(72)  评论(0)    收藏  举报