SQL不完全手册(一):概念和基础语句

版权声明:欢迎技术交流和帮助,提供IT相关服务,索要源码请联系博主QQ: 21497936,若该文为原创文章,未经允许不得转载 https://blog.csdn.net/qq21497936/article/details/80207610

原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/80207610


SQL不完全手册(一):概念和基础语句


目标人群

    系统学习数据库语法的人;

    当SQL语句查询手册的人;

    温习巩固数据库操作的人;

    其他人群。


概念

1.什么是SQL?

    SQL是结构化的查询语言,[因为能查询]使我们有能力访问数据库,[因为能访问数据库则可被计算机认识]所以使一种ANSI(美国标准化组织)的标准计算机语言。

2.SQL能做什么?

    [什么对象干什么]面向数据库执行查询;

    [对象如何来]创建新数据库,[数据库里什么对象]表;

    [干什么]增、删、改、查;

    [干的事有啥优势]创建存储过程,创建视图;

    [那些人可以干]可以设置表、存储和视图的权限。

3.RDBMS是什么?

    [按照英文解释]Relation,关系型数据库管理系统;

    [操作什么对象]数据,数据存在被称为表(table)的数据库对象中;

    [有什么用]REBMS是SQL的基础,同样也是所有现代数据库系统的基础,比如MY SQL SERVER, IBM DB2, ORACLE, MYSQL以及Microsoft Access。

4.数据库表

   [数据库由什么组成]一个数据库通常包含一个或多个表。[如何区别表]每个表有一个名字标识,[表里面有什么]表包含带有数据的记录(行),如下图:


上面的表包含4条记录和5列。

5.SQL语句

    [用来干什么]操作数据库,在数据库上执行大部分操作都有SQL语句完成;

    [很粗心]对大小写不敏感。

6.SQL服务器

    现代的SQL服务器构建在RDBMS之上。

DBMS-数据库管理系统

    一种可以访问数据库中数据的计算机程序,DMBS使我们有能力在数据库中提取、修改或者存储信息。

    不同的DBMS提供不同的函数供查询、提交以及修改数据。

RDBMS-关系数据库管理系统

    一种数据库管理系统,其数据库是根据数据间的关系来组织和访问数据的,RDBMS是SQL的基础,也是所有现代数据库系统诸如oracle/sqlserver/ibm db2/sbase/mysql/microsoft access的基础。


数据库操作方式

      操作数据库分为2个部分:[定义关键字]数据定义语言(DDL)和[如何使用关键则]数据操作语言(DML);

1.DDL(创建、修改数据库,创建、修改、删除表,创建、删除索引)

  • Create database : 创建数据库
  • Alter database : 修改数据库
  • Create table : 创建新表
  • Alter table : 变更(改变)数据库表
  • Drop table : 删除表
  • Create index : 创建索引(搜索键)
  • Drop index : 删除索引

2.DML(获取,更新,删除,插入)

  • Select :从数据库表中获取数据
  • Update :更新数据库中的数据
  • Delete :从数据库表中删除数据
  • Insert into :向数据库中插入数据


基础语句

1.创建数据库:create database

create database test

备注:如果使用sqlite数据库,直接命令行使用”sqlite3.exe [数据库]”即可创建。

2.使用/切换至指定数据库:use

  当前使用的数据库,不会是新建的数据库,切换当前使用的数据库使用use。

use test

3.删除数据库:drop database

drop database test

4.建表:create table

create table 表名称(
  列名称1 数据类型,
  列名称2 数据类型,
  列名称3 数据类型,
  …
)

create table student (
  num int, /*不能使用index列名,属于关键字*/
  namevarchar(255),
  agedecimal(2),
  birthdaydate
)

表1:SQL中最常用的数据类型

数据类型

描述

integer(size)

int(size)

smallint(size)

tinyint(size)

仅容纳整数,在括号内规定数字的最大位数

decimal(size,d)

numeric(size,d)

容纳带有小数的数字

“size”规定数字的最大位数,”d”规定小数点右侧的最大位数

char(size)

容纳固定长度的字符串(可容纳字母、数字以及特殊字符)

在括号中规定字符串的长度

varchar(size)

容纳可变长读的字符串(可容纳字母、数字以及特殊字符)

在括号中规定字符串的最大长度

date(yyyymmdd)

容纳日期

5.查询数据库所有表名和字段名

SQL 查询所有表名:

select name from sysObjects where type='u'
select * from information_schema.tables

查询表的所有字段名:

select * from syscolumns where ID=OBJECT_ID('student')
select * from information_schema.tables
select * from information_schema.views
select * from information_schema.columns


ORACLE 查看所有表名:

SELECT TABLE_NAME FROM USER_TABLES

ACCESS 查看所有表名:

SELECT NAME FROM MSYSOBJECTS WHERE TYPE=1 ANDFLAGS=0

    MSYSOBJECTS 是系统对象,默认情况是隐藏的。通过工具、选项、视图、显示、系统对象可以使之显示出来。

6.建表约束:NOT NULL,UNIQUE,PRIMARY KEY,FOREIGNKEY,CHECK,DEFAULT(建议回过头来看)

SQL NOT NULL

    约束强制列不接受NULL值,强制字段始终包含值,这意味着,如果不向字段添加值,就无法插入新纪录或者更新记录。

create table demo (
  id int not null,
  name varchar(255) not null
)


