SQL基礎語法—create語句


1 Create database語句

create database 語句是在MySQL實例上創建一個指定名的數據庫, create schema語句的語義和 create database是一樣的。先來看下create的語法:

Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

  • 當創建的數據庫本身存在而且沒有寫明 if not exists子句時,則創建數據庫的語句會報錯,實例如下:
  • create_specification子句指明創建的數據庫的屬性,並存儲在 db.opt文件中’
    • character set屬性指明此數據庫的默認字符集
    • collate屬性指明此數據庫的默認排序規則
  • 創建后的數據庫在數據文件所在的目錄會創建一個與數據庫名相同的文件目錄,用來包含后續創建的表文件;
  • 當然,也可以直接通過 mkdir的操作系統命令在數據目錄創建文件夾,則MySQL會識別為一個數據庫,並在執行 show databases命令時可以看到。

創建數據示例如下:

  • 通過在data目錄下創建目錄來創建數據庫:
    注意:8.0版本中不支持通過這種 方式創建數據庫。

  • 使用create database創建數據庫:

mysql> create database test;	##創建數據庫成功
Query OK, 1 row affected (0.08 sec)

mysql> create database test;	##再次創建數據庫失敗
ERROR 1007 (HY000): Can't create database 'test'; database exists
mysql> create database if not exists test;	##語句執行成功
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> use test;	##切換到test數據庫
Database changed

2 Create table語句

create table語句是在數據庫中創建表。在MySQL實例中可以 通過 ? create table方式來查看其語法:

Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]
    [partition_options]

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    [(create_definition,...)]
    [table_options]
    [partition_options]
    [IGNORE | REPLACE]
    [AS] query_expression

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    { LIKE old_tbl_name | (LIKE old_tbl_name) }

2.1 創建表的三種方式詳解

  • table_name表示被創建的表名,默認在當前數據庫下創建此表,當然也可以指定在某個數據庫下創建表;
  • if not exists表示當相同的表名存在是,則不執行此創建語句,避免語句執行錯誤
mysql> use test;
Database changed
mysql> create table student(sid int,sname varchar(10));  
Query OK, 0 rows affected (0.11 sec)

mysql> create table test1.student(sid int,sname varchar(10));  ##在test1數據庫下創建student表
Query OK, 0 rows affected (0.15 sec)
mysql> create table if not exists student(sid int,sname varchar(10));

  • temporary關鍵詞表示創建的是臨時表,臨時表僅對本次登陸MySQL實例的用戶可見,另外的數據庫連接不可見。當本連接斷開時,臨時表也會被 drop掉。
mysql> create temporary table temp1(sid int,sname varchar(10));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into temp1 values(1,'a');
Query OK, 1 row affected (0.01 sec)

##另一個數據庫連接執行相同的查詢語句查不到數據
mysql> select * from temp1;
ERROR 1146 (42S02): Table 'test.temp1' doesn't exist
##本數據庫連接斷開后再連接,臨時表也不存在
mysql> select * from temp1;
ERROR 1046 (3D000): No database selected

  • like關鍵詞表示基於另外一個表的定義賦值一個新的空表。空表上的字段屬性和索引都和原表相同
mysql> create table students_copy like student;
Query OK, 0 rows affected (0.04 sec)

##驗證like關鍵在創建表時只復制原表的字段信息,不復制表中的內容

##刪除students_copy表
mysql> drop table students_copy;
Query OK, 0 rows affected (0.13 sec)

##修改stedent表名為students
mysql> alter table student rename to students;
Query OK, 0 rows affected (0.06 sec)

##給students表添加一個gender int字段
mysql> alter table students add(gender int);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

##修改students表的字段屬性,sname不能為空,sid為主鍵
mysql> alter table students modify sname varchar(20) not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table students add primary key(sid);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

##給students表中的sname字段創建一個索引
mysql> create index idx_1 on students(sname);  
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

##使用like關鍵創建一個students_copy表
mysql> create table students_copy like students;
Query OK, 0 rows affected (0.03 sec)
  • create table ... as select語句表示創建表的同時將select的查詢結果數據插入到表中,但索引和主外鍵信息都不會同步過來
##復制所有字段上的數據
mysql> create table students_copy2 as select * from students where sid=1;
Query OK, 1 row affected (0.09 sec)
Records: 1  Duplicates: 0  Warnings: 0

