创建数据库

creat table test(

#整数通常用于int

test_id int,

#十进制通常使用decimal

test_price decimal,

#普通文本通常使用。并使用Default指定默认值

test_name varchar(255) default "Xxx",

#大文本类型使用test

test_desc text,

#图片使用blob

test_img blob,

#日期类型使用DateTime

test_date datetime,

);

-----------------------------------------------------------------

mysql 支持的列类型

1.tinyint,smallint,mediumint,int,bigint

2.float,double

3.decimal(dec)

4.date

5.time

6.datetime

7.timestamp

8.year

9.char

10.varchar

11.binary(定长的二进制字符串类型。以二进制形式保存字符串)

12.varbinary

13.tinyblob,blob,mediumblob,longblob

14.tinytext,text,mediumtext,longtext

15.enum('value1','value2'...)//枚举类型(仅仅能是当中之中的一个)

16.set('value1','value2'...)//集合类型(能够是当中几个)

--------------------------------------------------------------------

#创建数据表。该数据表和user_info全然同样,数据也全然同样

create table hehe

as

select * from user_info;

---------------------------------------------------------------------

#改动表的结构的语法

alert table 表名

add(

#能够定义多个列定义

colum_name datatype [default expr],

...

);

---------------------------------------------------------------------

#为hehe数据表添加一个hehe_id字段,该字段类型为int





alter table hehe

add hehe_id int;

#为hehe数据包添加aaa,bbb字段。两个字段的类型都为varchar(25)

alter table hehe

add aaa varchar(25),bbb varchar(25);

----------------------------------------------------------------------

#将hehe表的hehe_id列改动为varchar(255)类型

alter table hehe

modify hehe_id varchar(255);

#将hehe表的bbb列改动为int类型

alter table hehe

modify bbb int;

----------------------------------------------------------------------

#删除指定的列

alter table hehe

drop column_name

#重命名数据表

alter table hehe

rename to wawa;

----------------------------------------------------------------------

#将wawa表的字段bbb字段重命名为ddd

alter table wawa

change bbb ddd int;

#删除表

drop table 表名

----------------------------------------------------------------------

数据库约束

not null

unique

primary key

foreign key

check

#not null约束

create table hehe(

#建立了非空约束,这意味着hehe_id不能够为null

hehe_id int not null,

#mysql为空约束不能指定名字

hehe_name varchar(25) default 'xyz' not null,

#以下列能够为空。默认值就是为null

hehe_gender varchar(28) null

);





---------------------------------------------------------------------

#添加非空约束

alter table hehe

modify hehe_gender  varchar(30) not null

#取消非空约束

alter table hehe

modify hehe_name varchar(3) null;

#取消非空约束。并指定默认值

alter table hehe

modify hehe_name varchar(255) default 'abc' null;

-------------------------------------------------------------------

unique约束

#建立表时创建唯一约束,使用列级约束语法建立约束

create table unique_test(

#建立了非空约束,着意味着test_id不能够为null

test_id int not null,

#建立了unique约束

test_name varchar(30) unique

);

#创建表时,使用表级约束语法建立约束

create table unique_test(

test_id int not null,

test_name varchar(30),

test_pass varchar(30),

#使用表级约束创建唯一约束

unique(test_name),

constraint test_uk unique(test_pass)

#constrain test1_uk unique(test_name,test_pass)

);

#改动唯一约束

alter table unique_test

add unique(test_name,test_pass);

#为表添加约束

alter table unique_test

modify test_name varchar(30) unique;

-------------------------------------------------------------------

primary key约束

create table primaryKey_test(

primaryKey_id int primary key,

test_name varchar(255)

);

create table primaryTest(

primary_id int not null,

primary_name varchar(29),

constraint pk_test primary key(primary_id)

);





#删除主键约束

alter table test

drop primary key;

#使用表级语法添加主键约束

alter table test

add primary key(test_id ,test_name);

#使用列级约束语法添加主键约束

alter table test

modify test_name varchar(30) primary key;

------------------------------------------------------------------

#为了保证从表參照的主表存在。通常应该先建立主表

create table teacher_table(

#auto_increment

teacher_id int auto_increment,

teacher_name varchar(255),


primary key(teacher_id)

);

create table student_table(

student_id int auto_increment primary key,

student_name varchar(255),

#指定java_teacher參照到teacher_table的teacher_id的列

java_teacher int references teacher_table(teacher_id)

#java_teacher int

#foreign key(java_teacher) references teacher_table(teacher_id)

#constraint student_teacher_fk foreign key(java_teacher) references

teacher_table(teacher_id)

);

---------------------------------------------------------------------------------------------

#check约束

create table test(

test_id int auto_increment primary key,

test_age int not null,

check(test_age>0 and test_age<120)

)

版权声明:本文博主原创文章,博客,未经同意不得转载。

