msq_table's methods2
-- 删除数据 自增长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的更多相关文章
- msq_table's methods
-- 查看有哪些用户 host: % 代表任意地址都可以登录 host: localhost 代表仅本地可以连接select host,user from mysql.user; -- 建库 crea ...
- DIV+CSS中标签dl dt dd常用的用法
转自:http://smallpig301.blog.163.com/blog/static/9986093201010262499229/ < dl>< /dl>用来创建一个 ...
- Java的反射机制(Reflection)
反射机制 指可以在运动时加载.探知.使用编译期间完全未知的类 程序在运行状态中,可以动态加载一个只有名称的类,对于任意一个已加载的类,都能够获取这个类的属性和方法:对于任意一个对象可以调用它的任意一个 ...
- Java反射机制详解
Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反 ...
- C# 对象深度拷贝
转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...
- java反射之Class.getMethod与getDeclaredMethods()区别
Class对象的getMethods和getDeclaredMethods都是获取类对象的方法.但是又有所不同.废话不多说, 先看demo package com.westward; public c ...
- java反射快速入门(一)
本文会从以下几个方面讲起 ① 反射的简单解释 ② java反射的API接口 及 demo ③ 反射的优缺点.应用场景 一.什么是反射? java反射:在程序运行中动态获取类的信息,及动态调用对象的方法 ...
- 如何实现SQL事务的提交,又不对外进行污染(2)
紧接着上文,这里主要记录事务操作,实现多实体的功能 在SqlTran类中添加方法如下: 1.两个不同实体类型的事务方法: /// <summary> /// 执行事务(事务中不同实体) / ...
- Java反射探索研究(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankakay 摘要:本文详细深入讲解是Java中反射的机制,并介绍了如何通过反射来生成对象.调用函数.取得 ...
随机推荐
- [Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}
def permutationN(n): a=[None]*n for i in range(n): a[i]=i+1 sum=1 for j in range(n): sum*=(j+1) i=0 ...
- Codeforces gym101955 A【树形dp】
LINK 有n个大号和m个小号 然后需要对这些号进行匹配,一个大号最多匹配2个小号 匹配条件是大号和小号构成了前缀关系 字符串长度不超过10 问方案数 思路 因为要构成前缀关系 所以就考虑在trie树 ...
- C#浮点数保留位数
C#浮点数保留位数 这里用String.Forma("{0:F}",x);来解决. 下面是试验和截图 using System; using System.Collections. ...
- Let the Balloon Rise map一个数组
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the ...
- linux下普通用户无法使用sudo命令问题
今天在新装的linux虚拟机中使用sudo命令时,报错如下 We trust you have received the usual lecture from the local System Adm ...
- 93服务器上获取json数据
jdf u -p上传html文件,上传到page域名下:jdf u 上传css和js 上传到misc域名下: json数据放在html下,因为ajax请求是按照html路径走的,所以json数据放在h ...
- WPF 带CheckBox、图标的TreeView(转)
在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提供强大的ItemTemplate模板功能和自定义样式 ...
- C#中如何把byte[]数组转换成其他类型
http://bbs.csdn.net/topics/20447859 byte[] bytes = new byte[256]; //receive some stream from network ...
- Java HashMap原理
HashMap存储结构 HashMap中数据的存储是由数组与链表一起实现的 数组寻址非常容易,其时间复杂度为O(1),但是当要插入或删除数据时,时间复杂度就会变为O(n).链表插入和删除操作的内存复杂 ...
- SiteMap Editor for Microsoft Dynamics CRM 2011 使用说明
How to connect to CRM environments using this tool If you already connected to a CRM deployment usin ...