管理資料可和表

2021-07-09 08:15:05 字數 2943 閱讀 3101

一.管理資料庫

1.建立和使用資料庫

create  datebase  資料庫名;

資料庫名在伺服器中必須是唯一的,並符合識別符號規則

2.鏈結到資料庫

use  資料庫名;

3.刪除資料庫

drop  database 資料庫名;

二. 資料型別

1.整數資料型別

tinyint 型 : 占用1個位元組

smallint型:占用2個位元組

mediumint型:占用3個位元組

int型:占用4個位元組

bigint型:占用8個位元組

2浮點資料型別

real型:占用4個位元組

float型:占用4個位元組

double型:占用8個位元組

deciml型:占用17個位元組 (m,d)m為總位數 ,d為小數   m必須大於d

numeric型與deciml型相同

3.字串資料型別

char型

varchar型:是變長字元資料,長度不超過8kb   其效率高於char

4.二進位制資料型別

tinyblob 型:範圍在0~225

blob型:0~65kb

mediumblob型:0~16m

longblob型:0~4g

5.邏輯範圍型別

型別                 範圍                           用途

boolean          0~1                         mysql沒有boolean型別,但boolean型別可以建立

bit資料型別:bit資料型別儲存邏輯真與假資料 占用1個位元組 其值為0或1 

6.日期資料型別

型別                           大小                              格式                                  範圍

year                          1位元組                             yyyy                             1901~2156

date                            3位元組                           yyyy-mm-dd                   1000-01-01~9999-12-31

timestamp                4位元組                          yyyy-mm-dd/hh:mm :ss

datatime                    8位元組                         yyy-mm-dd/hh:mm:ss

7.貨幣資料型別

money型:佔8個位元組

smallmoey型:佔4個位元組

三.管理表

1.建立表

create table 《表名》(

列名     列的資料型別   列的約束 

2.刪除表

drop  table  表名

通過外來鍵約束連線在一起的表不能刪  在刪除表之前 必須先刪除約束

3.複製表

create table t_copystudent select * from t_student;

select * from t_copystudent where 1=0;

/*值複製表結構,不複製表的內容*/

create table t_cpoystudent2 select * from t_student 

where 1=0;

select * from t_cpoystudent2;

create table t_cpoy3 like t_student;

select * from t_cpoy3;

4.修改表

select *from t_student;

/*增加一列資料*/

alter table t_student add phone bigint;

/*刪除一列資料 column列*/

alter table t_student drop column phone;

/*修改列的名稱*/

alter table t_student change column phonenum phonenumber bigint;

alter table t_student change column phonenumber phonenum bigint;

/*修改列的資料型別*/

alter table t_student change phonenumber phonenumber bigint;

alter table t_student modify phonenumber varchar(100);

/*修改表名*/

alter table t_student rename t_stu;

select * from t_stu;

/*建立索引*/ 索引是從左到右解索的

create index nameindex on t_stu(s_name);

select * from t_stu where s_name="houyuan";

select * from t_stu where s_name="%yuan";#  查詢以yuan結尾的 索引不會起作用 因為%是不確定的值

select * from t_stu where s_name="hou%"

/*刪除索引*/

alter table t_stu drop index nameindex;

alter table 語句同其他選項一起 可以更改已有資料的表的結構,包括新增 、修改或刪除列、主鍵、外來鍵或其他約束等

使用create index 和drop index語句可以建立和刪除索引

管理資料庫和表

第乙個字元必須是 unicode中定義的字母包括拉丁字母a z和a z,以及來自其他語言的字母字元 以及下劃線 符號或者數字符號 識別符號不能是所用rdbms的保留字。不允許嵌入空格或其他特殊字元。2.刪除資料庫 語法為 drop database 3.整型資料型別 tinyint 型 1個位元組 ...

資料庫(1) 資料庫管理和表管理

一 資料庫管理 1 查詢所有資料庫 show databases 2 建立資料庫 create database name default character set utf8 指定預設字符集建立資料庫可以省略 3 檢視資料庫的預設字符集 show create database name 4 資料...

DDL(管理資料庫和表)

create database 資料庫名 例 mysql create database runoob 也可以直接使用mysqladmin來建立資料庫 mysqladmin u root p create 資料庫名show databases 注意結尾的sdrop database 資料庫名 也可以...