cmd
mysql -u root -p
use zerobase;
create table tablename
(
column1 datatype NOT NULL,
column2 datatype NOT NULL,
...
constraint constraint_name primary key (column1, column2, ...)
)
create table person( pid int NOT NULL, name varchar(16), age int, sex char, primary key(pid) );
desc person;
create table animal ( name varchar(16) NOT NULL, type varchar(16) NOT NULL, age int, primary key (name, type));
desc animal;
alter table tablename
drop primary key;
alter table person drop primary key;
desc person;
alter table animal drop primary key;
desc animal;
alter table tablename
add primary key (column1, column2, ...)
alter table person add primary key (pid);
desc person;
alter table animal add constraint PK_animal primary key (name, type);
desc animal;
create table tablename
(
column1 datatype NOT NULL,
column2 datatype NOT NULL,
column3 datatype,
column4 datatype,
...
constraint constraint_name primary key (column1, column2, ...),
constraint constraint_name foreign key (column3, column4, ...) references ref_tablename(ref_column)
)
create table orders (
oid int not null,
order_no varchar(16),
pid int, primary key(oid),
constraint FK_person foreign key (pid) references person(pid));
desc orders;
create table job (
jid int not null,
name varchar(16),
pid int,
primary key (jid),
foreign key (pid) references person(pid) );
desc job;
show create table tablename;
show create table job;
alter table tablename
drop foriegn key FK_constraint;
alter table orders drop foreign key FK_person;
show create table orders;
alter tble tablename
add foreign key (column) references REF_tablename(REF_column);
alter table orders add foreign key (pid) references person(pid);
desc orders;
show create table orders;
-h database-1.cuiypimjmxdl.us-east-2.rds.amazonaws.com -P 3306 -u admin -p
use zerobase;
show tables;
desc police_station;
select count(distinct name) from police_station;
desc crime_status;
select count(distinct police_station) from crime_status;
select distinct name from police_station limit 3;
select distinct police_station from crime_status limit 3;
select c.police_station, p.name
from crime_status c, police_station p
where p.name like concat('์์ธ', c.police_station, '๊ฒฝ์ฐฐ์')
group by c.police_station, p.name;
alter table police_station add primary key (name);
desc police_station;
alter table crime_status add column reference varchar(16);
desc crime_status;
alter table crime_status add foreign key (reference) references police_station(name);
update crime_status c, police_station p set c.reference = p.name where p.name like concat('์์ธ', c.police_station, '๊ฒฝ์ฐฐ์');
select distinct police_station, reference from crime_status;
select c.police_station, p.address
from crime_status c, police_station p
where c.reference = p.name
group by c.police_station;
cd Documents/ds_sql_ws
mysql -u root -p
use zerobase;
show tables;
create table study (
study_id int not null,
study_date date,
study_time time,
patient_id int,
primary key (study_id),
constraint FK_study foreign key (patient_id) references person(pid));
desc study;
show create table study;
alter table study drop primary key;
alter table study drop foreign key FK_study;
show create table study;
alter table study add foreign key (patient_id) references person(pid);
show create table study;
alter table study add primary key (study_id);
desc study;
์ด๋ ต๋ค...์์ํ๋ค..
๐ป ์ถ์ฒ : ์ ๋ก๋ฒ ์ด์ค ๋ฐ์ดํฐ ์ทจ์ ์ค์ฟจ