資料庫表 庫操作

2021-10-02 07:30:47 字數 2178 閱讀 4566

一、庫的管理

1、建立庫

create database (if not exist )庫名;

2、庫的修改;

rename database 舊庫名 to 新庫名;

#修改資料庫的字符集: alter database 資料庫名稱 character set 字符集名稱;

3、庫的刪除;

drop database(if exist) 庫名;

4、查詢所有資料庫的名稱:

show databases;

5、查詢當前正在使用的資料庫名稱 :select database(); 

使用資料庫 :use 資料庫名稱;

6、檢視表結構:

desc 表名字;

二、表的管理

1、表的建立;

create table 表名(

列名 列的型別【(長度) 約束】,

列名 列的型別【(長度) 約束】,

列名 列的型別【(長度) 約束】,

列名 列的型別【(長度) 約束】

2、表的修改:

語法:alter table 表名 add|drop|modify|change column 列名 【列型別 約束】;

①增加列

保留字:add

基本語句為(圓括號為必要項,方括號為選擇項。下同):

alter table (table_name) add (column_name) (col_type) [default|auto_increment|…]

②刪除列(字段)

保留字:drop,column

基本語句:

alter table (table_name) drop column (column_name)

③修改字段型別(長度)

保留字:modify

alter table (table_name) modify (col_name) (new_type)

④修改欄位名

保留字:change

基本語句:

alter table 表名 change 原欄位名 新欄位名 字段型別 約束條件

alter table (table_name) change (old_col_name) (new_col_name) [約束]

⑤新增欄位的外來鍵約束

保留字:add,constraint

alter table (table_name1) add constraint foreign key (col_name) references table_name2(ref_column)

⑥新增欄位的唯一性約束

基本語句:

alter table (table_name) add unique(col_name)

⑦為已有字段增加主鍵約束

alter table tbl_name add primary key (index_col_name,…);

括號中為欄位名,可以為乙個或者多個

⑧修改表名

alter table 舊表名 rename to 新錶名;

⑨表中插入資料

insert into 表名稱 values  (值1, 值2,....)

3、表的刪除

drop table 【if exist 】表名;

#通用寫法:

drop database if exist 舊庫名;

create database 新庫名;

drop table if exist 舊表名;

create database 新錶名();

4、表的複製

①僅複製表的結構

create table copy like 表名;

②複製表的結果+資料

create table 新錶名  select  *  from 舊表名; 

③只複製部分資料

create table 新錶名 selete  列1,列2  from 舊表名 where =『選擇條件』;

④僅僅複製某些字段

create table 新錶名 select 列名1,列名2  from 舊表名  where  1=2;

5、檢視當前資料庫中所有表名稱:

show tables;

6、檢視表結構;

desc table; 

MySQL資料庫 資料庫 表 資料常用操作

建立資料庫 create database 資料庫名 顯示所有可訪問資料庫 show databases 顯示當前選中的資料庫 select database 刪除資料庫 drop database 資料庫名 建立表 create table table name id int not null a...

資料庫操作 建立表 操作表

一般有兩種建立表的方法 1.使用具有互動式建立和管理表的工具 2.使用mysql語句。利用create table建立表,必須給出下列訊息 1.表的名字,在關鍵字create table之後給出 2.表列的名字和定義,用逗號分隔。create table customers cust id int ...

資料庫表操作總結

1 修改列的資料型別 alter table student inf alter column student name nvarchar 10 null 注 alter 修改 student inf 表名 column 列 student name 列名 nvarchar 10 資料型別 null...