• mysqldb


    http://www.cnblogs.com/Xjng/p/3437694.html

    http://www.codexiu.cn/python/blog/2729/

    ===================================

    http://www.jianshu.com/p/118e1c41e9f0

    http://www.jianshu.com/p/1d09d14976d7

    http://www.cnblogs.com/

    http://www.cnblogs.com/

    方式1:在创建表时,在外键字段之后使用foreign key(外键字段) references 主表(主键)
    方式2:在创建表之后,使用修改表结构的语法
    Alert table 从表名 add foreign key(从表中被主表约束的字段) references 主表(主键字段)
    查看外键名
    Show create table 表名
    删除外键
    外键不能修改只能删除后,重新创建。
    语法:
    Alter table 表名 drop foreign key 外键名


    1添加表字段
    alter table table1 add transactor varchar(10) not Null;
    alter table table1 add id int unsigned not Null auto_increment primary key
    2.修改某个表的字段类型及指定为空或非空
    >alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];
    >alter table 表名称 modify 字段名称 字段类型 [是否允许非空];
    >alter table 表名称 modify 字段名称 字段类型 [是否允许非空];

    这种理解错误的根源在于没有搞清楚外键两边谁是引用,谁是实体,也没有搞清楚级联删除的意义在于删除实体的时候级联的删除其所有引用的问题。

    级联操作只适用于主表(或被引用表)的删除(delete)和更新(Update)操作,
    不适用于从表(或引用表)的插入(Insert)操作,因此做插入操作只能是先插入主表(或被引用表),然后插入从表(引用表)。

    1.数据库操作
    //创建数据库
    create database h_test;
    create database h_test character set utf8;
    //查看数据库
    show databases;
    //查看数据库信息
    show create database h_test;
    //修改数据库的编码,可使用上一条语句查看是否修改成功
    alter database h_test default character set utf8;
    //删除数据库
    drop database h_test;

    2.表操作
    create table student( id int auto_increment primary key, name varchar(20), age tinyint ) engine=innodb default charset=utf8;

    //查看数据表
    show tables;
    //查看数据表信息,后面加上参数/G可使结果更加美观
    show create table student;
    //查看表的的字段信息
    desc student;
    //删除数据表
    drop table student;

    3.插入
    insert into student(id,name,age) values(1,'sb', 108);

    insert into student values(3,'2b', 8);
    insert into student set name='kk',age=29;
    insert into student values
    (4, 'xiaoming', 25),
    (5, 'xiaohong', 24);

    4.更新
    update student set name='kk' where id = 6;

    5.删除
    delete from student where id > 4;
    delete from student; #全部删除

    SELECT [ALL|DISTINCT] <目标列表达式>[,<目标列表达式>]…
    FROM <表名或视图名>[,<表名或视图名>]… [IN externaldatabase]
    [WHERE <条件表达式>]
    [GROUP BY <列名> [HAVING <条件表达式>]]
    [ORDER BY <列名> [ASC|DESC]…]

    SQL查询语句的顺序:SELECT、FROM、WHERE、GROUP BY、HAVING、ORDER BY。SELECT、FROM是必须的,HAVING子句只能与GROUP BY搭配使用。


    select * from student where id in(2,3,4);

    select * from student where id between 2 and 5;

    % 0个或多个字符
    _ 一个字符

    [charlist] 任一字符
    [^charlist] 不是任一字符

    select * from student where name like "%b" and age between 5 and 99;


    select * from student where name like "%b" and age between 5 and 99 order by name;

    select * from student limit 5;

    union 合并查询结果 删除重复
    all 不删除重复

    join ... on
    从左表读出一条,选出所有与on匹配的右表纪录(n条)进行连接


    select * from salaries where
    salaries.emp_no in
    (select employees.emp_no from employees
    where employees.first_name="Mary" and employees.last_name="Sluis");


    select * from dept_emp join departments
    on dept_emp.dept_no=departments.dept_no
    where ere dept_emp.emp_no in
    (select employees.emp_no from employees where
    employees.first_name=="Mary" and employees.last_name="Sluis");

    约束:

    primary key foreign key not null unique default


    -- 主键:

    alter table 表名

    add constraint PK_字段名--'PK_字段名'就为约束名

    primary key (字段名)

    --唯一约束:

    alter table 表名

    add constraint UQ_字段名

    unique (字段名)

    --外键约束:

    alter table 表名

    add constraint FK_字段名--"FK"为外键的缩写

    foreign key (字段名) references 关联的表名(关联的字段名) --注意'关联的表名'和'关联的字段名'

    alter table 表A add constraint FK_B foreign key (ticket_no) references 表B(ticket_no)

    alter table 表A add constraint FK_C foreign key (person_no) references 表C(person_no)

    alter table 成绩表 add constraint FK_StudentNo foreign key (StudentNo) references Student (StudentNo)

    ON UPDATE CASCADE ON DELETE CASCADE

    级联更新,级联删除,这样在删除主表Student时,成绩表中该学生的所有成绩都会删除。


    CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行
    SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL,如果使用该选项,必须保证子表列没有指定NOT NULL
    RESTRICT:拒绝对父表的删除或更新操作
    NO ACTION:标准SQL的关键字,在MySQL中与RESTRICT相同


    group by 索引

  • 相关阅读:
    SQL语法之DDL和DML
    流的装饰模式
    SharePoint 通过客户端API访问SharePoint状态栏
    SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
    30个特别酷的SharePoint站点
    在Sharepoint站点状态栏显示列表内容数据
    在SharePoint2010中如何使用状态栏?
    SPGridView and Pagination in SharePoint SharePoint中的SPGridView和分页功能
    一步步教你在SharePoint站点创建具有分页、排序、筛选功能的SPGridView Web部件
    使用对象模型读取SharePoint列表
  • 原文地址:https://www.cnblogs.com/newpython/p/6371749.html
Copyright © 2020-2023  润新知