SQL UNIQUE

    unique约束唯一标识数据库表中的每条记录(可存在一个NULL值);

    unique和primary key约束均为列或者列集合提供了唯一性的保证;

    每个表可有多个unique,但只允许有一个primary key约束;

create table demoUnique (
  id int unique,
  name varchar(255)
)
insert into demoUnique values(1, '1');
insert into demoUnique values(2, '2');
insert into demoUnique values(1, '1');



SQL FOREIGN KEY

创建外键约束

一个表中的foreign key指向另一个表的primary key。

mysql语法

create table A (
  id int,
  idB,
  foreign key(idB) references B(id)
)

sqlserver/oracle/ms access

create table A(
  id int,
  idB int foreign key references B(id)
)

mysql/sql server/oracle/msaccess

create table A (
  id int not null,
  idB int,
  primary key (id),
  constraint BId foreign key(idB) references B(id)
)

增加外键约束

mysql/sql server/oracle/ms access

alter table A
add foreign key(idB) references B(id)

mysql/sql server/oracle/ms access

alter table A
add constraint BId foreign key(idB) references B(id)

撤销外键约束

mysql

alter table A
drop foregin key idB

sql server/oracle/ms access

alter table A
drop constraint BId

SQL PRIMARY KEY

    primarykey约束唯一标识数据库表中的每条记录;

    主键必须包含唯一的值,且不为null,并且每个表只能有一个主键;

    unique和primary key约束均为列或者列集合提供了唯一性的保证;

    每个表可有多个unique,但只允许有一个primary key约束;

单列约束语法,sql server/oracle/MS Access

create table demoPrimaryKey (
  id int primary key,
  name varchar(255)
)
insert into demoPrimaryKey values(1, '1');
insert into demoPrimaryKey values(2, '2');
insert into demoPrimaryKey values(1, '1');

单列约束语法,MySql

create table demoPrimaryKey (
  id int,
  name varchar(255),
  primary key(id)
)

多列约束语法,mysql/sql server/oracle/ms access

create table demoPrimaryKey (
  id int primary key,
  name varchar(255),
  constraint KeyId primary key( id, name);
)

增加单列约束(该列之前必须约束不能为null)

    如果您使用 ALTER TABLE 语句添加主键,必须把主键列声明为不包含 NULL 值(在表首次创建时)。

alter table student
add primary key(num)

增加多列约束(该列之前必须约束不能为null)

alter table student
add constraint keyId primary key (num, name)

撤销单列primary主键约束

alter table student
drop primary key

撤销多列primary主键约束

alter table student
drop constraint keyId

SQL CHECK

创建检查约束

    check约束用于限制列中的值得范围:

    如果对单个列定义check约束,那么改了只会允许特定的值;

    如果对一个表定义check约束,那么此约束会在特定的列中对值进行限制。

create table DemoCheck (
  id int not null,
  name varchar(255),
  check(id>100)
)


mysql

create table DemoCheck (
  id int not null,
  name varchar(255),
  check(id>100)
)

sqlserver/oracle/ms access

create table DemoCheck (
  id int not null check (id>100)
  name varchar(255)
)

如果需要命令check约束,以及为多个列定义check约束,如下:

mysql/sql server/oracle/ms access

create table DemoCheck (
  id int not null,
  name varchar(255),
  consraint chk check (id>100 AND id<1000 and name>’a’)
)

增加检查约束

mysql/sql server/oracle/ms access

alter table DemoCheck
add check (id>200)

mysql/sql server/oracle/ms access

alter table DemoCheck
add constraint chk check(id >200 and id <800)

撤销check约束

sql server/oracle/ms access

alter table DemoCheck
Drop constraint chk

mysql

alter table DemoCheck
drop check chk

SQL DEFAULT

    约束在列中插入默认值

创建默认值约束

create table demoDefault (
  id int not null,
  name varchar(255) default ‘me’
)

增加默认值约束

mysql

alter table demoDefault
alter name set defualt ‘me’

sql server/oracle/ms access

alter table demoDefault
alter column name set default ‘me’

撤销默认值约束

alter table demoDefault
alter name drop default

7.删除表:drop table

drop table student

8.插入数据:insert into

insert into 表名称 values(值1,值2,…)

insert into table_name(列1,列2,…)values (值1,只2)

9.查询数据:select

Select 列名称 from 表名称

或                                                                         

Select * from 表名称

10.过滤掉重复值的查询:select distinct

select distinct 列名称 from 表名称

11.单条件查询:select where

select 列名称 from 表名称 where 列 运算符 值

表2:在select where居中使用的运算符

操作符

描述

=

等于

<>(在某些sql中,可写成!=)

不等于

大于

小于

>=

大于等于

<=

小于等于

BETWEEN

在某个范围内

LIKE

搜索某种模式

12.多条件查询:select where and & or

    and和or可在where子语句中把两个或多个条件结合起来

13.对查询结果进行排序:order by ASC(默认升序) DESC(降序)

14.更新数据:update

update 表名称 set 列名称 = 新值 where 列名称 = 某值

15.删除数据:delete from 和 truncate table

delete from 表名称 where 列名称 = 值

truncate table 表名


《SQL不完全手册(二):高级语句》https://blog.csdn.net/qq21497936/article/details/80242112

猜你喜欢

转载自blog.csdn.net/qq21497936/article/details/80207610