zoukankan      html  css  js  c++  java
  • 数据库进阶

    参考

    SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL)

    查询和更新指令构成了 SQL 的 DML 部分:

            SELECT - 从数据库表中获取数据
            UPDATE - 更新数据库表中的数据
            DELETE - 从数据库表中删除数据
            INSERT INTO - 向数据库表中插入数据
     

    SQL 的数据定义语言 (DDL) 能创建或删除表格,也可以定义索引(键),规定表之间的链接,以及施加表间的约束。

    SQL 中最重要的 DDL 语句:
            CREATE DATABASE - 创建新数据库
            ALTER DATABASE - 修改数据库
            CREATE TABLE - 创建新表
            ALTER TABLE - 变更(改变)数据库表
            DROP TABLE - 删除表
            CREATE INDEX - 创建索引(搜索键)
            DROP INDEX - 删除索引
     

    数据定义语言 (DDL)

    create database
    create database 数据库名

    create database my_db

    drop database
    drop database 数据库名

    drop database my_db

    create table
    create table 表名称(
    列名称 数据类型,
    列名称 数据类型,
    列名称 数据类型,
    ...
    )

    数据类型参考

    create table Persons (
        Id int not null,
        Name varchar(255) not null,
        Address varchar(255),
        Date Date
    )

    alter table

    表中添加列
    alter table 表名 add 列名 数据类型
    表中修改数据类型
    alter table 表名 alter column 列名 数据类型
    表中删除列
    alter table 表名 drop column 列名

    alter table Person add Birthday date
    alter table Person alter column Birthday year
    alter table Person drop column Birthday

    drop table

    drop table 表名称:用于删除表(表的结构、属性以及索引也会被删除)
    truncate table 表名称:仅仅删除表格中的数据

    
    

    create index
    可以在表中创建索引,以便更加快速高效地查询数据。用户无法看到索引,它们只能被用来加速搜索/查询。
    ps:更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。

    create index 索引名 on 表名 (列名)
    create unique index 索引名 on 表名 (列名)

    create index PersonIndex on Person (Name)
    create unique index PersonIndex on Person (Name)
    create index PersonIndex on Person (Name, Address)

    drop index

    alter table 表名 drop index 索引名

    alter table Person drop index PersonIndex

    约束 (Constraints)

    • not null
    • unique
    • primary key
    • foreign key
    • check
    • default

    not null
    not null 约束强制列不接受 null 值。

    unique

    nuique 约束唯一标记数据库表中的每条记录(目的不是为了提高访问速度,而只是为了避免数据出现重复)

    primary key

    primary key 约束唯一标记数据库表中的每条记录
    primary key 必须包含唯一的值, 且不能包含null值
    每个表都应该有且仅有一个primary key

    ps:unique 和 primary key 约束均为列或列集合提供了唯一性的保证。
    primary key 拥有自动定义的 unique 约束。
    每个表可以有多个 unique 约束,但是每个表只能有一个 primary key 约束。

    foreign key 
    一个表中的foreign key指向另一个表中的 primary key
    外键表示了两个表之间的联系

    check

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

    default

    default 约束用于向列中插入默认值。

    数据操作语言 (DML)

    select

    select 列名称 from 表名称

    select * from 表名称

    select LastName,FirstName from Persons
    select * from Persons

    select distinct

    select distinct 列名称 from 表名称

    select distinct * from 表名称

    select distinct LastName,FirstName from Persons
    select distinct * from Persons

    update

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

    update Person set Address = 'Zhongshan 23', City = 'Nanjing'
    where LastName = 'Wilson'

    insert into

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

    insert into Persons (LastName, Address) values ('Wilson', 'Champs-Elysees')

    delete

    delete from 表名称 where 列名称 = 某值

    delete * from 表名称

    delete form Person where LastName = 'Wilson' 
    delete * form Person

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

    select * from Persons where City='Beijing'

    and & or

    select * from Persons where (FirstName='Thomas' or FirstName='William') and LastName='Carter'

    order by
    order by 语句默认按照升序对记录进行排序。
    如果您希望按照降序对记录进行排序,可以使用 desc 关键字。

    select Company, OrderNumber from Orders order by Company, OrderNumber
    select Company, OrderNumber from Orders order by Company desc, OrderNumber asc

    sql注入

    在输入的字符串之中注入sql指令,并忽略了检查,那么这些注入进去的指令就会被数据库服务器误认为是正常的SQL指令而运行,因此数据库遭到破坏或是入侵。

    使用参数化查询来预防sql注入

    2015-05-19

  • 相关阅读:
    Linux的sz和rz命令
    python正则表达式(8)--分组、后向引用、前(后)向断言
    python正则表达式(7)--flag修饰符、match对象属性
    python正则表达式(6)--split、sub、escape方法
    python正则表达式(5)--findall、finditer方法
    python正则表达式(4)--search方法
    python正则表达式(3)--match方法
    python正则表达式(2)--编译正则表达式re.compile
    Go语言开发教程
    zabbix源码编译安装以及添加第一台host监控
  • 原文地址:https://www.cnblogs.com/whuyt/p/4514286.html
Copyright © 2011-2022 走看看