##選擇指定字段上的數據進行復制
mysql> create table students_copy3 as select sid,sname from students where sid = 1;
Query OK, 1 row affected (0.10 sec)
Records: 1  Duplicates: 0  Warnings: 0

##使用desc語句查看表的字段信息
mysql> desc students_copy3;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int(11)     | NO   |     | NULL    |       |
| sname | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

2.2 create table的其他選項

  • ignore replace表示在插入數據的過程中如果新表中碰到違反唯一約束的情況下怎么處理, ignore表示不插入, replace表示替換已有的數據,默認兩個關鍵詞都不寫則碰到違反的情況會報錯
  • data_type表示定義的字段類型
  • not null/null表示字段是否允許為空,默認為 null表示允許為空, not null表示需要對此字段明確數值,或者要有默認值,否則報錯
mysql> create table student2(sid int not null,sname varchar(20));
Query OK, 0 rows affected (0.08 sec)

mysql> insert into student2(sname) values('dabric');
ERROR 1364 (HY000): Field 'sid' doesn't have a default value

  • default表示設置字段的默認值
mysql> create table student3(sid int not null,sname varchar(10),gender int default 0);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into student3 values(1,'tom',default);
Query OK, 1 row affected (0.08 sec)

mysql> insert into student3(sid,sname) values(1,'jerry');
Query OK, 1 row affected (0.10 sec)
  • auto_increment表示字段為整數或者浮點數類型的 value+1遞增數值, value為當前表中該字段最大的值,默認是從1開始遞增;一個表中只容許有一個自增字段,且該字段必須有 key屬性,不能含有 default屬性,如果是負值會被當成很大的正數

  • column_format目前僅在 ndb存儲引擎的表上有用,表示該字段的存儲類型是 fixed dynamic或者 default

  • storage 目前也僅在ndb存儲引擎的表上有用

  • constraint表示為主鍵、唯一鍵、外鍵等約束條件命名,如果沒有命名則MySQL會默認給一個

  • primary_key表示該字段為主鍵,主鍵字段必須唯一,必須非空,一個表中只能有一個主鍵,主鍵可以包含一個或多個字段

  • key/index表示索引字段

  • unique表示該字段為唯一屬性字段,且允許包含多個null

##創建sid字段索引的unique
mysql> create unique index idx_2 on students(sid);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create unique index idx_3 on students(sname);
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

##多個字段的unique,必須保證兩個字段疊加在一起不重復
mysql> create unique index idx_4 on students(sid,sname);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

  • foreign key表示該字段為外鍵字段
    • 如果某個字段是另一個表中的字段的外鍵,在刪除這條數據時,得先刪除和外鍵相關的那條數據
mysql> create table 'gender'(
	gender_id int(11) not null,
    name varchar(10) default null,
    primary key (gender_id)
)
mysql> create student5(sid int not nll primary key auti_increment,sname varchar(10) unique,gender int,constraint for_1 foreign key(gender) references gender(gender_id))

3 案例練習

設計一個學生選課數據庫系統

  • 創建一個名為course的數據庫

  • 在該數據庫下創建一下幾個表:

    • student表:sid整型自增主鍵,sname字符串64位,gender字符串12位,dept_id整型並外鍵到dept表的id字段

    • dept表:id整型自增主鍵,dept_name字符串64位

    • course表:id整型自增字段主鍵,course_name字符串64位,teacher_id整型外鍵到teacher表的id字段

    • teacher表:id整型自增字段主鍵,name字符創64位,dept_id整型外鍵到dept表的id字段

  • students表和teacher表的dept_id為非空

mysql> create table dept(id int primary key auto_increment,dept_name varchar(64));

mysql> create table student(sid int primary key auto_increment,sname varchar(64),gender varchar(12),dept_id int,constraint for_1 foreign key(dept_id) references dept(id));

mysql> alter table student modify dept_id int not null;

mysql> create table teacher(id int primary key auto_increment,name varchar(64),dept_id int not null,constraint for_2 foreign key(dept_id) references dept(id))

mysql> create table course(id int primary key auto_increment,course_name varchar(64),teacher_id int,constraint for_3 foreign key(teacher_id) references teacher(id));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM