標籤:速度 -128 upd 添加 匯出資料庫 sql指令碼 esc bsp 方式
http://www.mysql.com/downloads/mysql/
連接埠號碼:3306 使用者名稱:root 密碼:自訂
串連到MySQL伺服器 >mysql -uroot -proot [-h127.0.0.1]
中斷連線 >exit 或者>quit
命令
顯示MySQL中所有的資料庫 > show databases;
切換到mydb資料庫 > use mydb;
查看資料庫中所有的表 > show tables;
查看錶結構 > desc t_user;
查看資料庫版本和時間 > select version(),now();
建立資料庫 >create database mydb;
資料類型(整型)
| 資料類型 |
無符號範圍 |
有符號範圍 |
| TINYINT |
0~255 |
-128~127 |
| SMALLINT |
0~65535 |
-32768~32767 |
| MEDIUMINT |
0~16777215 |
-8388608~8388607 |
| INT(Integer) |
0~4294967295 |
-2147483648~2147483647 |
| BIGINT |
0~18446744073709551 615 |
-9223372036854775808~9223372036854775807 |
資料類型(浮點型)
| 資料類型 |
無符號範圍 |
有符號範圍 |
| FLOAT |
0,(1.175 494 351 E-38,3.402 823 466 E+38) |
-3.402 823 466 E+38,1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) |
| DOUBLE |
(1.797 693 134 862 315 7 E+308,2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) |
0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) |
| DECIMAL(M,D) |
|
|
資料類型(字元型)
| 資料類型 |
大小 |
用途 |
| CHAR |
0-255位元組 |
定長字串 |
| VARCHAR |
0-255位元組 |
變長字串 |
| TINYTEXT |
0-255位元組 |
短文本字串 |
| TEXT |
0-65 535位元組 |
長文本資料 |
| MEDIUMTEXT |
0-16 777 215位元組 |
中等長度文本資料 |
| LONGTEXT |
0-4 294 967 295位元組 |
極大文本資料 |
資料類型(日期時間型)
| 類型 |
範圍 |
格式 |
| DATE |
1000-01-01~9999-12-31 |
YYYY-MM-DD |
| TIME |
-838:59:59~838:59:59 |
HH:MM:SS |
| DATETIME |
1000-01-01 00:00:00~9999-12-31 23:59:59 |
YYYY-MM-DD HH:MM:SS |
| TIMESTAMP |
1970-01-01 00:00:00~2037年 |
YYYY-MM-DD HH:MM:SS |
建立表:
create table stu(
id int auto_increment,
stuname varchar(20) not null,
classid int not null,
primary key(id)
);
create table cls(
id int auto_increment,
clsname varchar(20) not null,
primary key(id)
);
添加資料:
insert into stu (stuname,classid)values(‘lyl‘,2),(‘fj‘,2);
insert into cls (clsname)values(‘java‘),(‘c++‘);
主鍵 ? 在設計表時總是要定義表的主鍵 ? 表的主鍵設計策略 ? 任意兩行都不具備相同的主索引值 ? 每行都必須具有一個主索引值(主鍵不允許Null列) ? 主鍵和業務無關,不更改,不重用 ? 主鍵可以是一個列或者是多個列的組合 ? 使用PRIMARY KEY(XXX)來聲明一個主鍵列 ? 如果使用多個列作為主鍵則需要如下聲明:PRIMARY KEY(XXX,XXX)
主鍵自動成長 AUTO_INCREMENT ? 用來標示一個自動成長列 ? 一個表中只允許有一個自動成長列 >create table t_student ( id int auto_increment, … );
刪除表 > drop table t_student;
非null約束 stuname varchar(20) not null
預設約束 stuaddress varchar(100) default ‘鄭州‘
唯一約束 stuname varchar(20) not null unique
更改表
添加一列 >alter table t_student add tel char(20);
刪除一列 >alter table t_student drop column tel;
添加唯一約束 >alter table t_student add constraint uk_username unique(usercode);
添加主鍵約束 >alter table t_user add constraint pk_t_user_id primary key t_user(id);
添加預設約束 >alter table t_user alter password set default ‘123456‘;
添加非null約束 >alter table t_teacher modify column uname varchar(20) not null;
重新命名表 >rename table t_student to t_stu
匯出資料庫 >mysqldump -hlocalhost -uroot -proot mydb>C:/a.sql
大量匯入SQL指令碼 >source C:/a.sql
運算子
邏輯運算子 ? = 等於 ? <>,!= 不等於 ? < 小於 ? > 大於 ? <= 小於等於 ? >= 大於等於 ? between 在指定的兩個值之間
關係運算子 ? and ? or ? not
update >update t_student set stuname = ‘Alex‘,age = ‘26‘ [where id = 1];
where ? where stuname = ‘tom‘ ? where stuname = ‘tom‘ or stuname = ‘alex‘ ? where id > 1 and id < 3 ? where id != 23 ? where id = 12 or id = 34 ? where id in (12,34) ? where id between 12 and 34 ? where password is null ? where password is not null
delete >delete from t_student [where id = 1];
truncate >truncate table t_student; TRUNCATE TABLE用於刪除表中的所有記錄,但該語句不能包含WHERE語句,該操作運行速度比 DELETE語句快
建立外鍵 >alter table t_user add schoolid int; >alter table t_user add constraint fk_student_cus foreign key(schoolid) references t_school(id);
刪除外鍵 > alter table t_user drop foreign key fk_student_cus
基本查詢 查詢所有的列 >SELECT * FROM vendors;
查詢指定的列 >SELECT vend_id,vend_name,vend_address,vend_city FROM vendors;
如果查詢時需要顯示表中的所有列,盡量避免使用萬用字元(*),而要採用寫出所有列名的方式進行查詢, 因為採用萬用字元查詢會降低程式的查詢效能
mysql資料庫基本操作