mysql基礎知識回顧,mysql基礎知識

來源:互聯網
上載者:User

mysql基礎知識回顧,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的相關知識問題

上面下載的是一個jar包應該是C#串連MySQL的jar包,
程式設計語言提供了一個介面,資料庫廠商必須實現了這個介面
才能讓自己的資料庫支援這個程式設計語言,就像微軟的SqlServer
想要支援Java,還是要老老實實的寫一個sqljdbc.jar。
mssql是微軟的SqlServer資料庫,有圖形化操作軟體SqlServer manager studio,官網上可以免費下載。
 
mysql怎入門?

1.我建立一個資料庫,再使用時卻出現Database changed 然後就不知道怎麼辦了
Database changed 表示你現在可以操作移動到的資料庫裡的資料了,接下來我們就可以使用select查詢,用delete刪除,用update更新,還可以寫並且調用功能更強大的預存程序和觸發器~可以做得事情很多的~不過要一點一點來~
2.我一直不明白,資料庫是建立在自己的電腦裡嗎?
資料庫就建在你的電腦裡~當然要通過資料結構來組織和儲存這些資料。關於如何儲存的不用著急去探究,先從應用入手。
3.怎樣建立和其他電腦的資料聯絡
你學的是C,那麼可以使用ODBC進行串連(MicroSoft的開發工具和語言基本都用這個),如果以後用java了,則可以使用jdbc。這個也不要太急著弄。
4.樣本資料庫有什麼用啊?
樣本資料庫可以用來測試資料庫是否裝載成功
對於初學者來說,還可以用於練習(在還沒掌握DDL語言前,可以先使用這些既存的表來練習DML的)。

這樣說可能有點亂,所以最後總結一下
記得我們當年將資料庫的時候是從什麼是關聯式資料庫講起的,講實體,講關係,講關係代數,講函數依賴。。。。講了很多偏原理的基礎知識後,才開始上機操作。
不過自學的話,建議倒著來,先操作,再去看為什麼這麼操作。
不知道你的《Mysql技術內幕》是不是第四版的,剛上網下了一本,書不錯,不過建議從第二章順著往下看,先學怎麼操作DB,這樣能比較快上手,從而建立成就感,更有興趣學下去。
再配合這在網上找點視頻(項目推進的那種),上手勢比較快的。
現在愛看書不愛打遊戲的青年不多了,兄弟,挺你,祝好運~
 

相關文章

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.