-- 删除数据 自增长id被占用
-- 清楚所有数据并重置id 1 truncate table name;
-- 主键(唯一) id int primary key;
-- 主键内容不能重复,不能为空 create table test(
id int primary key auto_increment, -- auto_increment 自增长
name varchar(10) not null,
age char(5)
);

-- 删除 主键 有自增长需要先删除自增长 通过change改变该字段的自增长属性

alter table student change id id int not null;

alter table test drop primary key;

-- 复合主键

create table test(
id int;
name varchar(10) not null,
age char(5) null,
primary key(id,name,age)
); -- 只要是张表 都有id字段 通常只要存在id字段 ,那么基本上是主键 -- 自增 配合主键id使用 auto_increment
create table test(
id int primary key auto_increment;
name varchar(10) not null,
age char(5) null,
); -- 唯一键 unique key
-- 表里没有主键时,设置唯一键会显示主键的标志 PRI,但他本质还是唯一键
  
-- 删除唯一键
alter table test drop index `name`; -- name为字段名字

create table test(
name varchar(10) unique key , -- name 不能重复 要求唯一了
age char(5)
); create table test(
name varchar(10), -- name 不能重复 要求唯一了
age char(5),
unique key 'uni_name'(name)
);
-- 外键 foreign key 关联两张表的

-- 外键删除
alter table foreign_key drop foreign key `外键名`;  -- 外键名通过查看建表过程


外键的几种模式
* `restrict` 默认
* `cascade` 级联
* 父表更新 子表更新 父表删除 子表删除
* `set null` 置空
* 父表更新 子表置空 父表删除 子表置空 (前提 支持为空)

 
create database test;
use test; create table student(
id int primary key auto_increment,
name varchar(20) not null,
gender char(15) not null
); create table course(
id int primary key auto_increment,
name varchar(20)
); insert into student(name,gender) values('Which','male');
insert into student(name,gender) values('Tuple','male');
insert into student(name,gender) values('Rose','female'); insert into course values(1,'Python');
insert into course values(2,'Web');
insert into course values(3,'Java'); create table mark(
id int,
stu_id int,
course_id int,
score int not null,
primary key(id,stu_id,course_id),
constraint FK_ha foreign key(id) references course(id), -- 设置外键的名字
foreign key(course_id) references student(id)
);
-- 外键名字通过 show create table mark; 查看 insert into mark values(1,1,1,99);
insert into mark values(2,1,2,98);
insert into mark values(3,2,1,60);
insert into mark values(3,3,2,99);
mysql> select * from student;
+----+-------+--------+
| id | name | gender |
+----+-------+--------+
| 1 | Which | male |
| 2 | Tuple | male |
| 3 | Rose | female |
+----+-------+--------+
3 rows in set (0.00 sec) mysql> select * from course;
+----+--------+
| id | name |
+----+--------+
| 1 | Python |
| 2 | Web |
| 3 | Java |
+----+--------+
3 rows in set (0.00 sec) mysql> select * from mark;
+----+--------+-----------+-------+
| id | stu_id | course_id | score |
+----+--------+-----------+-------+
| 1 | 1 | 1 | 99 |
| 2 | 1 | 2 | 98 |
| 3 | 2 | 1 | 60 |
| 3 | 3 | 2 | 99 |
+----+--------+-----------+-------+
4 rows in set (0.00 sec)

 

msq_table's methods2的更多相关文章

  1. msq_table's methods

    -- 查看有哪些用户 host: % 代表任意地址都可以登录 host: localhost 代表仅本地可以连接select host,user from mysql.user; -- 建库 crea ...

  2. DIV+CSS中标签dl dt dd常用的用法

    转自:http://smallpig301.blog.163.com/blog/static/9986093201010262499229/ < dl>< /dl>用来创建一个 ...

  3. Java的反射机制(Reflection)

    反射机制 指可以在运动时加载.探知.使用编译期间完全未知的类 程序在运行状态中,可以动态加载一个只有名称的类,对于任意一个已加载的类,都能够获取这个类的属性和方法:对于任意一个对象可以调用它的任意一个 ...

  4. Java反射机制详解

    Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反 ...

  5. C# 对象深度拷贝

    转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...

  6. java反射之Class.getMethod与getDeclaredMethods()区别

    Class对象的getMethods和getDeclaredMethods都是获取类对象的方法.但是又有所不同.废话不多说, 先看demo package com.westward; public c ...

  7. java反射快速入门(一)

    本文会从以下几个方面讲起 ① 反射的简单解释 ② java反射的API接口 及 demo ③ 反射的优缺点.应用场景 一.什么是反射? java反射:在程序运行中动态获取类的信息,及动态调用对象的方法 ...

  8. 如何实现SQL事务的提交,又不对外进行污染(2)

    紧接着上文,这里主要记录事务操作,实现多实体的功能 在SqlTran类中添加方法如下: 1.两个不同实体类型的事务方法: /// <summary> /// 执行事务(事务中不同实体) / ...

  9. Java反射探索研究(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankakay 摘要:本文详细深入讲解是Java中反射的机制,并介绍了如何通过反射来生成对象.调用函数.取得 ...

随机推荐

  1. c++跨文件变量声明

    常量是内部链接的,可以直接定义;  变量是外部链接,如果在头文件定义的话,如果出现多次引用同一个头文件的编译单元,就会引发multi redifine错误, 这个时候就要只是声明变量来解决:exter ...

  2. Robot Framework 自定义库

    进入  python安装路径\Lib\site-packages 创建文件夹,库名 创建py文件,myclass.py 创建py文件,__init__.py 导入自定义库 遇到的问题: python版 ...

  3. ElasticSearch(九):springboot项目集成消息中间件activeMQ

    目的:为了将elasticsearch做成单独的服务,那么我们必须解耦,也就是业务逻辑和搜索模块是没有关系的,并且是异步的.那么项目之间通信,使用的选择有限,消息中间件是一个不错的选择. 消息中间件常 ...

  4. 为什么我们不应该使用微信或者 QQ 作为团队协作的 IM 工具?

    如果你的团队没有觉得微信是低效的团队 IM 工具,那只有两种可能: 团队成员很少使用微信进行私人的生活和娱乐. 你就是一个低效的团队,而且还不自知. 本文内容 微信,连接一切 每个人都有微信 微信,低 ...

  5. Ordering Tasks 拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  6. 《DSP using MATLAB》Problem 4.11

    代码: %% ---------------------------------------------------------------------------- %% Output Info a ...

  7. jQuery prop() 方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. index.do

  9. XSL自定义函数

    利用微软的XSL的继承特性,用户可以自定义XSL函数.基本原理是用户写的脚本代码写在msxsl中,并设置这部分msxsl继承到用户自定义空间中,那么用户就可以通过用户自定义空间使用msxsl中的脚本代 ...

  10. 【MVC】Controller的使用

    1,控制器中所有的动作方法必须声明为public,如声明为private或protected,将不被视为动作方法. 如果将Action声明为private,或者是添加[NonAction]属性,则不对 ...