SQL語句建表基礎教程

2021-09-27 09:10:08 字數 991 閱讀 6815

create database if not exists 資料庫名;

eg:create database if not exists test;

use 資料庫名;

eg:use test;

create table if not exists 表名 (

欄位名1 資料型別 [列級完整性約束條件] [comment 『該字段的注釋』],

欄位名2 資料型別 [列級完整性約束條件] [comment 『該字段的注釋』]

)在mysql資料庫中,欄位或列的注釋是用屬性comment來新增,注釋要加英文單引號『 』。

[常用資料型別]

integer(n):整數,n為整數的位數

double(n,m):浮點數,n為整個浮點數的最大位數,m為小數點後幾位,整數部分的最大位數為n-m

char:單個字元

varchar(n):多字元字串,n為設定的字串最大位元組數

datatime:日期時間,包括年月日時分秒更多

[列級完整性約束條件]

(中括號表示可加可不加)

1.非空約束:not null

2.唯一約束:unique

3.預設約束:default 預設值

4.主鍵約束:行的唯一標示

作用:1>方便查詢

2>用於引用:其他表可以通過此主鍵來引用本表的記錄

特點:非空 唯一

5.自增約束:auto_increment

必須定義在int型別的主鍵上

表中的最後乙個字段定義的最後是不加 ,的

eg:create table if not exists test_table1 (

test_id varchar(17) primary key not null comment 『主鍵』,

test_name varchar(10) not null comment 『姓名』

)

SQL語句基礎教程 二

三 技巧 1 1 1,1 2的使用,在sql語句組合時用的較多 where 1 1 是表示選擇全部 where 1 2 全部不選,如 if strwhere begin set strsql select count as total from tblname where strwhere ende...

SQL基礎教程

sql是用於訪問和處理資料庫的標準的計算機語言。什麼是sql?sql指結構化查詢語言 sql是我們有能力訪問資料庫 sql是一種更ansi的標準計算機語言 sql能做什麼?sql面向資料庫執行查詢 sql可從資料庫取回資料 sql可在資料庫中插入新的記錄 sql可以更新資料庫中的資料 sql可以從資...

SQL基礎教程

1.sql 指的是?structured query language 2.哪個 sql 語句用於從資料庫中提取資料?select 3.哪條 sql 語句用於更新資料庫中的資料?update 4.哪條 sql 語句用於刪除資料庫中的資料?delete 5.哪條 sql 語句用於在資料庫中插入新的資料...