mysql回想一下基础知识的更多相关文章

  1. mysql学习之基础知识

    一.什么是数据库 一般而言,数据库(Database)是按照数据结构来组织.存储和管理数据的仓库.我们也可以将数据存储在文件中,但是在文件中读写数据速度相对较慢.所以,使用关系型数据库管理系统(RDB ...

  2. MySQL系列(一)--基础知识(转载)

    安装就不说了,网上多得是,我的MySQL是8.0版本,可以参考:CentOS7安装MySQL8.0图文教程和MySQL8.0本地访问设置为远程访问权限 我的MySQL安装在阿里云上面,阿里云向外暴露端 ...

  3. mysql原生语句基础知识

    要操作数据库,首先要登录mysql: *mysql -u root -p 密码 创建数据库: *create database Runoob(数据库名); 删除数据库: *drop database ...

  4. mysql的优化基础知识

    1.查看各种SQL执行的频率 mysql> show status like 'Com_select';--Com_insert,Com_delete,connections(试图连接mysql ...

  5. mysql的部分基础知识....

  6. mysql基础知识详解

    分享一些mysql数据库的基础知识. 1.每个客户端连接都会从服务器进程中分到一个属于它的线程.而该连接的相应查询都都会通过该线程处理.2.服务器会缓存线程.因此并不会为每个新连接创建或者销毁线程.3 ...

  7. 深入理解mysql之BDB系列(1)---BDB相关基础知识

        深入理解mysql之BDB系列(1) ---BDB相关基础知识 作者:杨万富   一:BDB体系结构 1.1.BDB体系结构 BDB总体的体系结构如图1.1所看到的,包括五个子系统(见图1.1 ...

  8. 深入了解mysql它BDB系列(1)---BDB基础知识

        深入了解mysql它BDB系列(1) ---BDB关基础知识 作者:杨万富   一:BDB体系结构 1.1.BDB体系结构 BDB总体的体系结构如图1.1所看到的.包括五个子系统(见图1.1中 ...

  9. MySQL系列(一)--基础知识大总结

    MySQL系列(一)---基础知识大总结 前言:本文主要为mysql基础知识的大总结,mysql的基础知识很多,这里只是作为简单的介绍,但是具体的细节还是需要自行搜索.当然本文还有很多遗漏的地方,后续 ...

随机推荐

  1. jvm调优经验分享

    当Java程序申请内存,超出VM可分配内纯的时候,VM首先可能会GC,假设GC完还是不够,或者申请的直接超够VM可能有的,就会抛出内 存溢出异常.从VM规范中我们能够得到,一下几种异常. java.l ...

  2. Storm集群中执行的各种组件及其并行

    一.Storm中执行的组件      我们知道,Storm的强大之处就是能够非常easy地在集群中横向拓展它的计算能力,它会把整个运算过程切割成多个独立的tasks在集群中进行并行计算.在Storm中 ...

  3. Storyboard 经常用法总结-精华版

    1.prepareForSegue: Now we know what the destinationViewController is we can set its data properties. ...

  4. 应用层open(read、write、close)怎样调用驱动open(read、write、close)函数的?

    应用层open(read.write.close)怎样调用驱动open(read.write.close)函数的? 华清远见2014-09-29   北京海淀区 张俊浩 三大数据结构关系图

  5. hdu4521(线段树+dp)

    传送门:小明系列问题——小明序列 题意:有n个数,求间距大于d的最长上升序列. 分析:dp[i]表示在i点以a[i]结束距离大于d的最长上升序列,然后每更新到第i点时,取i-d之前小于a[i]的数为结 ...

  6. Factorization Machines 学习笔记(四)学习算法

      近期学习了一种叫做 Factorization Machines(简称 FM)的算法.它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...

  7. Oracle 11g服务详解

    装上Oracle之后大家都会感觉到我们的电脑慢了下来,如何提高计算机的速度呢?我们应该打开必要的服务,关闭没有用的服务.下面是Oracle服务的详解: Oracle ORCL VSS Writer S ...

  8. firebug登陆之数据包分析

    登陆之数据包分析 工具: python-urllib2   |  firefox+firebug或者chrome,用浏览器打开登陆页面之后,按F12键会默认打开开发者工具或者启动firebug,点击n ...

  9. hyper-v 报错 0x80070569

    在Windows8.1Pro版使用过程中,突然出现HYPER-V无法创建虚拟机.显示错误为: 登录失败:未授予用户在此计算机上的请求登录类型.(0x80070569). 回顾起近期通过组策略增强了系统 ...

  10. ARM体系结构与编程

    ARM处理器的7中执行模式:usr.fiq.irq.svc.abt.und.sys. ARM处理器共37个寄存器:31个通用寄存器(未备份寄存器R0-R7,在全部模式下指的都是同一个物理寄存器:备份寄 ...