mysql基礎知識回顧

來源:互聯網
上載者:User

標籤: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,
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基礎知識回顧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.