資料庫 資料定義

2021-09-25 19:06:31 字數 4418 閱讀 6298

關係資料庫系統支援**模式結構,其模式、外模式和內模式中的基本物件有資料庫模式、表、索引、檢視等。響應的,sql的資料定義功能包括資料庫模式定義、表定義、索引定義和檢視定義。

資料模式定義包括資料庫的建立、選擇、 修改、刪除、檢視等操作。

資料型別:指系統中所允許的資料的型別

資料庫模式定義

在mysql中可以使用 create database 或 create schema  語句建立資料庫。

use  db_name

alterdatabase或alter schema 語句

drop database或drop schema語句

show databases或show schemas

關鍵字"default"用於指定預設值

"character set"指定資料庫字符集(charset)

"collate"指定字符集的校隊規則

"if not database"避免資料庫已經存在而在新建的錯誤

"like"用於匹配指定的資料庫名稱

"where"指定資料庫名稱查詢範圍的條件

表定義

1.建立表

在mysql中,可以使用create table語句建立表

2.更新表

例:向資料庫mysql_test的表customers中新增一列,命名為cust_city,用於描述使用者所在城市,要求其不能為null,預設值為字串"wuhan",且該列位於原表 cust_***列之後。

例:將資料庫mysql_test中表cust_***列重新命名為***,且將其資料型別更改為字串長度為1的字元資料型別char(1),允許其為null,預設值為字元常量'm'.

alter table mysql_test.customers

change column cust_*** *** char(1) null default 'm';

例:將資料庫mysql_test中表customers的cust_city列的預設值修改為字元常量'beijing'

alter table mysql_test,customers

alter column cust_city set default 'beijing'

例:將資料庫mysql_test中表customers的cust_name列的資料型別由之前的字元長度為50的定長字元資料型別char(50)更改為字元長度為20的定長字元資料型別char(20),並將此列設定成表的第一列。

alter table mysql_test.customers

modify column cust_name char(20) first;

例:刪除資料庫mysql_test中表customers的cust_contact列

alter table mysql_test.customers

drop colimn cust_contact;

例:重新命名資料庫mysql_test中表customers的表名為backup_customers

later table mysql_stest.customers

rename to mysql_test.backup_customers

"temporary"語句建立的表為臨時表

"auto_increment"表中資料型別為整形的列設定自增屬性

"default"指定預設值

"null"指沒有值或缺值

"primary key"指定主鍵,主鍵一定為 not null

"foreign key"指定外來鍵

"index"指定索引+

"after"新增新列

"first"將新列為原表的第一行,不指定為最後

3.重新命名表

除了使用前面的語句alter table 語句還可以使用rename table語句來更改表名字,並可同時更改多個表。

例:將資料庫mysql_test表backup.customers再重新命名為customers。

rename table mysql_test.back_customers to mysql_test.customers;
4.刪除表  :可以同時刪除多行表

drop table

5.檢視表

例:顯示資料庫mysql_test中所有的表名

use mysql_test;

database changed

show tables

mysql支援用describe作為show columns from的一種快捷方式

索引定義

所謂索引,就是dbms根據表中的一列或若干列按照一定的順序建立的列值與記錄行之間的對應關係表,因而索引實質上是一張描述索引列的列值與原表中記錄行之間一一對應關係的有序表。

索引是提高資料檔案訪問的有效方法。

1.以檔案形式儲存,dbms將所有索引儲存在同一檔案中,占用磁碟空間。如果有大量的索引,索引檔案比資料檔案更快到達最大的檔案尺寸。如果一張表建立多種組合索引,索引檔案會膨脹的非常快。

2.索引在提高查詢速度的同時,卻會降低更新表的速度。

普通索引(index):普通索引,沒有限制。   關鍵字"index"或"key"

唯一性索引(unique):與普通索引不同的是,索引列中所有值都只能出現一次,必須是唯一的。

主鍵(primary key):主鍵是一種唯一性索引,且不能為空值。

create index:在已有表上建立索引,但該語句不能建立主鍵。

例:在資料庫mysql_test的表customers上,根據客戶姓名列的前三個字元建立乙個公升序索引index_customers.

create index index_customers

on mysql_test.customers (cust_name(3)asc)

例:在資料庫mysql_test的表customers上,根據客戶姓名列和客戶id號建立愛你乙個組合索引index_cust.

create index index_cust

on mysql_test.customers(cust_name,cust_id)

create table:索引可以在建立表的同時一起被建立。

例:在已有資料庫mysql_test上新建乙個包括產品賣家id號、姓名、位址、****、售賣產品型別、當月銷量等內容的產品賣家資訊表seller,要求在建立表的同時,為該錶新增由賣家id號和售賣產品型別組成的聯合主鍵,並在當月銷售量上建立索引。

use mysql_test

database changed

create table seller

(seller_id int not null auto_increment,

seller_name char (50)not null,

seller_address char(50)null,

seller_contact char(50)null,

product_type int(5)null,

sales int null,

primary key(seller_id,product_type),

index index_seller(sales)

);

alter table:在修改表的同時,向已有的表中新增索引。

例:使用alter table語句在資料庫mysql_test中表seller的姓名列上新增乙個非唯一的索引,取名為index_seller_name.

alter table mysql_test.seller

add index index_seller_name(seller_name)

show index:索引檢視

索引刪除

1.deop index

例:「在資料庫mysql_test的表customers上,根據客戶姓名列的前三個字元建立乙個公升序索引index_customers.」,刪除所建立的索引index_cust.

drop index index_cust on mysql_test.customers;
2.alter table

例:刪除資料庫mysql_test中表customers的主鍵和索引index_customers.

alter table mysql_test.customers

drop primary key,

deop index index_customers;

"unique"指定建立唯一性索引。

"asc"公升序

"desc"降序

資料庫基礎定義

資料庫 按照資料結構來組織 儲存和管理資料的建立在計算機儲存裝置上的倉庫。簡單來說是本身可視為電子化的檔案櫃 儲存電子檔案的處所,使用者可以對檔案中的資料進行新增 擷取 更新 刪除等操作。關聯式資料庫 建立在關係模型基礎上的資料庫,借助於集合代數等數學概念和方法來處理資料庫中的資料。如oracle,...

Flask SQLAlchemy定義資料庫模型

如果有兩張表role和user,那麼定義 role 和 user 模型 class role db.model tablename roles 表名 定義id列,型別為整型,flask sqlalchemy 要求每個模型都要定義主鍵,這一列經常命名為 id id db.column db.integ...

資料庫定義語言

ddl 資料庫定義語言 create table 建立表 alter table 修改表 drop table 刪除表 create index 建立索引 drop index 刪除索引 dml 資料操縱語言 insert 將資料插入表中 updata 修改表中的資料 delete 刪除表中的資料 ...