设为首页 加入收藏

TOP

mysql基础知识回顾(一)
2014-11-24 03:16:28 来源: 作者: 【 】 浏览:8
Tags:mysql 基础知识 回顾
创建 数据库
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,
p
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MYSQL使用指南DOS下操作 下一篇CentOS-6.5下MariaDB-10基于XtraB..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·工业机器人TCP校准中 (2025-12-25 05:19:17)
·opc 通讯协议与 TCP (2025-12-25 05:19:15)
·labview中tcp/ip通信 (2025-12-25 05:19:13)
·新书介绍《Python数 (2025-12-25 04:49:47)
·怎么利用 Python 进 (2025-12-25 04:49:45)