mysql數據庫基本操做

下載地址 http://www.mysql.com/downloads/mysql/java

端口號:3306 用戶名:root 密碼:自定義mysql

鏈接到MySQL服務器 >mysql -uroot -proot [-h127.0.0.1]c++

斷開鏈接 >exit 或者>quitsql

命令數據庫

顯示MySQL中全部的數據庫 > show databases;服務器

切換到mydb數據庫 > use mydb;性能

查看數據庫中全部的表 > show tables;ui

查看錶結構 > desc t_user;spa

查看數據庫版本和時間 > 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;

若是查詢時須要顯示錶中的全部列,儘可能避免使用通配符(*),而要採用寫出全部列名的方式進行查詢, 由於採用通配符查詢會下降程序的查詢性能

相關文章
相關標籤/搜索