MySQL建立表的語句

來源:互聯網
上載者:User

標籤:校正   values   for   http   字元集   字元   script   ddr   ima   

 1 show variables like ‘character_set_client‘;#查詢字元集 2 show databases;#列出所有的伺服器上的資料庫alter 3 create database if not exists test;#建立一個資料庫 4 drop database fk;#刪除資料庫 5 show tables from test;#顯示一個資料庫中的表 6 use test; 7  8 create table tb_dept( 9     Id int primary key auto_increment,#部門編號 整形 主鍵 自增長10     Name varchar(18),#部門名稱11     description varchar(100)#描述12 );13 14 show tables from test;15 16 desc tb_dept;#查看錶資訊17 18 show create table tb_dept;19 20 use test;21 #員工表22 create table tb_emp(23 id int primary key auto_increment,#auto_increment只是MySQL特有的24 Name varchar(18),25 sex varchar(2),26 age int,27 address varchar(200),28 email varchar(100)29 );30 31 drop table tb_dept;32 #修改列類型33 #注意:不是任何情況下都可以去修改的,34 #只有當欄位只包含空值時才可以修改。35 alter table tb_emp modify sex  varchar(4);36 #增加列37 alter table tb_emp add tel varchar(12);38 #刪除列39 alter table tb_emp drop tel;40 alter table tb_emp drop column tel;41 #列改名42 alter table tb_emp change Name emp_Name varchar(18);43 #更改表名44 alter table tb_emp rename emp;45 rename table emp to tb_emp;46 47 insert into dept_emp (Name,sex,age,address,email)values(‘‘,‘‘,‘‘,‘‘,‘‘);48 49 #約束50 #是在表上強制執行地資料校正規則,主要用於保證資料庫地完整性51 /*52 not null 53 unique 唯一鍵tb_depttb_dept54 primary key 55 foreign key 外鍵56 check 檢查57 */58 59 create table tb_emp(60 id int primary key auto_increment,61 Name varchar(18),62 sex varchar(2) default‘男‘ check(sex=‘男‘or sex=‘女‘),#表級寫法check 在mysql中不起作用63 age int,64 address varchar(200),65 email varchar(100) unique,66 dept_id int,#references tb_dept(id) #表級寫法外鍵不起作用67 constraint foreign key fk_emp(dept_id) references tb_dept(id)68 );69 70 #建立表之後在添加71 alter table tb_emp add constraint foreign key fk_emp(dept_id) references tb_dept(id);

 

 

MySQL建立表的語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.